aperture_cli/
fs.rs

1use std::io;
2use std::path::{Path, PathBuf};
3
4pub trait FileSystem {
5    /// Reads the entire contents of a file into a string.
6    ///
7    /// # Errors
8    ///
9    /// Returns an error if the file does not exist, cannot be read, or contains invalid UTF-8.
10    fn read_to_string(&self, path: &Path) -> io::Result<String>;
11
12    /// Writes a slice of bytes to a file, creating the file if it does not exist.
13    ///
14    /// # Errors
15    ///
16    /// Returns an error if the file cannot be written to or created.
17    fn write_all(&self, path: &Path, contents: &[u8]) -> io::Result<()>;
18
19    /// Creates a directory and all of its parent components if they are missing.
20    ///
21    /// # Errors
22    ///
23    /// Returns an error if the directory cannot be created.
24    fn create_dir_all(&self, path: &Path) -> io::Result<()>;
25
26    /// Removes a file from the filesystem.
27    ///
28    /// # Errors
29    ///
30    /// Returns an error if the file does not exist or cannot be removed.
31    fn remove_file(&self, path: &Path) -> io::Result<()>;
32
33    /// Removes a directory and all of its contents.
34    ///
35    /// # Errors
36    ///
37    /// Returns an error if the directory does not exist or cannot be removed.
38    fn remove_dir_all(&self, path: &Path) -> io::Result<()>;
39
40    /// Returns `true` if the path points to an existing entity.
41    fn exists(&self, path: &Path) -> bool;
42
43    /// Returns `true` if the path exists and is pointing at a directory.
44    fn is_dir(&self, path: &Path) -> bool;
45
46    /// Returns `true` if the path exists and is pointing at a regular file.
47    fn is_file(&self, path: &Path) -> bool;
48
49    /// Returns the canonical, absolute form of the path with all intermediate components normalized.
50    ///
51    /// # Errors
52    ///
53    /// Returns an error if the path does not exist or cannot be canonicalized.
54    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf>;
55
56    /// Returns a vector of all entries in a directory.
57    ///
58    /// # Errors
59    ///
60    /// Returns an error if the directory does not exist or cannot be read.
61    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>>;
62}
63
64pub struct OsFileSystem;
65
66impl FileSystem for OsFileSystem {
67    fn read_to_string(&self, path: &Path) -> io::Result<String> {
68        std::fs::read_to_string(path)
69    }
70
71    fn write_all(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
72        std::fs::write(path, contents)
73    }
74
75    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
76        std::fs::create_dir_all(path)
77    }
78
79    fn remove_file(&self, path: &Path) -> io::Result<()> {
80        std::fs::remove_file(path)
81    }
82
83    fn remove_dir_all(&self, path: &Path) -> io::Result<()> {
84        std::fs::remove_dir_all(path)
85    }
86
87    fn exists(&self, path: &Path) -> bool {
88        path.exists()
89    }
90
91    fn is_dir(&self, path: &Path) -> bool {
92        path.is_dir()
93    }
94
95    fn is_file(&self, path: &Path) -> bool {
96        path.is_file()
97    }
98
99    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
100        path.canonicalize()
101    }
102
103    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
104        Ok(std::fs::read_dir(path)?
105            .filter_map(std::result::Result::ok)
106            .map(|entry| entry.path())
107            .collect())
108    }
109}