1use crate::{async_trait, OpenOptions, VfsError, VfsResult};
2use futures_lite::{AsyncRead, AsyncSeek, AsyncWrite};
3use std::{io::ErrorKind, pin::Pin};
4
5pub trait VMetadata: Sync + Send {
6 fn path(&self) -> &str;
7 fn is_dir(&self) -> bool;
8 fn is_file(&self) -> bool;
9 fn len(&self) -> u64;
10 fn mtime(&self) -> u64;
11}
12
13pub trait VFile: AsyncRead + AsyncWrite + AsyncSeek {}
14impl<T> VFile for T where T: AsyncRead + AsyncWrite + AsyncSeek {}
15
16#[async_trait]
17pub trait Vfs: Sync + Send {
18 async fn exists(&self, path: &str) -> VfsResult<bool> {
20 match self.metadata(path).await {
21 Ok(_) => Ok(true),
22 Err(VfsError::IoError(err)) => match &err.kind() {
23 ErrorKind::NotFound => Ok(false),
24 _ => Err(VfsError::IoError(err)),
25 },
26 Err(e) => Err(e),
27 }
28 }
29
30 async fn ls(
31 &self,
32 path: &str,
33 skip_token: Option<String>,
34 ) -> VfsResult<(Vec<Box<dyn VMetadata>>, Option<String>)>;
35
36 async fn cp(&self, from: &str, to: &str) -> VfsResult<()>;
37 async fn metadata(&self, path: &str) -> VfsResult<Box<dyn VMetadata>>;
38 async fn mkdir(&self, path: &str) -> VfsResult<()>;
39 async fn mkdir_all(&self, path: &str) -> VfsResult<()>;
40 async fn mv(&self, from: &str, to: &str) -> VfsResult<()>;
41 async fn open(&self, path: &str, options: OpenOptions)
42 -> VfsResult<Pin<Box<dyn VFile + Send>>>;
43 async fn rm(&self, path: &str) -> VfsResult<()>;
44}