feattle_sync/
disk.rs

1use async_trait::async_trait;
2use feattle_core::persist::*;
3use feattle_core::BoxError;
4use serde::de::DeserializeOwned;
5use serde::Serialize;
6use std::io::ErrorKind;
7use std::path::PathBuf;
8use tokio::fs::{create_dir_all, File};
9use tokio::io::{AsyncReadExt, AsyncWriteExt};
10
11/// Persist the data in the local filesystem, under a given directory.
12///
13/// At every save action, if the directory does not exist, it will be created.
14///
15/// # Example
16/// ```
17/// use std::sync::Arc;
18/// use feattle_core::{feattles, Feattles};
19/// use feattle_sync::Disk;
20///
21/// feattles! {
22///     struct MyToggles {
23///         a: bool,
24///     }
25/// }
26///
27/// let my_toggles = MyToggles::new(Arc::new(Disk::new("some/local/directory")));
28/// ```
29#[derive(Debug, Clone)]
30pub struct Disk {
31    dir: PathBuf,
32}
33
34impl Disk {
35    pub fn new<P: Into<PathBuf>>(dir: P) -> Self {
36        let dir = dir.into();
37        Disk { dir }
38    }
39
40    async fn save<T: Serialize>(&self, name: &str, value: T) -> Result<(), BoxError> {
41        create_dir_all(&self.dir).await?;
42
43        let contents = serde_json::to_string(&value)?;
44        let mut file = File::create(self.dir.join(name)).await?;
45        file.write_all(contents.as_bytes())
46            .await
47            .map_err(Into::into)
48    }
49
50    async fn load<T: DeserializeOwned>(&self, name: &str) -> Result<Option<T>, BoxError> {
51        match File::open(self.dir.join(name)).await {
52            Err(err) if err.kind() == ErrorKind::NotFound => Ok(None),
53            Err(err) => Err(err.into()),
54            Ok(mut file) => {
55                let mut contents = String::new();
56                file.read_to_string(&mut contents).await?;
57                Ok(Some(serde_json::from_str(&contents)?))
58            }
59        }
60    }
61}
62
63#[async_trait]
64impl Persist for Disk {
65    async fn save_current(&self, value: &CurrentValues) -> Result<(), BoxError> {
66        self.save("current.json", value).await
67    }
68
69    async fn load_current(&self) -> Result<Option<CurrentValues>, BoxError> {
70        self.load("current.json").await
71    }
72
73    async fn save_history(&self, key: &str, value: &ValueHistory) -> Result<(), BoxError> {
74        self.save(&format!("history-{}.json", key), value).await
75    }
76
77    async fn load_history(&self, key: &str) -> Result<Option<ValueHistory>, BoxError> {
78        self.load(&format!("history-{}.json", key)).await
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use crate::tests::test_persistence;
86
87    #[tokio::test]
88    async fn disk() {
89        let dir = tempfile::TempDir::new().unwrap();
90        test_persistence(Disk::new(dir.path())).await;
91    }
92}