Skip to main content

fakecloud_persistence/
config.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum StorageMode {
8    #[default]
9    Memory,
10    Persistent,
11}
12
13#[derive(Clone, Debug)]
14pub struct PersistenceConfig {
15    pub mode: StorageMode,
16    pub data_path: Option<PathBuf>,
17    pub s3_cache_bytes: u64,
18}
19
20impl PersistenceConfig {
21    pub fn memory() -> Self {
22        Self {
23            mode: StorageMode::Memory,
24            data_path: None,
25            s3_cache_bytes: 0,
26        }
27    }
28
29    pub fn persistent(path: PathBuf, cache_bytes: u64) -> Self {
30        Self {
31            mode: StorageMode::Persistent,
32            data_path: Some(path),
33            s3_cache_bytes: cache_bytes,
34        }
35    }
36
37    pub fn validate(&self) -> Result<(), String> {
38        match self.mode {
39            StorageMode::Persistent => {
40                if self.data_path.is_none() {
41                    return Err(
42                        "--storage-mode=persistent requires --data-path to be set".to_string()
43                    );
44                }
45            }
46            StorageMode::Memory => {
47                if self.data_path.is_some() {
48                    return Err(
49                        "--data-path is only valid with --storage-mode=persistent".to_string()
50                    );
51                }
52            }
53        }
54        Ok(())
55    }
56}