use crate::models::{
ClaudeQuotaSnapshot, CodexQuotaSnapshot, CopilotQuotaSnapshot, CursorQuotaSnapshot,
};
use crate::utils::{
get_claude_usage_cache_path, get_codex_usage_cache_path, get_copilot_usage_cache_path,
get_cursor_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)
}
pub fn load_copilot_cache() -> Option<CopilotQuotaSnapshot> {
load_cache(get_copilot_usage_cache_path())
}
pub fn save_copilot_cache(snap: &CopilotQuotaSnapshot) -> Result<()> {
save_cache(get_copilot_usage_cache_path(), snap)
}
pub fn load_cursor_cache() -> Option<CursorQuotaSnapshot> {
load_cache(get_cursor_usage_cache_path())
}
pub fn save_cursor_cache(snap: &CursorQuotaSnapshot) -> Result<()> {
save_cache(get_cursor_usage_cache_path(), snap)
}