Skip to content

Commit

Permalink
path_ext
Browse files Browse the repository at this point in the history
  • Loading branch information
SupperZum committed Apr 9, 2024
1 parent 43475cd commit 05ac4a4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
54 changes: 52 additions & 2 deletions module/core/proper_path_tools/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 @@ -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;
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 05ac4a4

Please sign in to comment.