Skip to content

Commit f6e4bba

Browse files
committed
feat: add read_dir(precompose_unicode)
This way, the returned iterator can provide dir entries that can adjust the filepath automatically depending on the value of precompose_unicode
1 parent 24d081a commit f6e4bba

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

gix-fs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ doctest = false
1717
serde = ["dep:serde"]
1818

1919
[dependencies]
20-
gix-features = { version = "^0.37.1", path = "../gix-features" }
20+
gix-features = { version = "^0.37.1", path = "../gix-features", features = ["fs-read-dir"] }
2121
gix-utils = { version = "^0.1.8", path = "../gix-utils" }
2222
serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"] }
2323

gix-fs/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ pub use snapshot::{FileSnapshot, SharedFileSnapshot, SharedFileSnapshotMut};
3838
///
3939
pub mod symlink;
4040

41+
///
42+
pub mod read_dir;
43+
pub use read_dir::function::read_dir;
44+
4145
///
4246
pub mod dir;
4347

gix-fs/src/read_dir.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub use gix_features::fs::read_dir::DirEntry;
2+
3+
pub(crate) mod function {
4+
use std::path::Path;
5+
6+
/// List all entries in `path`, similar to [`std::fs::read_dir()`], and assure all available information
7+
/// adheres to the value of `precompose_unicode`.
8+
pub fn read_dir(
9+
path: &Path,
10+
precompose_unicode: bool,
11+
) -> std::io::Result<impl Iterator<Item = std::io::Result<super::DirEntry>>> {
12+
std::fs::read_dir(path)
13+
.map(move |it| it.map(move |res| res.map(|entry| super::DirEntry::new(entry, precompose_unicode))))
14+
}
15+
}

gix-fs/tests/fs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send +
22

33
mod capabilities;
44
mod dir;
5+
mod read_dir;
56
mod stack;

gix-fs/tests/read_dir/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[test]
2+
fn with_precomposed_unicode() -> crate::Result {
3+
let tmp = tempfile::tempdir()?;
4+
5+
let decomposed = "a\u{308}";
6+
7+
let root = tmp.path().join(decomposed);
8+
std::fs::create_dir(&root)?;
9+
std::fs::write(root.join(decomposed), [])?;
10+
11+
let precomposed = "ä";
12+
for entry in gix_fs::read_dir(&root, true)? {
13+
let entry = entry?;
14+
assert_eq!(
15+
entry.file_name().to_str().unwrap(),
16+
precomposed,
17+
"precomposition is applied"
18+
);
19+
assert_eq!(
20+
entry.path().parent().unwrap().file_name().unwrap(),
21+
precomposed,
22+
"precomposition is applied for the whole path"
23+
);
24+
}
25+
26+
for entry in gix_fs::read_dir(&root, false)? {
27+
let entry = entry?;
28+
assert_eq!(
29+
entry.file_name().to_str().unwrap(),
30+
decomposed,
31+
"by default, the path is unchanged"
32+
);
33+
assert_eq!(
34+
entry.path().parent().unwrap().file_name().unwrap(),
35+
decomposed,
36+
"the same is true for the parent path"
37+
);
38+
}
39+
40+
Ok(())
41+
}

0 commit comments

Comments
 (0)