1use alloc::vec::Vec;
2use relative_path::{RelativePath, RelativePathBuf};
3
4use crate::FileSystem;
5
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub struct File {
9 pub path: RelativePathBuf,
11 pub contents: Vec<u8>,
13 pub new: bool,
16}
17
18impl File {
19 pub fn new(path: RelativePathBuf, contents: Vec<u8>, new: bool) -> Self {
21 Self {
22 path,
23 contents,
24 new,
25 }
26 }
27
28 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 pub fn save(self, fs: &mut FileSystem) -> anyhow::Result<()> {
39 fs.save(self)
40 }
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
45pub struct FilePath {
46 pub path: RelativePathBuf,
48 pub len: usize,
50}
51
52impl FilePath {
53 pub fn new(path: RelativePathBuf, len: usize) -> Self {
55 Self { path, len }
56 }
57
58 pub fn open(&self, fs: &mut FileSystem) -> anyhow::Result<File> {
60 fs.open(&self.path)
61 }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
66pub struct Dir {
67 pub path: RelativePathBuf,
69}
70
71impl Dir {
72 pub fn new(path: RelativePathBuf) -> Self {
74 Self { path }
75 }
76
77 pub fn ls(&self, fs: &mut FileSystem) -> anyhow::Result<Vec<DirOrFile>> {
79 fs.ls(&self.path)
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
85pub enum DirOrFile {
86 Dir(Dir),
88 File(FilePath),
90}
91
92impl DirOrFile {
93 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 pub fn as_dir(&self) -> Option<&Dir> {
103 match self {
104 DirOrFile::Dir(dir) => Some(dir),
105 _ => None,
106 }
107 }
108
109 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}