fakecloud_persistence/
config.rs1use 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}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn memory_config_has_defaults() {
64 let cfg = PersistenceConfig::memory();
65 assert_eq!(cfg.mode, StorageMode::Memory);
66 assert!(cfg.data_path.is_none());
67 assert_eq!(cfg.s3_cache_bytes, 0);
68 }
69
70 #[test]
71 fn persistent_config_stores_path() {
72 let cfg = PersistenceConfig::persistent(PathBuf::from("/tmp/x"), 1024);
73 assert_eq!(cfg.mode, StorageMode::Persistent);
74 assert_eq!(
75 cfg.data_path.as_deref(),
76 Some(std::path::Path::new("/tmp/x"))
77 );
78 assert_eq!(cfg.s3_cache_bytes, 1024);
79 }
80
81 #[test]
82 fn validate_memory_ok() {
83 assert!(PersistenceConfig::memory().validate().is_ok());
84 }
85
86 #[test]
87 fn validate_persistent_requires_path() {
88 let cfg = PersistenceConfig {
89 mode: StorageMode::Persistent,
90 data_path: None,
91 s3_cache_bytes: 0,
92 };
93 assert!(cfg.validate().is_err());
94 }
95
96 #[test]
97 fn validate_memory_with_data_path_errors() {
98 let cfg = PersistenceConfig {
99 mode: StorageMode::Memory,
100 data_path: Some(PathBuf::from("/tmp")),
101 s3_cache_bytes: 0,
102 };
103 assert!(cfg.validate().is_err());
104 }
105
106 #[test]
107 fn validate_persistent_with_path_ok() {
108 assert!(PersistenceConfig::persistent(PathBuf::from("/tmp"), 0)
109 .validate()
110 .is_ok());
111 }
112}