Skip to content

Commit

Permalink
Merge pull request #1291 from SupperZum/ext
Browse files Browse the repository at this point in the history
READY : path_ext
  • Loading branch information
Wandalen authored May 3, 2024
2 parents 07a22a0 + 19e2101 commit a9a02dd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
56 changes: 54 additions & 2 deletions module/core/proper_path_tools/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ pub( crate ) mod private
.chars()
.filter( | c | c.is_digit( 10 ) )
.collect();

// dbg!( &tid );

Ok( format!( "{}_{}_{}_{}", timestamp, pid, tid, count ) )
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions module/core/proper_path_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions module/core/proper_path_tools/tests/inc/path_ext.rs
Original file line number Diff line number Diff line change
@@ -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 ), "" );
}

0 comments on commit a9a02dd

Please sign in to comment.