1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use async_trait::async_trait;
use std::path::Path;
use tokio::fs;

#[async_trait]
pub trait SysFS {
    fn get_path(&self) -> &Path;

    /// Reads the content of a file in the SysFS.
    async fn read_file(&self, file: &str) -> Option<String> {
        match fs::read_to_string(self.get_path().join(file)).await {
            Ok(contents) => Some(contents.trim().to_owned()),
            Err(_) => None,
        }
    }

    /// Write to a file in the SysFS.
    async fn write_file<C: AsRef<[u8]> + Send>(
        &self,
        file: &str,
        contents: C,
    ) -> Result<(), std::io::Error> {
        fs::write(self.get_path().join(file), contents).await
    }
}