From 5235b5bd84b4cd459b7a3ee92f2d3b772a271de4 Mon Sep 17 00:00:00 2001 From: SupperZum Date: Tue, 9 Apr 2024 14:41:02 +0300 Subject: [PATCH 1/2] path_exts --- module/core/proper_path_tools/src/path.rs | 58 ++++++++++++++++++- .../core/proper_path_tools/tests/inc/mod.rs | 2 +- .../proper_path_tools/tests/inc/path_exts.rs | 50 ++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 module/core/proper_path_tools/tests/inc/path_exts.rs diff --git a/module/core/proper_path_tools/src/path.rs b/module/core/proper_path_tools/src/path.rs index 35560947f7..2d5e2c6e35 100644 --- a/module/core/proper_path_tools/src/path.rs +++ b/module/core/proper_path_tools/src/path.rs @@ -301,11 +301,67 @@ pub( crate ) mod private Ok( format!( "{}_{}_{}_{}", timestamp, pid, tid, count ) ) } + /// Extracts multiple extensions from the given path. + /// + /// This function takes a path and returns a vector of strings representing the extensions of the file. + /// If the input path is empty or if it doesn't contain any extensions, it returns an empty vector. + /// + /// # Arguments + /// + /// * `path` - An object that can be converted into a Path reference, representing the file path. + /// + /// # Returns + /// + /// A vector of strings containing the extensions of the file, or an empty vector if the input path is empty or lacks extensions. + /// + /// # Examples + /// + /// ``` + /// use proper_path_tools::path::exts; + /// + /// let path = "/path/to/file.tar.gz"; + /// let extensions = exts(path); + /// assert_eq!(extensions, vec!["tar", "gz"]); + /// ``` + /// + /// ``` + /// use proper_path_tools::path::exts; + /// + /// let empty_path = ""; + /// let extensions = exts(empty_path); + /// assert_eq!(extensions, vec![]); + /// ``` + /// + pub fn exts( path : impl AsRef< std::path::Path > ) -> Vec< String > + { + use std::path::Path; + + if let Some( file_name ) = Path::new( path.as_ref() ).file_name() + { + if let Some( file_name_str ) = file_name.to_str() + { + let mut file_name_str = file_name_str.to_string(); + if file_name_str.starts_with( '.' ) + { + file_name_str.remove( 0 ); + } + if let Some( dot_index ) = file_name_str.find( '.' ) + { + + let extensions = &file_name_str[ dot_index + 1.. ]; + + return extensions.split( '.' ).map( | s | s.to_string() ).collect() + } + } + } + vec![] + } + } crate::mod_interface! { - + protected use exts; protected use is_glob; protected use normalize; protected use canonicalize; diff --git a/module/core/proper_path_tools/tests/inc/mod.rs b/module/core/proper_path_tools/tests/inc/mod.rs index cc74b4a975..6db0955c21 100644 --- a/module/core/proper_path_tools/tests/inc/mod.rs +++ b/module/core/proper_path_tools/tests/inc/mod.rs @@ -4,6 +4,6 @@ use super::*; mod path_normalize; mod path_is_glob; mod absolute_path; - +mod path_exts; #[ cfg( feature = "path_unique_folder_name" ) ] mod path_unique_folder_name; diff --git a/module/core/proper_path_tools/tests/inc/path_exts.rs b/module/core/proper_path_tools/tests/inc/path_exts.rs new file mode 100644 index 0000000000..2e96a55341 --- /dev/null +++ b/module/core/proper_path_tools/tests/inc/path_exts.rs @@ -0,0 +1,50 @@ +#[ allow( unused_imports ) ] +use super::*; + +#[ test ] +fn empty_path() +{ + let path = ""; + let expected : Vec< String > = vec![]; + assert_eq!( the_module::path::exts( path ), expected ); +} + +#[ test ] +fn txt_extension() +{ + let path = "some.txt"; + let expected : Vec< String > = vec![ "txt".to_string() ]; + assert_eq!( the_module::path::exts( path ), expected ); +} + +#[ test ] +fn path_with_non_empty_dir_name() +{ + let path = "/foo/bar/baz.asdf"; + let expected : Vec< String > = vec![ "asdf".to_string() ]; + assert_eq!( the_module::path::exts( path ), expected ); +} + +#[ test ] +fn hidden_file() +{ + let path = "/foo/bar/.baz"; + let expected : Vec< String > = vec![]; + assert_eq!( the_module::path::exts( path ), expected ); +} + +#[ test ] +fn several_extension() +{ + let path = "/foo.coffee.md"; + let expected : Vec< String > = vec![ "coffee".to_string(), "md".to_string() ]; + assert_eq!( the_module::path::exts( path ), expected ); +} + +#[ test ] +fn hidden_file_extension() +{ + let path = "/foo/bar/.baz.txt"; + let expected : Vec< String > = vec![ "txt".to_string() ]; + assert_eq!( the_module::path::exts( path ), expected ); +} \ No newline at end of file From 7b35cec92c0c884f508562c52b38890c659d8e10 Mon Sep 17 00:00:00 2001 From: SupperZum Date: Tue, 9 Apr 2024 14:43:53 +0300 Subject: [PATCH 2/2] upd --- module/core/proper_path_tools/src/path.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/module/core/proper_path_tools/src/path.rs b/module/core/proper_path_tools/src/path.rs index 2d5e2c6e35..b9952756d8 100644 --- a/module/core/proper_path_tools/src/path.rs +++ b/module/core/proper_path_tools/src/path.rs @@ -320,16 +320,17 @@ pub( crate ) mod private /// use proper_path_tools::path::exts; /// /// let path = "/path/to/file.tar.gz"; - /// let extensions = exts(path); - /// assert_eq!(extensions, vec!["tar", "gz"]); + /// let extensions = exts( path ); + /// assert_eq!( extensions, vec![ "tar", "gz" ] ); /// ``` /// /// ``` /// use proper_path_tools::path::exts; /// /// let empty_path = ""; - /// let extensions = exts(empty_path); - /// assert_eq!(extensions, vec![]); + /// let extensions = exts( empty_path ); + /// let expected : Vec< String > = vec![]; + /// assert_eq!( extensions, expected ); /// ``` /// pub fn exts( path : impl AsRef< std::path::Path > ) -> Vec< String >