use crate::models::{ClaudeQuotaSnapshot, CodexQuotaSnapshot};
use crate::utils::{get_claude_usage_cache_path, get_codex_usage_cache_path, write_json_atomic};
use anyhow::Result;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::path::PathBuf;
fn load_cache<T: DeserializeOwned>(path: Result<PathBuf>) -> Option<T> {
let body = std::fs::read_to_string(path.ok()?).ok()?;
serde_json::from_str(&body).ok()
}
fn save_cache<T: Serialize>(path: Result<PathBuf>, snap: &T) -> Result<()> {
write_json_atomic(path?, snap)
}
pub fn load_claude_cache() -> Option<ClaudeQuotaSnapshot> {
load_cache(get_claude_usage_cache_path())
}
pub fn save_claude_cache(snap: &ClaudeQuotaSnapshot) -> Result<()> {
save_cache(get_claude_usage_cache_path(), snap)
}
pub fn load_codex_cache() -> Option<CodexQuotaSnapshot> {
load_cache(get_codex_usage_cache_path())
}
pub fn save_codex_cache(snap: &CodexQuotaSnapshot) -> Result<()> {
save_cache(get_codex_usage_cache_path(), snap)
}