Skip to content

Commit

Permalink
Merge branch 'alpha' into ext
Browse files Browse the repository at this point in the history
  • Loading branch information
SupperZum committed May 1, 2024
2 parents 24dc9a5 + 79f21ac commit 56e935e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
61 changes: 60 additions & 1 deletion module/core/proper_path_tools/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,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 @@ -734,7 +791,9 @@ pub( crate ) mod private

crate::mod_interface!
{
protected use ext; protected use path_relative;
protected use ext;
protected use exts;
protected use path_relative;
protected use rebase;
protected use path_common;
protected use is_glob;
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_exts;
mod path_ext;
mod path_common;
mod rebase_path;
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 56e935e

Please sign in to comment.