@@ -5,14 +5,14 @@ use std::path::{Component, Path, PathBuf};
55
66///
77pub mod to_normal_path_components {
8- use std:: ffi :: OsString ;
8+ use std:: path :: PathBuf ;
99
1010 /// The error used in [`ToNormalPathComponents::to_normal_path_components()`](super::ToNormalPathComponents::to_normal_path_components()).
1111 #[ derive( Debug , thiserror:: Error ) ]
1212 #[ allow( missing_docs) ]
1313 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 ) ,
1616 #[ error( "Could not convert to UTF8 or from UTF8 due to ill-formed input" ) ]
1717 IllegalUtf8 ,
1818 }
@@ -26,54 +26,54 @@ pub trait ToNormalPathComponents {
2626
2727impl ToNormalPathComponents for & Path {
2828 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 ) )
3530 }
3631}
3732
3833impl ToNormalPathComponents for PathBuf {
3934 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+ ) ) ,
4648 }
4749}
4850
4951impl ToNormalPathComponents for & BStr {
5052 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)
5654 }
5755}
5856
5957impl ToNormalPathComponents for & str {
6058 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 ( ) ) )
6660 }
6761}
6862
6963impl ToNormalPathComponents for & BString {
7064 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 ;
7672 }
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 ( )
7777}
7878
7979/// Access
0 commit comments