vfs/async_vfs/
filesystem.rs

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