Skip to content

Commit

Permalink
feat: add read_dir(precompose_unicode)
Browse files Browse the repository at this point in the history
This way, the returned iterator can provide dir entries that can
adjust the filepath automatically depending on the value of
precompose_unicode
  • Loading branch information
Byron committed Jan 17, 2024
1 parent 417b1fe commit 26e26a1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gix-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ doctest = false
serde = ["dep:serde"]

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

Expand Down
4 changes: 4 additions & 0 deletions gix-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub use snapshot::{FileSnapshot, SharedFileSnapshot, SharedFileSnapshotMut};
///
pub mod symlink;

///
pub mod read_dir;
pub use read_dir::function::read_dir;

///
pub mod dir;

Expand Down
15 changes: 15 additions & 0 deletions gix-fs/src/read_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub use gix_features::fs::read_dir::DirEntry;

pub(crate) mod function {
use std::path::Path;

/// List all entries in `path`, similar to [`std::fs::read_dir()`], and assure all available information
/// adheres to the value of `precompose_unicode`.
pub fn read_dir(
path: &Path,
precompose_unicode: bool,
) -> std::io::Result<impl Iterator<Item = std::io::Result<super::DirEntry>>> {
std::fs::read_dir(path)
.map(move |it| it.map(move |res| res.map(|entry| super::DirEntry::new(entry, precompose_unicode))))
}
}
1 change: 1 addition & 0 deletions gix-fs/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send +

mod capabilities;
mod dir;
mod read_dir;
mod stack;
41 changes: 41 additions & 0 deletions gix-fs/tests/read_dir/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[test]
fn with_precomposed_unicode() -> crate::Result {
let tmp = tempfile::tempdir()?;

let decomposed = "a\u{308}";

let root = tmp.path().join(decomposed);
std::fs::create_dir(&root)?;
std::fs::write(root.join(decomposed), [])?;

let precomposed = "ä";
for entry in gix_fs::read_dir(&root, true)? {
let entry = entry?;
assert_eq!(
entry.file_name().to_str().unwrap(),
precomposed,
"precomposition is applied"
);
assert_eq!(
entry.path().parent().unwrap().file_name().unwrap(),
precomposed,
"precomposition is applied for the whole path"
);
}

for entry in gix_fs::read_dir(&root, false)? {
let entry = entry?;
assert_eq!(
entry.file_name().to_str().unwrap(),
decomposed,
"by default, the path is unchanged"
);
assert_eq!(
entry.path().parent().unwrap().file_name().unwrap(),
decomposed,
"the same is true for the parent path"
);
}

Ok(())
}

0 comments on commit 26e26a1

Please sign in to comment.