buf_fs/
types.rs

1use alloc::vec::Vec;
2use relative_path::{RelativePath, RelativePathBuf};
3
4use crate::FileSystem;
5
6/// A file representation.
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub struct File {
9    /// The path of the file in the fs.
10    pub path: RelativePathBuf,
11    /// The byte contents of the file.
12    pub contents: Vec<u8>,
13    /// Flag indicating whether the file is newly created and hasn't existed before its current
14    /// state for saving purposes.
15    pub new: bool,
16}
17
18impl File {
19    /// Creates a new file representation.
20    pub fn new(path: RelativePathBuf, contents: Vec<u8>, new: bool) -> Self {
21        Self {
22            path,
23            contents,
24            new,
25        }
26    }
27
28    /// Updates the byte contents of the file.
29    pub fn update<F>(mut self, f: F) -> Self
30    where
31        F: FnOnce(&mut Vec<u8>),
32    {
33        f(&mut self.contents);
34        self
35    }
36
37    /// Saves the file to the fs.
38    pub fn save(self, fs: &mut FileSystem) -> anyhow::Result<()> {
39        fs.save(self)
40    }
41}
42
43/// A file path, without its contents.
44#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
45pub struct FilePath {
46    /// The path of the file in the fs.
47    pub path: RelativePathBuf,
48    /// The length of the file in the fs.
49    pub len: usize,
50}
51
52impl FilePath {
53    /// Creates a new file path representation.
54    pub fn new(path: RelativePathBuf, len: usize) -> Self {
55        Self { path, len }
56    }
57
58    /// Opens the file with its contents from the fs.
59    pub fn open(&self, fs: &mut FileSystem) -> anyhow::Result<File> {
60        fs.open(&self.path)
61    }
62}
63
64/// A directory representation
65#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
66pub struct Dir {
67    /// The path of the directory in the fs.
68    pub path: RelativePathBuf,
69}
70
71impl Dir {
72    /// Creates a new directory path representation.
73    pub fn new(path: RelativePathBuf) -> Self {
74        Self { path }
75    }
76
77    /// Lists the contents of the directory in the fs.
78    pub fn ls(&self, fs: &mut FileSystem) -> anyhow::Result<Vec<DirOrFile>> {
79        fs.ls(&self.path)
80    }
81}
82
83/// The contents of a directory, being a directory or a file.
84#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
85pub enum DirOrFile {
86    /// A fs directory variant.
87    Dir(Dir),
88    /// A fs file variant.
89    File(FilePath),
90}
91
92impl DirOrFile {
93    /// Returns the underlying path of the entry.
94    pub fn path(&self) -> &RelativePath {
95        match self {
96            DirOrFile::Dir(p) => p.path.as_relative_path(),
97            DirOrFile::File(p) => p.path.as_relative_path(),
98        }
99    }
100
101    /// Returns the directory, if the variant is in accordance.
102    pub fn as_dir(&self) -> Option<&Dir> {
103        match self {
104            DirOrFile::Dir(dir) => Some(dir),
105            _ => None,
106        }
107    }
108
109    /// Returns the file path, if the variant is in accordance.
110    pub fn as_file(&self) -> Option<&FilePath> {
111        match self {
112            DirOrFile::File(file) => Some(file),
113            _ => None,
114        }
115    }
116}
117
118impl From<Dir> for DirOrFile {
119    fn from(dir: Dir) -> Self {
120        Self::Dir(dir)
121    }
122}
123
124impl From<FilePath> for DirOrFile {
125    fn from(file: FilePath) -> Self {
126        Self::File(file)
127    }
128}