@@ -5,14 +5,14 @@ use std::path::{Component, Path, PathBuf};
5
5
6
6
///
7
7
pub mod to_normal_path_components {
8
- use std:: ffi :: OsString ;
8
+ use std:: path :: PathBuf ;
9
9
10
10
/// The error used in [`ToNormalPathComponents::to_normal_path_components()`](super::ToNormalPathComponents::to_normal_path_components()).
11
11
#[ derive( Debug , thiserror:: Error ) ]
12
12
#[ allow( missing_docs) ]
13
13
pub enum Error {
14
- #[ error( "Input path \" {path}\" contains relative or absolute components" , path = std :: path :: Path :: new ( . 0 . as_os_str ( ) ) . display( ) ) ]
15
- NotANormalComponent ( OsString ) ,
14
+ #[ error( "Input path \" {path}\" contains relative or absolute components" , path = . 0 . display( ) ) ]
15
+ NotANormalComponent ( PathBuf ) ,
16
16
#[ error( "Could not convert to UTF8 or from UTF8 due to ill-formed input" ) ]
17
17
IllegalUtf8 ,
18
18
}
@@ -26,54 +26,54 @@ pub trait ToNormalPathComponents {
26
26
27
27
impl ToNormalPathComponents for & Path {
28
28
fn to_normal_path_components ( & self ) -> impl Iterator < Item = Result < & OsStr , to_normal_path_components:: Error > > {
29
- self . components ( ) . map ( |component| match component {
30
- Component :: Normal ( os_str) => Ok ( os_str) ,
31
- _ => Err ( to_normal_path_components:: Error :: NotANormalComponent (
32
- self . as_os_str ( ) . to_owned ( ) ,
33
- ) ) ,
34
- } )
29
+ self . components ( ) . map ( |c| component_to_os_str ( c, self ) )
35
30
}
36
31
}
37
32
38
33
impl ToNormalPathComponents for PathBuf {
39
34
fn to_normal_path_components ( & self ) -> impl Iterator < Item = Result < & OsStr , to_normal_path_components:: Error > > {
40
- self . components ( ) . map ( |component| match component {
41
- Component :: Normal ( os_str) => Ok ( os_str) ,
42
- _ => Err ( to_normal_path_components:: Error :: NotANormalComponent (
43
- self . as_os_str ( ) . to_owned ( ) ,
44
- ) ) ,
45
- } )
35
+ self . components ( ) . map ( |c| component_to_os_str ( c, self ) )
36
+ }
37
+ }
38
+
39
+ fn component_to_os_str < ' a > (
40
+ component : Component < ' a > ,
41
+ path_with_component : & Path ,
42
+ ) -> Result < & ' a OsStr , to_normal_path_components:: Error > {
43
+ match component {
44
+ Component :: Normal ( os_str) => Ok ( os_str) ,
45
+ _ => Err ( to_normal_path_components:: Error :: NotANormalComponent (
46
+ path_with_component. to_owned ( ) ,
47
+ ) ) ,
46
48
}
47
49
}
48
50
49
51
impl ToNormalPathComponents for & BStr {
50
52
fn to_normal_path_components ( & self ) -> impl Iterator < Item = Result < & OsStr , to_normal_path_components:: Error > > {
51
- self . split ( |b| * b == b'/' ) . filter ( |c| !c. is_empty ( ) ) . map ( |component| {
52
- gix_path:: try_from_byte_slice ( component. as_bstr ( ) )
53
- . map_err ( |_| to_normal_path_components:: Error :: IllegalUtf8 )
54
- . map ( Path :: as_os_str)
55
- } )
53
+ self . split ( |b| * b == b'/' ) . filter_map ( bytes_component_to_os_str)
56
54
}
57
55
}
58
56
59
57
impl ToNormalPathComponents for & str {
60
58
fn to_normal_path_components ( & self ) -> impl Iterator < Item = Result < & OsStr , to_normal_path_components:: Error > > {
61
- self . split ( '/' ) . filter ( |c| !c. is_empty ( ) ) . map ( |component| {
62
- gix_path:: try_from_byte_slice ( component. as_bytes ( ) )
63
- . map_err ( |_| to_normal_path_components:: Error :: IllegalUtf8 )
64
- . map ( Path :: as_os_str)
65
- } )
59
+ self . split ( '/' ) . filter_map ( |c| bytes_component_to_os_str ( c. as_bytes ( ) ) )
66
60
}
67
61
}
68
62
69
63
impl ToNormalPathComponents for & BString {
70
64
fn to_normal_path_components ( & self ) -> impl Iterator < Item = Result < & OsStr , to_normal_path_components:: Error > > {
71
- self . split ( |b| * b == b'/' ) . filter ( |c| !c. is_empty ( ) ) . map ( |component| {
72
- gix_path:: try_from_byte_slice ( component. as_bstr ( ) )
73
- . map_err ( |_| to_normal_path_components:: Error :: IllegalUtf8 )
74
- . map ( Path :: as_os_str)
75
- } )
65
+ self . split ( |b| * b == b'/' ) . filter_map ( bytes_component_to_os_str)
66
+ }
67
+ }
68
+
69
+ fn bytes_component_to_os_str ( component : & [ u8 ] ) -> Option < Result < & OsStr , to_normal_path_components:: Error > > {
70
+ if component. is_empty ( ) {
71
+ return None ;
76
72
}
73
+ gix_path:: try_from_byte_slice ( component. as_bstr ( ) )
74
+ . map_err ( |_| to_normal_path_components:: Error :: IllegalUtf8 )
75
+ . map ( Path :: as_os_str)
76
+ . into ( )
77
77
}
78
78
79
79
/// Access
0 commit comments