Skip to main content

cmx_core/gateway/
filesystem.rs

1use anyhow::Result;
2use std::path::{Path, PathBuf};
3
4/// A simplified directory entry for the `Filesystem` trait boundary.
5///
6/// Uses a custom type (not `std::fs::DirEntry`) so that test fakes can
7/// construct instances without touching the real filesystem.
8pub struct DirEntry {
9    pub path: PathBuf,
10    pub file_name: String,
11    pub is_dir: bool,
12}
13
14/// Abstraction over filesystem I/O operations used by cmx.
15///
16/// Every function that reads or writes files accepts `&dyn Filesystem` rather
17/// than calling `std::fs` directly.  Production code uses `RealFilesystem`;
18/// tests use the in-memory fake defined in [`super::fakes`].
19pub trait Filesystem {
20    fn exists(&self, path: &Path) -> bool;
21    fn is_dir(&self, path: &Path) -> bool;
22    fn is_file(&self, path: &Path) -> bool;
23    fn read_to_string(&self, path: &Path) -> Result<String>;
24    fn read(&self, path: &Path) -> Result<Vec<u8>>;
25    fn write(&self, path: &Path, contents: &str) -> Result<()>;
26    fn write_bytes(&self, path: &Path, contents: &[u8]) -> Result<()>;
27    fn create_dir_all(&self, path: &Path) -> Result<()>;
28    fn copy_file(&self, src: &Path, dest: &Path) -> Result<()>;
29    /// Atomically rename `from` to `to`, replacing `to` if it already exists.
30    fn rename(&self, from: &Path, to: &Path) -> Result<()>;
31    fn remove_file(&self, path: &Path) -> Result<()>;
32    fn remove_dir_all(&self, path: &Path) -> Result<()>;
33    fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>>;
34    fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
35}