1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::path::Path;

use async_trait::async_trait;

pub mod in_memory;
pub mod local;

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct FileSystemError(#[from] anyhow::Error);

impl From<std::io::Error> for FileSystemError {
    fn from(error: std::io::Error) -> Self {
        Self(error.into())
    }
}

pub type FileSystemResult<T> = Result<T, FileSystemError>;

#[async_trait]
pub trait FileSystem {
    async fn create_dir<P>(&self, path: P) -> FileSystemResult<()>
    where
        P: AsRef<Path> + Send;

    async fn create_dir_all<P>(&self, path: P) -> FileSystemResult<()>
    where
        P: AsRef<Path> + Send;

    async fn read<P>(&self, path: P) -> FileSystemResult<Vec<u8>>
    where
        P: AsRef<Path> + Send;

    async fn read_to_string<P>(&self, path: P) -> FileSystemResult<String>
    where
        P: AsRef<Path> + Send;

    async fn write<P, C>(&self, path: P, contents: C) -> FileSystemResult<()>
    where
        P: AsRef<Path> + Send,
        C: AsRef<[u8]> + Send;

    async fn append<P, C>(&self, path: P, contents: C) -> FileSystemResult<()>
    where
        P: AsRef<Path> + Send,
        C: AsRef<[u8]> + Send;

    async fn copy<P1, P2>(&self, from: P1, to: P2) -> FileSystemResult<()>
    where
        P1: AsRef<Path> + Send,
        P2: AsRef<Path> + Send;

    async fn set_mode<P>(&self, path: P, perm: u32) -> FileSystemResult<()>
    where
        P: AsRef<Path> + Send;

    async fn exists<P>(&self, path: P) -> bool
    where
        P: AsRef<Path> + Send;
}