1use crate::models::{
9 ClaudeQuotaSnapshot, CodexQuotaSnapshot, CopilotQuotaSnapshot, CursorQuotaSnapshot,
10};
11use crate::utils::{
12 get_claude_usage_cache_path, get_codex_usage_cache_path, get_copilot_usage_cache_path,
13 get_cursor_usage_cache_path, write_json_atomic,
14};
15use anyhow::Result;
16use serde::Serialize;
17use serde::de::DeserializeOwned;
18use std::path::PathBuf;
19
20fn load_cache<T: DeserializeOwned>(path: Result<PathBuf>) -> Option<T> {
22 let body = std::fs::read_to_string(path.ok()?).ok()?;
23 serde_json::from_str(&body).ok()
24}
25
26fn save_cache<T: Serialize>(path: Result<PathBuf>, snap: &T) -> Result<()> {
28 write_json_atomic(path?, snap)
29}
30
31pub fn load_claude_cache() -> Option<ClaudeQuotaSnapshot> {
33 load_cache(get_claude_usage_cache_path())
34}
35
36pub fn save_claude_cache(snap: &ClaudeQuotaSnapshot) -> Result<()> {
38 save_cache(get_claude_usage_cache_path(), snap)
39}
40
41pub fn load_codex_cache() -> Option<CodexQuotaSnapshot> {
43 load_cache(get_codex_usage_cache_path())
44}
45
46pub fn save_codex_cache(snap: &CodexQuotaSnapshot) -> Result<()> {
48 save_cache(get_codex_usage_cache_path(), snap)
49}
50
51pub fn load_copilot_cache() -> Option<CopilotQuotaSnapshot> {
53 load_cache(get_copilot_usage_cache_path())
54}
55
56pub fn save_copilot_cache(snap: &CopilotQuotaSnapshot) -> Result<()> {
58 save_cache(get_copilot_usage_cache_path(), snap)
59}
60
61pub fn load_cursor_cache() -> Option<CursorQuotaSnapshot> {
63 load_cache(get_cursor_usage_cache_path())
64}
65
66pub fn save_cursor_cache(snap: &CursorQuotaSnapshot) -> Result<()> {
68 save_cache(get_cursor_usage_cache_path(), snap)
69}