Skip to main content

vfs_kit/
core.rs

1use std::path::Path;
2
3/// FsBackend defines a common API for all virtual file systems (vfs) in the crate.
4/// Some functions here use `path` as a parameter or return value.
5/// In all cases, `path` will refer to the virtual file system. The exception
6/// is the `root()` function, which returns the path in the host file system.
7pub trait FsBackend {
8    /// Returns root path refer to the host file system.
9    fn root(&self) -> &Path;
10
11    /// Returns current working directory related to the vfs root.
12    fn cwd(&self) -> &Path;
13
14    /// Changes the current working directory.
15    /// `path` can be in relative or absolute form, but in both cases it must exist in vfs.
16    /// Error returns in case the `path` is not exist.
17    fn cd<P: AsRef<Path>>(&mut self, path: P) -> Result<()>;
18
19    /// Returns true, if `path` exists.
20    fn exists<P: AsRef<Path>>(&self, path: P) -> bool;
21
22    /// Creates directory and all it parents, if necessary.
23    fn mkdir<P: AsRef<Path>>(&mut self, path: P) -> Result<()>;
24
25    /// Creates new file in vfs.
26    fn mkfile<P: AsRef<Path>>(&mut self, name: P, content: Option<&[u8]>) -> Result<()>;
27    
28    /// Reads the entire contents of a file into a byte vector.
29    fn read<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>>;
30
31    /// Writes bytes to an existing file, replacing its entire contents.
32    fn write<P: AsRef<Path>>(&self, path: P, content: &[u8]) -> Result<()>;
33
34    /// Appends bytes to the end of an existing file, preserving its old contents.
35    fn append<P: AsRef<Path>>(&self, path: P, content: &[u8]) -> Result<()>;
36
37    /// Removes a file or directory at the specified path.
38    fn rm<P: AsRef<Path>>(&mut self, path: P) -> Result<()>;
39
40    /// Removes all artifacts (dirs and files) in vfs, but preserve its root.
41    fn cleanup(&mut self) -> bool;
42}
43
44pub type Result<T> = std::result::Result<T, anyhow::Error>;