buf_fs/
types.rs

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