summaryrefslogtreecommitdiff
path: root/service/src/cache.rs
blob: acaf9d3dcbdd6d020c0ecceef0b5bdf4dff66e9a (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::{
    path::PathBuf,
    time::{Duration, SystemTime},
};

use anyhow::{Result, anyhow};

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
struct SerialCacheEntry {
    serial: String,
    expires_at_unix: u64,
}

fn cache_file_path() -> Result<PathBuf> {
    let proj_dir = dirs::cache_dir()
        .ok_or_else(|| anyhow!("No cache dir"))?
        .join("camper-widget-refresh");
    Ok(proj_dir.join("serial.json"))
}

pub async fn read_serial_cache() -> Result<Option<String>> {
    let path = cache_file_path()?;
    if !path.exists() {
        return Ok(None);
    }
    let content = tokio::fs::read_to_string(&path)
        .await
        .map_err(|e| anyhow!("Failed to read cache file at {}: {}", path.display(), e))?;
    let entry: SerialCacheEntry = serde_json::from_str(&content)?;
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs();
    if now <= entry.expires_at_unix {
        Ok(Some(entry.serial))
    } else {
        Ok(None)
    }
}

pub async fn write_serial_cache(serial: &str, ttl: Duration) -> Result<()> {
    let path = cache_file_path()?;
    if let Some(parent) = path.parent() {
        tokio::fs::create_dir_all(parent).await.map_err(|e| {
            anyhow!(
                "Failed to create cache directory {}: {}",
                parent.display(),
                e
            )
        })?;
    }
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs();
    let entry = SerialCacheEntry {
        serial: serial.to_string(),
        expires_at_unix: now + ttl.as_secs(),
    };
    let data = serde_json::to_string(&entry)?;
    tokio::fs::write(&path, data)
        .await
        .map_err(|e| anyhow!("Failed to write cache file at {}: {}", path.display(), e))?;
    Ok(())
}