From 996205b366bcab2cff5d777463ed2c77a1167acf Mon Sep 17 00:00:00 2001 From: SupperZum Date: Tue, 9 Apr 2024 13:45:06 +0300 Subject: [PATCH] add without_ext --- module/core/proper_path_tools/src/path.rs | 76 ++++++++++++ .../core/proper_path_tools/tests/inc/mod.rs | 2 +- .../tests/inc/without_ext.rs | 114 ++++++++++++++++++ 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 module/core/proper_path_tools/tests/inc/without_ext.rs diff --git a/module/core/proper_path_tools/src/path.rs b/module/core/proper_path_tools/src/path.rs index 35560947f7..a7320f5d34 100644 --- a/module/core/proper_path_tools/src/path.rs +++ b/module/core/proper_path_tools/src/path.rs @@ -301,11 +301,87 @@ pub( crate ) mod private Ok( format!( "{}_{}_{}_{}", timestamp, pid, tid, count ) ) } + + /// Extracts the parent directory and file stem (without extension) from the given path. + /// + /// This function takes a path and returns an Option containing the modified path without the extension. + /// If the input path is empty or if it doesn't contain a file stem, it returns None. + /// + /// # Arguments + /// + /// * `path` - An object that can be converted into a Path reference, representing the file path. + /// + /// # Returns + /// + /// An Option containing the modified path without the extension, or None if the input path is empty or lacks a file stem. + /// + /// # Examples + /// + /// ``` + /// use std::path::PathBuf; + /// use proper_path_tools::path::without_ext; + /// + /// let path = "/path/to/file.txt"; + /// let modified_path = without_ext(path); + /// assert_eq!(modified_path, Some(PathBuf::from("/path/to/file"))); + /// ``` + /// + /// ``` + /// use std::path::PathBuf; + /// use proper_path_tools::path::without_ext; + /// + /// let empty_path = ""; + /// let modified_path = without_ext(empty_path); + /// assert_eq!(modified_path, None); + /// ``` + /// + pub fn without_ext( path : impl AsRef< std::path::Path > ) -> Option< std::path::PathBuf > + { + use std::path::Path; + use std::path::PathBuf; + + if path.as_ref().to_string_lossy().is_empty() + { + return None; + } + + let path_buf = Path::new( path.as_ref() ); + + let parent = match path_buf.parent() + { + Some( parent ) => parent, + None => return None, + }; + let file_stem = match path_buf.file_stem() + { + Some( name ) => + { + let ends = format!( "{}/", name.to_string_lossy() ); + if path.as_ref().to_string_lossy().ends_with( &ends ) + { + ends + } + else + { + String::from( name.to_string_lossy() ) + } + + } + None => return None, + }; + + let mut full_path = parent.to_path_buf(); + full_path.push( file_stem ); + + Some( PathBuf::from( full_path.to_string_lossy().replace( "\\", "/" ) ) ) + } + } crate::mod_interface! { + protected use without_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..3e581c3e4c 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 without_ext; #[ cfg( feature = "path_unique_folder_name" ) ] mod path_unique_folder_name; diff --git a/module/core/proper_path_tools/tests/inc/without_ext.rs b/module/core/proper_path_tools/tests/inc/without_ext.rs new file mode 100644 index 0000000000..fa1c5bf11e --- /dev/null +++ b/module/core/proper_path_tools/tests/inc/without_ext.rs @@ -0,0 +1,114 @@ +#[ allow( unused_imports ) ] +use super::*; + +#[ test ] +fn empty_path() +{ + let path = ""; + let expected = None; + assert_eq!( the_module::path::without_ext( path ), expected ); +} + +#[ test ] +fn txt_extension() +{ + let path = "some.txt"; + let expected = "some"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn path_with_non_empty_dir_name() +{ + let path = "/foo/bar/baz.asdf"; + let expected = "/foo/bar/baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn hidden_file() +{ + let path = "/foo/bar/.baz"; + let expected = "/foo/bar/.baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn file_with_composite_file_name() +{ + let path = "/foo.coffee.md"; + let expected = "/foo.coffee"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn path_without_extension() +{ + let path = "/foo/bar/baz"; + let expected = "/foo/bar/baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_1() +{ + let path = "./foo/.baz"; + let expected = "./foo/.baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_2() +{ + let path = "./.baz"; + let expected = "./.baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_3() +{ + let path = ".baz.txt"; + let expected = ".baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_4() +{ + let path = "./baz.txt"; + let expected = "./baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_5() +{ + let path = "./foo/baz.txt"; + let expected = "./foo/baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_6() +{ + let path = "./foo/"; + let expected = "./foo/"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_7() +{ + let path = "baz"; + let expected = "baz"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} + +#[ test ] +fn relative_path_8() +{ + let path = "baz.a.b"; + let expected = "baz.a"; + assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected ); +} \ No newline at end of file