1use std::path::{Path, PathBuf};
2
3use crate::FileSystem;
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
7pub struct File {
8 pub path: PathBuf,
10 pub contents: Vec<u8>,
12 pub new: bool,
15}
16
17impl File {
18 pub fn new(path: PathBuf, contents: Vec<u8>, new: bool) -> Self {
20 Self {
21 path,
22 contents,
23 new,
24 }
25 }
26
27 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 pub fn save(self, fs: &mut FileSystem) -> anyhow::Result<()> {
38 fs.save(self)
39 }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
44pub struct FilePath {
45 pub path: PathBuf,
47 pub len: usize,
49}
50
51impl FilePath {
52 pub fn new(path: PathBuf, len: usize) -> Self {
54 Self { path, len }
55 }
56
57 pub fn open(&self, fs: &mut FileSystem) -> anyhow::Result<File> {
59 fs.open(&self.path)
60 }
61}
62
63#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
65pub struct Dir {
66 pub path: PathBuf,
68}
69
70impl Dir {
71 pub fn new(path: PathBuf) -> Self {
73 Self { path }
74 }
75
76 pub fn ls(&self, fs: &mut FileSystem) -> anyhow::Result<Vec<DirOrFile>> {
78 fs.ls(&self.path)
79 }
80}
81
82#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
84pub enum DirOrFile {
85 Dir(Dir),
87 File(FilePath),
89}
90
91impl DirOrFile {
92 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 pub fn as_dir(&self) -> Option<&Dir> {
102 match self {
103 DirOrFile::Dir(dir) => Some(dir),
104 _ => None,
105 }
106 }
107
108 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}