Skip to main content

bias_vfs/
filesystem.rs

1//! The filesystem trait definitions needed to implement new virtual filesystems
2
3use crate::error::VfsErrorKind;
4use crate::{SeekAndRead, SeekAndWrite, VfsError, VfsMetadata, VfsPath, VfsResult};
5use std::fmt::Debug;
6use std::path::PathBuf;
7use std::time::SystemTime;
8
9/// File system implementations must implement this trait
10/// All path parameters are absolute, starting with '/', except for the root directory
11/// which is simply the empty string (i.e. "")
12/// The character '/' is used to delimit directories on all platforms.
13/// Path components may be any UTF-8 string, except "/", "." and ".."
14///
15/// Please use the test_macros [test_macros::test_vfs!] and [test_macros::test_vfs_readonly!]
16pub trait FileSystem: Debug + Sync + Send + 'static {
17    /// Iterates over all direct children of this directory path
18    /// NOTE: the returned String items denote the local bare filenames, i.e. they should not contain "/" anywhere
19    fn read_dir(&self, path: &str) -> VfsResult<Box<dyn Iterator<Item = String> + Send>>;
20    /// Creates the directory at this path
21    ///
22    /// Note that the parent directory must already exist.
23    fn create_dir(&self, path: &str) -> VfsResult<()>;
24    /// Opens the file at this path for reading
25    fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send>>;
26    /// Creates a file at this path for writing
27    fn create_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndWrite + Send>>;
28    /// Opens the file at this path for appending
29    fn append_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndWrite + Send>>;
30    /// Returns the file metadata for the file at this path
31    fn metadata(&self, path: &str) -> VfsResult<VfsMetadata>;
32    /// Sets the files creation timestamp, if the implementation supports it
33    fn set_creation_time(&self, _path: &str, _time: SystemTime) -> VfsResult<()> {
34        Err(VfsError::from(VfsErrorKind::NotSupported))
35    }
36    /// Sets the files modification timestamp, if the implementation supports it
37    fn set_modification_time(&self, _path: &str, _time: SystemTime) -> VfsResult<()> {
38        Err(VfsError::from(VfsErrorKind::NotSupported))
39    }
40    /// Sets the files access timestamp, if the implementation supports it
41    fn set_access_time(&self, _path: &str, _time: SystemTime) -> VfsResult<()> {
42        Err(VfsError::from(VfsErrorKind::NotSupported))
43    }
44    /// Returns true if a file or directory at path exists, false otherwise
45    fn exists(&self, path: &str) -> VfsResult<bool>;
46    /// Removes the file at this path
47    fn remove_file(&self, path: &str) -> VfsResult<()>;
48    /// Removes the directory at this path
49    fn remove_dir(&self, path: &str) -> VfsResult<()>;
50    /// Copies the src path to the destination path within the same filesystem (optional)
51    fn copy_file(&self, _src: &str, _dest: &str) -> VfsResult<()> {
52        Err(VfsErrorKind::NotSupported.into())
53    }
54    /// Moves the src path to the destination path within the same filesystem (optional)
55    fn move_file(&self, _src: &str, _dest: &str) -> VfsResult<()> {
56        Err(VfsErrorKind::NotSupported.into())
57    }
58    /// Moves the src directory to the destination path within the same filesystem (optional)
59    fn move_dir(&self, _src: &str, _dest: &str) -> VfsResult<()> {
60        Err(VfsErrorKind::NotSupported.into())
61    }
62    /// Returns the real filesystem path for a given virtual path, if available
63    fn real_path(&self, _path: &str) -> VfsResult<PathBuf> {
64        Err(VfsErrorKind::NotSupported.into())
65    }
66}
67
68impl<T: FileSystem> From<T> for VfsPath {
69    fn from(filesystem: T) -> Self {
70        VfsPath::new(filesystem)
71    }
72}