luminol_filesystem/
erased.rs

1// Copyright (C) 2024 Melody Madeline Lyons
2//
3// This file is part of Luminol.
4//
5// Luminol is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Luminol is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Luminol.  If not, see <http://www.gnu.org/licenses/>.
17use crate::File;
18use crate::{DirEntry, Metadata, OpenFlags, Result};
19
20pub trait ErasedFilesystem: Send + Sync {
21    fn open_file(&self, path: &camino::Utf8Path, flags: OpenFlags) -> Result<Box<dyn File>>;
22
23    fn metadata(&self, path: &camino::Utf8Path) -> Result<Metadata>;
24
25    fn rename(&self, from: &camino::Utf8Path, to: &camino::Utf8Path) -> Result<()>;
26
27    fn exists(&self, path: &camino::Utf8Path) -> Result<bool>;
28
29    fn create_dir(&self, path: &camino::Utf8Path) -> Result<()>;
30
31    fn remove_dir(&self, path: &camino::Utf8Path) -> Result<()>;
32
33    fn remove_file(&self, path: &camino::Utf8Path) -> Result<()>;
34
35    fn remove(&self, path: &camino::Utf8Path) -> Result<()>;
36
37    fn read_dir(&self, path: &camino::Utf8Path) -> Result<Vec<DirEntry>>;
38
39    fn read(&self, path: &camino::Utf8Path) -> Result<Vec<u8>>;
40
41    fn read_to_string(&self, path: &camino::Utf8Path) -> Result<String>;
42
43    fn write(&self, path: &camino::Utf8Path, data: &[u8]) -> Result<()>;
44}
45
46impl<T> ErasedFilesystem for T
47where
48    T: crate::FileSystem,
49    T::File: 'static,
50{
51    fn open_file(&self, path: &camino::Utf8Path, flags: OpenFlags) -> Result<Box<dyn File>> {
52        let file = self.open_file(path, flags)?;
53        Ok(Box::new(file))
54    }
55
56    fn metadata(&self, path: &camino::Utf8Path) -> Result<Metadata> {
57        self.metadata(path)
58    }
59
60    fn rename(&self, from: &camino::Utf8Path, to: &camino::Utf8Path) -> Result<()> {
61        self.rename(from, to)
62    }
63
64    fn exists(&self, path: &camino::Utf8Path) -> Result<bool> {
65        self.exists(path)
66    }
67
68    fn create_dir(&self, path: &camino::Utf8Path) -> Result<()> {
69        self.create_dir(path)
70    }
71
72    fn remove_dir(&self, path: &camino::Utf8Path) -> Result<()> {
73        self.remove_dir(path)
74    }
75
76    fn remove_file(&self, path: &camino::Utf8Path) -> Result<()> {
77        self.remove_file(path)
78    }
79
80    fn remove(&self, path: &camino::Utf8Path) -> Result<()> {
81        self.remove(path)
82    }
83
84    fn read_dir(&self, path: &camino::Utf8Path) -> Result<Vec<DirEntry>> {
85        self.read_dir(path)
86    }
87
88    fn read(&self, path: &camino::Utf8Path) -> Result<Vec<u8>> {
89        self.read(path)
90    }
91
92    fn read_to_string(&self, path: &camino::Utf8Path) -> Result<String> {
93        self.read_to_string(path)
94    }
95
96    fn write(&self, path: &camino::Utf8Path, data: &[u8]) -> Result<()> {
97        self.write(path, data)
98    }
99}
100
101impl File for Box<dyn File> {
102    fn metadata(&self) -> std::io::Result<Metadata> {
103        self.as_ref().metadata()
104    }
105
106    fn set_len(&self, new_size: u64) -> std::io::Result<()> {
107        self.as_ref().set_len(new_size)
108    }
109}
110
111impl crate::FileSystem for dyn ErasedFilesystem {
112    type File = Box<dyn File>;
113
114    fn open_file(
115        &self,
116        path: impl AsRef<camino::Utf8Path>,
117        flags: OpenFlags,
118    ) -> Result<Self::File> {
119        self.open_file(path.as_ref(), flags)
120    }
121
122    fn metadata(&self, path: impl AsRef<camino::Utf8Path>) -> Result<Metadata> {
123        self.metadata(path.as_ref())
124    }
125
126    fn rename(
127        &self,
128        from: impl AsRef<camino::Utf8Path>,
129        to: impl AsRef<camino::Utf8Path>,
130    ) -> Result<()> {
131        self.rename(from.as_ref(), to.as_ref())
132    }
133
134    fn exists(&self, path: impl AsRef<camino::Utf8Path>) -> Result<bool> {
135        self.exists(path.as_ref())
136    }
137
138    fn create_dir(&self, path: impl AsRef<camino::Utf8Path>) -> Result<()> {
139        self.create_dir(path.as_ref())
140    }
141
142    fn remove_dir(&self, path: impl AsRef<camino::Utf8Path>) -> Result<()> {
143        self.remove_dir(path.as_ref())
144    }
145
146    fn remove_file(&self, path: impl AsRef<camino::Utf8Path>) -> Result<()> {
147        self.remove_file(path.as_ref())
148    }
149
150    fn read_dir(&self, path: impl AsRef<camino::Utf8Path>) -> Result<Vec<DirEntry>> {
151        self.read_dir(path.as_ref())
152    }
153
154    fn remove(&self, path: impl AsRef<camino::Utf8Path>) -> Result<()> {
155        self.remove(path.as_ref())
156    }
157
158    fn read(&self, path: impl AsRef<camino::Utf8Path>) -> Result<Vec<u8>> {
159        self.read(path.as_ref())
160    }
161
162    fn read_to_string(&self, path: impl AsRef<camino::Utf8Path>) -> Result<String> {
163        self.read_to_string(path.as_ref())
164    }
165
166    fn write(&self, path: impl AsRef<camino::Utf8Path>, data: impl AsRef<[u8]>) -> Result<()> {
167        self.write(path.as_ref(), data.as_ref())
168    }
169}