1use crate::{
3 error::{Error, ErrorContext},
4 Result,
5};
6use std::{fmt::Debug, fs, path::Path, str::FromStr};
7
8pub trait SysFS {
10 fn get_path(&self) -> &Path;
12
13 fn read_file(&self, file: impl AsRef<Path> + Debug) -> Result<String> {
15 let path = file.as_ref();
16 Ok(fs::read_to_string(self.get_path().join(path))
17 .with_context(|| format!("Could not read file {file:?}"))?
18 .replace(char::from(0), "") .trim()
20 .to_owned())
21 }
22
23 fn read_file_parsed<T: FromStr<Err = E>, E: ToString>(&self, file: &str) -> Result<T> {
25 fs::read_to_string(self.get_path().join(file))
26 .with_context(|| format!("Could not read file {file}"))?
27 .trim()
28 .parse()
29 .map_err(|err: E| Error::basic_parse_error(err.to_string()))
30 }
31
32 fn write_file<C: AsRef<[u8]> + Send>(&self, file: &str, contents: C) -> Result<()> {
34 Ok(fs::write(self.get_path().join(file), contents)?)
35 }
36}