From 05ac4a429f5f033ce11d48bc5ef19fd2c0432825 Mon Sep 17 00:00:00 2001 From: SupperZum Date: Tue, 9 Apr 2024 14:10:07 +0300 Subject: [PATCH 1/2] path_ext --- module/core/proper_path_tools/src/path.rs | 54 ++++++++++++++++++- .../core/proper_path_tools/tests/inc/mod.rs | 1 + .../proper_path_tools/tests/inc/path_ext.rs | 44 +++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 module/core/proper_path_tools/tests/inc/path_ext.rs diff --git a/module/core/proper_path_tools/src/path.rs b/module/core/proper_path_tools/src/path.rs index 35560947f7..52ecb308fc 100644 --- a/module/core/proper_path_tools/src/path.rs +++ b/module/core/proper_path_tools/src/path.rs @@ -295,17 +295,67 @@ pub( crate ) mod private .chars() .filter( | c | c.is_digit( 10 ) ) .collect(); - // dbg!( &tid ); Ok( format!( "{}_{}_{}_{}", timestamp, pid, tid, count ) ) } + /// Extracts the extension from the given path. + /// + /// This function takes a path and returns a string representing the extension of the file. + /// If the input path is empty or if it doesn't contain an extension, it returns an empty string. + /// + /// # Arguments + /// + /// * `path` - An object that can be converted into a Path reference, representing the file path. + /// + /// # Returns + /// + /// A string containing the extension of the file, or an empty string if the input path is empty or lacks an extension. + /// + /// # Examples + /// + /// ``` + /// use proper_path_tools::path::ext; + /// + /// let path = "/path/to/file.txt"; + /// let extension = ext(path); + /// assert_eq!(extension, "txt"); + /// ``` + /// + /// ``` + /// use proper_path_tools::path::ext; + /// + /// let empty_path = ""; + /// let extension = ext(empty_path); + /// assert_eq!(extension, ""); + /// ``` + /// + pub fn ext( path : impl AsRef< std::path::Path > ) -> String + { + use std::path::Path; + + if path.as_ref().to_string_lossy().is_empty() + { + return String::new(); + } + let path_buf = Path::new( path.as_ref() ); + match path_buf.extension() + { + Some( ext ) => + { + ext.to_string_lossy().to_string() + } + None => String::new(), + } + } + } + crate::mod_interface! { - + protected use ext; 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..24bbb8d2fb 100644 --- a/module/core/proper_path_tools/tests/inc/mod.rs +++ b/module/core/proper_path_tools/tests/inc/mod.rs @@ -4,6 +4,7 @@ use super::*; mod path_normalize; mod path_is_glob; mod absolute_path; +mod path_ext; #[ cfg( feature = "path_unique_folder_name" ) ] mod path_unique_folder_name; diff --git a/module/core/proper_path_tools/tests/inc/path_ext.rs b/module/core/proper_path_tools/tests/inc/path_ext.rs new file mode 100644 index 0000000000..63de0bfcca --- /dev/null +++ b/module/core/proper_path_tools/tests/inc/path_ext.rs @@ -0,0 +1,44 @@ +#[ allow( unused_imports ) ] +use super::*; + +#[ test ] +fn empty_path() +{ + let path = ""; + assert_eq!( the_module::path::ext( path ), "" ); +} + +#[ test ] +fn txt_extension() +{ + let path = "some.txt"; + assert_eq!( the_module::path::ext( path ), "txt" ); +} + +#[ test ] +fn path_with_non_empty_dir_name() +{ + let path = "/foo/bar/baz.asdf"; + assert_eq!( the_module::path::ext( path ), "asdf" ); +} + +#[ test ] +fn hidden_file() +{ + let path = "/foo/bar/.baz"; + assert_eq!( the_module::path::ext( path ), "" ); +} + +#[ test ] +fn several_extension() +{ + let path = "/foo.coffee.md"; + assert_eq!( the_module::path::ext( path ), "md" ); +} + +#[ test ] +fn file_without_extension() +{ + let path = "/foo/bar/baz"; + assert_eq!( the_module::path::ext( path ), "" ); +} \ No newline at end of file From a976bd18a435154a9b78ed03d1b9f9682fdc4d86 Mon Sep 17 00:00:00 2001 From: SupperZum Date: Tue, 9 Apr 2024 14:44:34 +0300 Subject: [PATCH 2/2] upd --- module/core/proper_path_tools/src/path.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/module/core/proper_path_tools/src/path.rs b/module/core/proper_path_tools/src/path.rs index 52ecb308fc..8b6ce83fea 100644 --- a/module/core/proper_path_tools/src/path.rs +++ b/module/core/proper_path_tools/src/path.rs @@ -319,16 +319,16 @@ pub( crate ) mod private /// use proper_path_tools::path::ext; /// /// let path = "/path/to/file.txt"; - /// let extension = ext(path); - /// assert_eq!(extension, "txt"); + /// let extension = ext( path ); + /// assert_eq!( extension, "txt" ); /// ``` /// /// ``` /// use proper_path_tools::path::ext; /// /// let empty_path = ""; - /// let extension = ext(empty_path); - /// assert_eq!(extension, ""); + /// let extension = ext( empty_path ); + /// assert_eq!( extension, "" ); /// ``` /// pub fn ext( path : impl AsRef< std::path::Path > ) -> String