Skip to main content

remendo/config/
cache.rs

1//! Cache behaviour configuration.
2
3use serde::Deserialize;
4use std::path::PathBuf;
5
6/// Default cache TTL in seconds.
7const fn default_ttl() -> u64 {
8    300
9}
10
11/// Settings controlling local API response caching.
12#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
13#[serde(default)]
14pub struct CacheConfig {
15    /// Override for the cache directory (default: `$XDG_CACHE_HOME/remendo/`).
16    pub dir: Option<PathBuf>,
17    /// Time-to-live for cached responses, in seconds.
18    #[serde(default = "default_ttl")]
19    pub ttl_seconds: u64,
20}
21
22impl Default for CacheConfig {
23    fn default() -> Self {
24        Self {
25            dir: None,
26            ttl_seconds: default_ttl(),
27        }
28    }
29}
30
31#[cfg(test)]
32#[allow(clippy::expect_used)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn default_cache_config() {
38        let cfg = CacheConfig::default();
39        assert!(cfg.dir.is_none());
40        assert_eq!(cfg.ttl_seconds, 300);
41    }
42
43    #[test]
44    fn deserialize_cache_override() {
45        let toml_str = r#"
46            dir = "/tmp/remendo-cache"
47            ttl_seconds = 60
48        "#;
49        let cfg: CacheConfig = toml::from_str(toml_str).expect("parse cache config");
50        assert_eq!(
51            cfg.dir.as_deref(),
52            Some(std::path::Path::new("/tmp/remendo-cache"))
53        );
54        assert_eq!(cfg.ttl_seconds, 60);
55    }
56}