Skip to content

Commit 815e41e

Browse files
committed
pth : add from_paths for AbsolutePath
1 parent 0650d22 commit 815e41e

File tree

9 files changed

+120
-89
lines changed

9 files changed

+120
-89
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ path = "module/alias/file_tools"
367367
default-features = false
368368

369369
[workspace.dependencies.pth]
370-
version = "~0.17.0"
370+
version = "~0.18.0"
371371
path = "module/core/pth"
372372
default-features = false
373373

module/core/pth/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pth"
3-
version = "0.17.0"
3+
version = "0.18.0"
44
edition = "2021"
55
authors = [
66
"Kostiantyn Wandalen <[email protected]>",

module/core/pth/src/path.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,19 @@ mod private
314314
/// use pth::path;
315315
///
316316
/// let paths = vec![ PathBuf::from( "a/b/c" ), PathBuf::from( "/d/e" ), PathBuf::from( "f/g" ) ];
317-
/// let joined = path::join_paths( paths.iter().map( | p | p.as_path() ) );
317+
/// let joined = path::iter_join( paths.iter().map( | p | p.as_path() ) );
318318
/// assert_eq!( joined, std::path::PathBuf::from( "/d/e/f/g" ) );
319319
///
320320
/// let paths = vec![ PathBuf::from( "" ), PathBuf::from( "a/b" ), PathBuf::from( "" ), PathBuf::from( "c" ), PathBuf::from( "" ) ];
321-
/// let joined = path::join_paths( paths.iter().map( | p | p.as_path() ) );
321+
/// let joined = path::iter_join( paths.iter().map( | p | p.as_path() ) );
322322
/// assert_eq!( joined, std::path::PathBuf::from( PathBuf::from( "/a/b/c" ) ) );
323323
///
324324
/// ```
325325
// qqq : make macro paths_join!( ... )
326-
pub fn join_paths< I, P >( paths : I ) -> PathBuf
326+
pub fn iter_join< 'a ,I, P >( paths : I ) -> PathBuf
327327
where
328328
I : Iterator< Item = P >,
329-
P : AsPath,
329+
P : TryIntoCowPath< 'a >,
330330
{
331331
#[ cfg( feature = "no_std" ) ]
332332
extern crate alloc;
@@ -340,7 +340,8 @@ mod private
340340
for path in paths
341341
{
342342
// let mut path = path.to_string_lossy().replace( '\\', "/" );
343-
let path = path.as_path().to_string_lossy().replace( '\\', "/" );
343+
// qqq : xxx : avoid unwrap
344+
let path = path.try_into_cow_path().unwrap().to_string_lossy().replace( '\\', "/" );
344345
// qqq : xxx : avoid converting to String, keep it Path
345346

346347
// path = path.replace( ':', "" );
@@ -1052,17 +1053,20 @@ mod private
10521053
crate::mod_interface!
10531054
{
10541055

1055-
orphan use ext;
1056-
orphan use exts;
1057-
orphan use change_ext;
1058-
orphan use path_relative;
1059-
orphan use rebase;
1060-
orphan use path_common;
1061-
orphan use join_paths;
1062-
orphan use without_ext;
1063-
orphan use is_glob;
1064-
orphan use normalize;
1065-
orphan use canonicalize;
1056+
orphan use
1057+
{
1058+
ext,
1059+
exts,
1060+
change_ext,
1061+
path_relative,
1062+
rebase,
1063+
path_common,
1064+
iter_join,
1065+
without_ext,
1066+
is_glob,
1067+
normalize,
1068+
canonicalize,
1069+
};
10661070

10671071
#[ cfg( feature = "path_unique_folder_name" ) ]
10681072
orphan use unique_folder_name;

module/core/pth/src/path/absolute_path.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ mod private
6868
self.0
6969
}
7070

71-
/// Creates an `AbsolutePath` from an iterator over items that implement `AsPath`.
71+
/// Creates an `AbsolutePath` from an iterator over items that implement `TryIntoCowPath`.
7272
///
7373
/// This function joins all path segments into a single path and attempts to convert it
7474
/// into an `AbsolutePath`. The resulting path must be absolute.
@@ -81,14 +81,32 @@ mod private
8181
///
8282
/// * `Ok(AbsolutePath)` if the joined path is absolute.
8383
/// * `Err(io::Error)` if the joined path is not absolute.
84-
pub fn from_paths< I, P >( iter : I ) -> Result< Self, io::Error >
84+
pub fn from_iter< 'a, I, P >( iter : I ) -> Result< Self, io::Error >
8585
where
8686
I : Iterator< Item = P >,
87-
P : AsPath,
87+
P : TryIntoCowPath< 'a >,
8888
{
89-
let joined_path = join_paths( iter );
89+
let joined_path = iter_join( iter );
9090
AbsolutePath::try_from( joined_path )
9191
}
92+
93+
/// Joins path components into a `PathBuf`.
94+
///
95+
/// This function leverages the `PathJoined` trait to join multiple path components into a single `PathBuf`.
96+
///
97+
/// # Arguments
98+
///
99+
/// * `paths` - A tuple of path components implementing the `PathJoined` trait.
100+
///
101+
/// # Returns
102+
///
103+
/// * `Ok(PathBuf)` - The joined path as a `PathBuf`.
104+
/// * `Err(io::Error)` - An error if any component fails to convert.
105+
pub fn from_paths< Paths : PathJoined >( paths : Paths ) -> Result< Self, io::Error >
106+
{
107+
Self::try_from( paths.iter_join()? )
108+
}
109+
92110
}
93111

94112
impl fmt::Display for AbsolutePath

module/core/pth/src/path/joining.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod private
1717
/// * `Err(io::Error)` - An error if any component fails to convert.
1818
pub fn join< Paths : PathJoined >( paths : Paths ) -> Result< PathBuf, io::Error >
1919
{
20-
paths.join_paths()
20+
paths.iter_join()
2121
}
2222

2323
/// A trait for joining path components into a `PathBuf`.
@@ -33,16 +33,33 @@ mod private
3333
///
3434
/// * `Ok(PathBuf)` - The joined path as a `PathBuf`.
3535
/// * `Err(io::Error)` - An error if any component fails to convert.
36-
fn join_paths( self ) -> Result< PathBuf, io::Error >;
36+
fn iter_join( self ) -> Result< PathBuf, io::Error >;
3737
}
3838

39+
// // Implementation for an Iterator over items implementing TryIntoCowPath
40+
// impl< 'a, I, T > PathJoined for I
41+
// where
42+
// I : Iterator< Item = T >,
43+
// T : TryIntoCowPath< 'a >,
44+
// {
45+
// fn iter_join( self ) -> Result< PathBuf, io::Error >
46+
// {
47+
// let mut result = PathBuf::new();
48+
// for item in self
49+
// {
50+
// result.push( item.try_into_cow_path()?.as_ref() );
51+
// }
52+
// Ok( result )
53+
// }
54+
// }
55+
3956
// Implementation for a tuple of length 1
4057
impl< 'a, T1 > PathJoined for ( T1, )
4158
where
4259
T1 : TryIntoCowPath< 'a >,
4360
{
4461
#[ inline ]
45-
fn join_paths( self ) -> Result< PathBuf, io::Error >
62+
fn iter_join( self ) -> Result< PathBuf, io::Error >
4663
{
4764
let ( p1, ) = self;
4865
let mut result = PathBuf::new();
@@ -58,7 +75,7 @@ mod private
5875
T2 : TryIntoCowPath< 'a >,
5976
{
6077
#[ inline ]
61-
fn join_paths( self ) -> Result< PathBuf, io::Error >
78+
fn iter_join( self ) -> Result< PathBuf, io::Error >
6279
{
6380
let ( p1, p2 ) = self;
6481
let mut result = PathBuf::new();
@@ -76,7 +93,7 @@ mod private
7693
T3 : TryIntoCowPath< 'a >,
7794
{
7895
#[ inline ]
79-
fn join_paths( self ) -> Result< PathBuf, io::Error >
96+
fn iter_join( self ) -> Result< PathBuf, io::Error >
8097
{
8198
let ( p1, p2, p3 ) = self;
8299
let mut result = PathBuf::new();
@@ -96,7 +113,7 @@ mod private
96113
T4 : TryIntoCowPath< 'a >,
97114
{
98115
#[ inline ]
99-
fn join_paths( self ) -> Result< PathBuf, io::Error >
116+
fn iter_join( self ) -> Result< PathBuf, io::Error >
100117
{
101118
let ( p1, p2, p3, p4 ) = self;
102119
let mut result = PathBuf::new();
@@ -118,7 +135,7 @@ mod private
118135
T5 : TryIntoCowPath< 'a >,
119136
{
120137
#[ inline ]
121-
fn join_paths( self ) -> Result< PathBuf, io::Error >
138+
fn iter_join( self ) -> Result< PathBuf, io::Error >
122139
{
123140
let ( p1, p2, p3, p4, p5 ) = self;
124141
let mut result = PathBuf::new();
@@ -137,7 +154,7 @@ mod private
137154
T : TryIntoCowPath< 'a > + Clone,
138155
{
139156
#[ inline ]
140-
fn join_paths( self ) -> Result< PathBuf, io::Error >
157+
fn iter_join( self ) -> Result< PathBuf, io::Error >
141158
{
142159
let mut result = PathBuf::new();
143160
for item in self
@@ -154,7 +171,7 @@ mod private
154171
T : TryIntoCowPath< 'a > + Clone,
155172
{
156173
#[ inline ]
157-
fn join_paths( self ) -> Result< PathBuf, io::Error >
174+
fn iter_join( self ) -> Result< PathBuf, io::Error >
158175
{
159176
let mut result = PathBuf::new();
160177
for item in &self

module/core/pth/tests/inc/absolute_path_test/basic_test.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
use super::*;
22

3-
#[ cfg( not( feature="no_std" ) ) ]
43
use the_module::
54
{
65
AbsolutePath,
76
Path,
87
PathBuf,
98
};
109

11-
#[ cfg( feature="no_std" ) ]
12-
use crate::the_module::AbsolutePath;
13-
14-
// #[ cfg( feature = "path_utf8" ) ]
15-
// use the_module::Utf8PathBuf;
16-
1710
#[ test ]
1811
fn basic()
1912
{
@@ -74,7 +67,6 @@ fn test_join()
7467
assert_eq!( joined_path.to_string_lossy(), "/path/to/some/file.txt" );
7568
}
7669

77-
7870
#[test]
7971
fn test_relative_path_try_from_str()
8072
{

module/core/pth/tests/inc/absolute_path_test/from_paths_test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn test_from_paths_single_absolute_segment()
99
use std::convert::TryFrom;
1010

1111
let segments = vec![ "/single" ];
12-
let got = AbsolutePath::from_paths( segments.iter().map( |s| *s ) ).unwrap();
12+
let got = AbsolutePath::from_iter( segments.iter().map( |s| *s ) ).unwrap();
1313
let exp = AbsolutePath::try_from( "/single" ).unwrap();
1414

1515
assert_eq!( got, exp );
@@ -22,7 +22,7 @@ fn test_from_paths_multiple_segments()
2222
use std::convert::TryFrom;
2323

2424
let segments = vec![ "/path", "to", "file" ];
25-
let got = AbsolutePath::from_paths( segments.iter().map( |s| *s ) ).unwrap();
25+
let got = AbsolutePath::from_iter( segments.iter().map( |s| *s ) ).unwrap();
2626
let exp = AbsolutePath::try_from( "/path/to/file" ).unwrap();
2727

2828
assert_eq!( got, exp );
@@ -34,7 +34,7 @@ fn test_from_paths_empty_segments()
3434
use the_module::AbsolutePath;
3535

3636
let segments : Vec< &str > = vec![];
37-
let result = AbsolutePath::from_paths( segments.iter().map( | s | *s ) );
37+
let result = AbsolutePath::from_iter( segments.iter().map( | s | *s ) );
3838

3939
assert!( result.is_err(), "Expected an error for empty segments" );
4040
}
@@ -46,7 +46,7 @@ fn test_from_paths_with_dot_segments()
4646
use std::convert::TryFrom;
4747

4848
let segments = vec![ "/path", ".", "to", "file" ];
49-
let got = AbsolutePath::from_paths( segments.iter().map( |s| *s ) ).unwrap();
49+
let got = AbsolutePath::from_iter( segments.iter().map( |s| *s ) ).unwrap();
5050
let exp = AbsolutePath::try_from( "/path/to/file" ).unwrap();
5151

5252
assert_eq!( got, exp );
@@ -59,7 +59,7 @@ fn test_from_paths_with_dotdot_segments()
5959
use std::convert::TryFrom;
6060

6161
let segments = vec![ "/path", "to", "..", "file" ];
62-
let got = AbsolutePath::from_paths( segments.iter().map( |s| *s ) ).unwrap();
62+
let got = AbsolutePath::from_iter( segments.iter().map( |s| *s ) ).unwrap();
6363
let exp = AbsolutePath::try_from( "/path/file" ).unwrap();
6464

6565
assert_eq!( got, exp );
@@ -72,7 +72,7 @@ fn test_from_paths_with_trailing_slash()
7272
use std::convert::TryFrom;
7373

7474
let segments = vec![ "/path", "to", "file/" ];
75-
let got = AbsolutePath::from_paths( segments.iter().map( |s| *s ) ).unwrap();
75+
let got = AbsolutePath::from_iter( segments.iter().map( |s| *s ) ).unwrap();
7676
let exp = AbsolutePath::try_from( "/path/to/file/" ).unwrap();
7777

7878
assert_eq!( got, exp );
@@ -85,7 +85,7 @@ fn test_from_paths_with_mixed_slashes()
8585
use std::convert::TryFrom;
8686

8787
let segments = vec![ "/path\\to", "file" ];
88-
let got = AbsolutePath::from_paths( segments.iter().map( |s| *s ) ).unwrap();
88+
let got = AbsolutePath::from_iter( segments.iter().map( |s| *s ) ).unwrap();
8989
let exp = AbsolutePath::try_from( "/path/to/file" ).unwrap();
9090

9191
assert_eq!( got, exp );

0 commit comments

Comments
 (0)