Trait grass::Fs

source ·
pub trait Fs: Debug {
    // Required methods
    fn is_dir(&self, path: &Path) -> bool;
    fn is_file(&self, path: &Path) -> bool;
    fn read(&self, path: &Path) -> Result<Vec<u8>, Error>;

    // Provided method
    fn canonicalize(&self, path: &Path) -> Result<PathBuf, Error> { ... }
}
Expand description

A trait to allow replacing the file system lookup mechanisms.

As it stands, this is imperfect: it’s still using the types and some operations from std::path, which constrain it to the target platform’s norms. This could be ameliorated by the use of associated types for Path and PathBuf, and putting all remaining methods on this trait (is_absolute, parent, join, &c.); but that would infect too many other APIs to be desirable, so we live with it as it is—which is also acceptable, because the motivating example use case is mostly using this as an optimisation over the real platform underneath.

Required Methods§

source

fn is_dir(&self, path: &Path) -> bool

Returns true if the path exists on disk and is pointing at a directory.

source

fn is_file(&self, path: &Path) -> bool

Returns true if the path exists on disk and is pointing at a regular file.

source

fn read(&self, path: &Path) -> Result<Vec<u8>, Error>

Read the entire contents of a file into a bytes vector.

Provided Methods§

source

fn canonicalize(&self, path: &Path) -> Result<PathBuf, Error>

Canonicalize a file path

Implementors§