Skip to content

Commit

Permalink
Merge branch 'alpha' into ext
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen authored May 3, 2024
2 parents 56e935e + 07a22a0 commit 19e2101
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 24 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,3 @@ default-features = true
version = "~0.3.0"
path = "module/test/c"
default-features = true


[patch.crates-io]
pathfinder_geometry = { git = "https://github.com/servo/pathfinder.git" }
pathfinder_simd = { git = "https://github.com/servo/pathfinder.git" }
79 changes: 79 additions & 0 deletions module/core/proper_path_tools/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,84 @@ pub( crate ) mod private
vec![]
}





/// Extracts the parent directory and file stem (without extension) from the given path.
///
/// This function takes a path and returns an Option containing the modified path without the extension.
/// If the input path is empty or if it doesn't contain a file stem, it returns None.
///
/// # Arguments
///
/// * `path` - An object that can be converted into a Path reference, representing the file path.
///
/// # Returns
///
/// An Option containing the modified path without the extension, or None if the input path is empty or lacks a file stem.
///
/// # Examples
///
/// ```
/// use std::path::PathBuf;
/// use proper_path_tools::path::without_ext;
///
/// let path = "/path/to/file.txt";
/// let modified_path = without_ext(path);
/// assert_eq!(modified_path, Some(PathBuf::from("/path/to/file")));
/// ```
///
/// ```
/// use std::path::PathBuf;
/// use proper_path_tools::path::without_ext;
///
/// let empty_path = "";
/// let modified_path = without_ext(empty_path);
/// assert_eq!(modified_path, None);
/// ```
///
pub fn without_ext( path : impl AsRef< std::path::Path > ) -> Option< std::path::PathBuf >
{
use std::path::Path;
use std::path::PathBuf;

if path.as_ref().to_string_lossy().is_empty()
{
return None;
}

let path_buf = Path::new( path.as_ref() );

let parent = match path_buf.parent()
{
Some( parent ) => parent,
None => return None,
};
let file_stem = match path_buf.file_stem()
{
Some( name ) =>
{
let ends = format!( "{}/", name.to_string_lossy() );
if path.as_ref().to_string_lossy().ends_with( &ends )
{
ends
}
else
{
String::from( name.to_string_lossy() )
}

}
None => return None,
};

let mut full_path = parent.to_path_buf();
full_path.push( file_stem );

Some( PathBuf::from( full_path.to_string_lossy().replace( "\\", "/" ) ) )
}

/// 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 @@ -796,6 +874,7 @@ crate::mod_interface!
protected use path_relative;
protected use rebase;
protected use path_common;
protected use without_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 @@ -6,6 +6,7 @@ mod path_is_glob;
mod absolute_path;
mod path_exts;
mod path_ext;
mod without_ext;
mod path_common;
mod rebase_path;
mod path_relative;
Expand Down
114 changes: 114 additions & 0 deletions module/core/proper_path_tools/tests/inc/without_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#[ allow( unused_imports ) ]
use super::*;

#[ test ]
fn empty_path()
{
let path = "";
let expected = None;
assert_eq!( the_module::path::without_ext( path ), expected );
}

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

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

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

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

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

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

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

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

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

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

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

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

#[ test ]
fn relative_path_8()
{
let path = "baz.a.b";
let expected = "baz.a";
assert_eq!( the_module::path::without_ext( path ).unwrap().to_string_lossy(), expected );
}
3 changes: 3 additions & 0 deletions module/move/optimization_tools/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[patch.crates-io]
pathfinder_geometry = { git = "https://github.com/servo/pathfinder.git" }
pathfinder_simd = { git = "https://github.com/servo/pathfinder.git" }
4 changes: 2 additions & 2 deletions module/move/optimization_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ full = [
enabled = []
rapidity_6 = [] # to enable slow tests
static_plot = []
dynamic_plot = [ "static_plot", "plotters-backend", "piston_window" ]
lp_parse = [ "exmex" ]
dynamic_plot = [ "static_plot", "dep:plotters-backend", "dep:piston_window" ]
lp_parse = [ "dep:exmex" ]

[dependencies]
derive_tools = { workspace = true, features = [ "derive_more", "full", "strum" ] }
Expand Down
17 changes: 0 additions & 17 deletions module/move/optimization_tools/src/problems/sudoku/sudoku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,11 @@ use iter_tools::Itertools;
trait BoardExt
{
/// Validate that each bloack has at least one non-fixed cell.
fn validate_each_block_has_non_fixed_cell( &self ) -> bool;
fn validate_block_has_non_fixed_cells( &self, block : BlockIndex ) -> bool;
}

impl BoardExt for Board
{
fn validate_each_block_has_non_fixed_cell( &self ) -> bool
{
for block in self.blocks()
{
let fixed = self.block_cells( block )
.map( | cell | self.cell( cell ) )
.fold( 0, | acc, e | if e == 0.into() { acc + 1 } else { acc } )
;
if fixed <= 1 || fixed >= 10
{
return false;
}
}
true
}

fn validate_block_has_non_fixed_cells( &self, block : BlockIndex ) -> bool
{
let fixed = self.block_cells( block )
Expand Down
1 change: 1 addition & 0 deletions module/move/optimization_tools/tests/ga_optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn crossover()
/// cargo test solve_with_ga --release --features rapidity_6
///
#[ cfg( feature = "rapidity_6" ) ]
#[ ignore ]
#[ test ]
fn solve_with_ga()
{
Expand Down
3 changes: 3 additions & 0 deletions module/move/optimization_tools/tests/optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn initial_temperature()
/// cargo test solve_with_sa --release --features rapidity_6
///
#[ cfg( feature = "rapidity_6" ) ]
#[ ignore ]
#[ test ]
fn solve_with_sa()
{
Expand Down Expand Up @@ -106,6 +107,7 @@ fn solve_with_sa()
/// cargo test solve_empty_full_block --release --features rapidity_6
///
#[ cfg( feature = "rapidity_6" ) ]
#[ ignore ]
#[ test ]
fn solve_empty_full_block()
{
Expand Down Expand Up @@ -182,6 +184,7 @@ fn solve_empty_full_block()
/// cargo test time_measure --release --features rapidity_6
///
#[ cfg( feature = "rapidity_6" ) ]
#[ ignore ]
#[ test ]
fn time_measure()
{
Expand Down
1 change: 1 addition & 0 deletions module/move/optimization_tools/tests/traveling_salesman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn tsp_person_mutate()
a_id!( person.route.len() - 1, unique.len() );
}

#[ ignore ]
#[ test ]
fn find_route()
{
Expand Down

0 comments on commit 19e2101

Please sign in to comment.