Skip to content

Commit

Permalink
Merge pull request #1292 from SupperZum/path_exts
Browse files Browse the repository at this point in the history
READY : path_exts
  • Loading branch information
Wandalen authored May 1, 2024
2 parents de533df + c210556 commit 79f21ac
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
59 changes: 58 additions & 1 deletion module/core/proper_path_tools/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,63 @@ pub( crate ) mod private
Ok( format!( "{}_{}_{}_{}", timestamp, pid, tid, count ) )
}

/// Extracts multiple extensions from the given path.
///
/// This function takes a path and returns a vector of strings representing the extensions of the file.
/// If the input path is empty or if it doesn't contain any extensions, it returns an empty vector.
///
/// # Arguments
///
/// * `path` - An object that can be converted into a Path reference, representing the file path.
///
/// # Returns
///
/// A vector of strings containing the extensions of the file, or an empty vector if the input path is empty or lacks extensions.
///
/// # Examples
///
/// ```
/// use proper_path_tools::path::exts;
///
/// let path = "/path/to/file.tar.gz";
/// let extensions = exts( path );
/// assert_eq!( extensions, vec![ "tar", "gz" ] );
/// ```
///
/// ```
/// use proper_path_tools::path::exts;
///
/// let empty_path = "";
/// let extensions = exts( empty_path );
/// let expected : Vec< String > = vec![];
/// assert_eq!( extensions, expected );
/// ```
///
pub fn exts( path : impl AsRef< std::path::Path > ) -> Vec< String >
{
use std::path::Path;

if let Some( file_name ) = Path::new( path.as_ref() ).file_name()
{
if let Some( file_name_str ) = file_name.to_str()
{
let mut file_name_str = file_name_str.to_string();
if file_name_str.starts_with( '.' )
{
file_name_str.remove( 0 );
}
if let Some( dot_index ) = file_name_str.find( '.' )
{

let extensions = &file_name_str[ dot_index + 1.. ];

return extensions.split( '.' ).map( | s | s.to_string() ).collect()
}
}
}
vec![]
}

/// Finds the common directory path among a collection of paths.
///
/// Given an iterator of path strings, this function determines the common directory
Expand Down Expand Up @@ -684,7 +741,7 @@ pub( crate ) mod private

crate::mod_interface!
{
protected use path_relative;
protected use exts; protected use path_relative;
protected use rebase;
protected use path_common;
protected use is_glob;
Expand Down
2 changes: 1 addition & 1 deletion module/core/proper_path_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
mod path_normalize;
mod path_is_glob;
mod absolute_path;
mod path_common;
mod path_exts;mod path_common;
mod rebase_path;
mod path_relative;

Expand Down
50 changes: 50 additions & 0 deletions module/core/proper_path_tools/tests/inc/path_exts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#[ allow( unused_imports ) ]
use super::*;

#[ test ]
fn empty_path()
{
let path = "";
let expected : Vec< String > = vec![];
assert_eq!( the_module::path::exts( path ), expected );
}

#[ test ]
fn txt_extension()
{
let path = "some.txt";
let expected : Vec< String > = vec![ "txt".to_string() ];
assert_eq!( the_module::path::exts( path ), expected );
}

#[ test ]
fn path_with_non_empty_dir_name()
{
let path = "/foo/bar/baz.asdf";
let expected : Vec< String > = vec![ "asdf".to_string() ];
assert_eq!( the_module::path::exts( path ), expected );
}

#[ test ]
fn hidden_file()
{
let path = "/foo/bar/.baz";
let expected : Vec< String > = vec![];
assert_eq!( the_module::path::exts( path ), expected );
}

#[ test ]
fn several_extension()
{
let path = "/foo.coffee.md";
let expected : Vec< String > = vec![ "coffee".to_string(), "md".to_string() ];
assert_eq!( the_module::path::exts( path ), expected );
}

#[ test ]
fn hidden_file_extension()
{
let path = "/foo/bar/.baz.txt";
let expected : Vec< String > = vec![ "txt".to_string() ];
assert_eq!( the_module::path::exts( path ), expected );
}

0 comments on commit 79f21ac

Please sign in to comment.