Skip to main content

Crate gix_fs

Crate gix_fs 

Source
Expand description

A crate with file-system specific utilities.

§Examples

use std::path::{Path, PathBuf};

use gix_fs::{
    stack::{Delegate, ToNormalPathComponents},
    Stack,
};

let components = "src/lib.rs"
    .to_normal_path_components()
    .collect::<Result<Vec<_>, _>>()?;
assert_eq!(
    components
        .into_iter()
        .map(|component| component.to_string_lossy().into_owned())
        .collect::<Vec<_>>(),
    vec!["src", "lib.rs"]
);

#[derive(Default)]
struct Recorder {
    directories: Vec<PathBuf>,
    paths: Vec<PathBuf>,
}

impl Delegate for Recorder {
    fn push_directory(&mut self, stack: &Stack) -> std::io::Result<()> {
        self.directories.push(stack.current_relative().to_path_buf());
        Ok(())
    }

    fn push(&mut self, _is_last_component: bool, stack: &Stack) -> std::io::Result<()> {
        self.paths.push(stack.current_relative().to_path_buf());
        Ok(())
    }

    fn pop_directory(&mut self) {}
}

let capabilities = gix_fs::Capabilities::probe_dir(dir.path());
let mut stack = Stack::new(dir.path().to_path_buf());
let mut recorder = Recorder::default();
stack.make_relative_path_current("src/lib.rs", &mut recorder)?;

assert_eq!(stack.current_relative(), Path::new("src/lib.rs"));
assert_eq!(recorder.directories[0], Path::new(""));
assert_eq!(recorder.paths, vec![PathBuf::from("src"), PathBuf::from("src/lib.rs")]);
assert_eq!(gix_fs::current_dir(capabilities.precompose_unicode)?, std::env::current_dir()?);

Modules§

dir
io_err
Classifiers for IO-errors.
read_dir
stack
symlink

Structs§

Capabilities
Common knowledge about the worktree that is needed across most interactions with the work tree
FileSnapshot
A structure holding enough information to reload a value if its on-disk representation changes as determined by its modified time.
SharedFileSnapshotMut
Use this type for fields in structs that are to store the FileSnapshot, typically behind an OwnShared.
Stack
A stack of path components with the delegation of side-effects as the currently set path changes, component by component.

Functions§

current_dir
Like std::env::current_dir(), but it will precompose_unicode if that value is true, if the current directory is valid unicode and if there are decomposed unicode codepoints.
is_executable
Returns whether a file has the executable permission set.
read_dir
List all entries in path, similar to std::fs::read_dir(), and assure all available information adheres to the value of precompose_unicode.

Type Aliases§

SharedFileSnapshot
A snapshot of a resource which is up-to-date in the moment it is retrieved.