diff --git a/module/core/proper_path_tools/src/path.rs b/module/core/proper_path_tools/src/path.rs index 150cfe8485..81af7d961e 100644 --- a/module/core/proper_path_tools/src/path.rs +++ b/module/core/proper_path_tools/src/path.rs @@ -295,7 +295,6 @@ pub( crate ) mod private .chars() .filter( | c | c.is_digit( 10 ) ) .collect(); - // dbg!( &tid ); Ok( format!( "{}_{}_{}_{}", timestamp, pid, tid, count ) ) @@ -815,11 +814,64 @@ pub( crate ) mod private + /// 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 exts; protected use path_relative; + protected use ext; + protected use exts; + protected use path_relative; protected use rebase; protected use path_common; protected use without_ext; diff --git a/module/core/proper_path_tools/tests/inc/mod.rs b/module/core/proper_path_tools/tests/inc/mod.rs index ddab6de7c0..4107e22677 100644 --- a/module/core/proper_path_tools/tests/inc/mod.rs +++ b/module/core/proper_path_tools/tests/inc/mod.rs @@ -5,6 +5,7 @@ mod path_normalize; mod path_is_glob; mod absolute_path; mod path_exts; +mod path_ext; mod without_ext; mod path_common; mod rebase_path; 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