Skip to content

Commit

Permalink
Merge pull request #1293 from SupperZum/change_ext
Browse files Browse the repository at this point in the history
NOT READY : Change ext
  • Loading branch information
Wandalen committed May 13, 2024
2 parents ba0a0cd + e1fde20 commit 5eb9939
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
60 changes: 58 additions & 2 deletions module/core/proper_path_tools/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,59 @@ pub( crate ) mod private
Some( PathBuf::from( full_path.to_string_lossy().replace( "\\", "/" ) ) )
}

/// Replaces the existing path extension with the provided extension.
///
/// If the input path is empty or contains non-ASCII characters, or if the provided extension is empty or contains non-ASCII characters,
/// the function returns None.
/// Otherwise, it returns an Option containing the modified path with the new extension.
///
/// # Arguments
///
/// * `path` - An object that can be converted into a Path reference, representing the file path.
/// * `ext` - A string slice representing the new extension to be appended to the path.
///
/// # Returns
///
/// An Option containing the modified path with the new extension, or None if any of the input parameters are invalid.
///
/// # Examples
///
/// ```
/// use std::path::PathBuf;
/// use proper_path_tools::path::change_ext;
///
/// let path = "/path/to/file.txt";
/// let modified_path = change_ext( path, "json" );
/// assert_eq!( modified_path, Some( PathBuf::from( "/path/to/file.json" ) ) );
/// ```
///
/// ```
/// use std::path::PathBuf;
/// use proper_path_tools::path::change_ext;
///
/// let empty_path = "";
/// let modified_path = change_ext( empty_path, "txt" );
/// assert_eq!( modified_path, None );
/// ```
///
pub fn change_ext( path : impl AsRef< std::path::Path >, ext : &str ) -> Option< std::path::PathBuf >
{
use std::path::PathBuf;
if path.as_ref().to_string_lossy().is_empty() || !path.as_ref().to_string_lossy().is_ascii() || !ext.is_ascii()
{
return None;
}

let without_ext = without_ext( path )?;
if ext.is_empty()
{
Some( without_ext )
} else
{
Some( PathBuf::from( format!( "{}.{}", without_ext.to_string_lossy(), ext ) ) )
}
}

/// 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 @@ -818,6 +871,7 @@ pub( crate ) mod private
Some( normalize( rebased_path ) )
}


/// Computes the relative path from one path to another.
///
/// This function takes two paths and returns a relative path from the `from` path to the `to` path.
Expand Down Expand Up @@ -932,6 +986,8 @@ 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.
Expand Down Expand Up @@ -984,10 +1040,10 @@ pub( crate ) mod private
}
}

crate::mod_interface!
{
crate::mod_interface! {
protected use ext;
protected use exts;
protected use change_ext;
protected use path_relative;
protected use rebase;
protected use path_common;
Expand Down
2 changes: 2 additions & 0 deletions module/core/proper_path_tools/tests/inc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[allow(unused_imports)]

use super::*;

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


#[ test ]
fn test_empty_ext()
{
let got = the_module::path::change_ext( "some.txt", "" );
let expected = "some";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_simple_change_extension()
{
let got = the_module::path::change_ext( "some.txt", "json" );
let expected = "some.json";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_path_with_non_empty_dir_name()
{
let got = the_module::path::change_ext( "/foo/bar/baz.asdf", "txt" );
let expected = "/foo/bar/baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_change_extension_of_hidden_file()
{
let got = the_module::path::change_ext( "/foo/bar/.baz", "sh" );
let expected = "/foo/bar/.baz.sh";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_change_extension_in_composite_file_name()
{
let got = the_module::path::change_ext( "/foo.coffee.md", "min" );
let expected = "/foo.coffee.min";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_add_extension_to_file_without_extension()
{
let got = the_module::path::change_ext( "/foo/bar/baz", "txt" );
let expected = "/foo/bar/baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_path_folder_contains_dot_file_without_extension()
{
let got = the_module::path::change_ext( "/foo/baz.bar/some.md", "txt" );
let expected = "/foo/baz.bar/some.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_relative_path_1()
{
let got = the_module::path::change_ext( "./foo/.baz", "txt" );
let expected = "./foo/.baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_relative_path_2()
{
let got = the_module::path::change_ext( "./.baz", "txt" );
let expected = "./.baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_relative_path_3()
{
let got = the_module::path::change_ext( ".baz", "txt" );
let expected = ".baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_relative_path_4()
{
let got = the_module::path::change_ext( "./baz", "txt" );
let expected = "./baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_relative_path_5()
{
let got = the_module::path::change_ext( "./foo/baz", "txt" );
let expected = "./foo/baz.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

#[ test ]
fn test_relative_path_6()
{
let got = the_module::path::change_ext( "./foo/", "txt" );
let expected = "./foo/.txt";
assert_eq!( got.unwrap().to_string_lossy(), expected );
}

0 comments on commit 5eb9939

Please sign in to comment.