use anyhow::{Context, Result};
use std::fs;
use std::io::Write;
use std::path::Path;
use crate::constants::paths::STATS_FILENAME;
use super::types::ProductivityStats;
pub fn load_productivity_stats(cache_dir: &Path) -> Result<ProductivityStats> {
let path = cache_dir.join(STATS_FILENAME);
if !path.exists() {
return Ok(ProductivityStats::default());
}
let content = fs::read_to_string(&path)
.with_context(|| format!("Failed to read productivity stats from {}", path.display()))?;
let stats: ProductivityStats = serde_json::from_str(&content)
.with_context(|| format!("Failed to parse productivity stats from {}", path.display()))?;
Ok(stats)
}
pub fn save_productivity_stats(stats: &ProductivityStats, cache_dir: &Path) -> Result<()> {
let path = cache_dir.join(STATS_FILENAME);
fs::create_dir_all(cache_dir)
.with_context(|| format!("Failed to create cache directory {}", cache_dir.display()))?;
let content =
serde_json::to_string_pretty(stats).context("Failed to serialize productivity stats")?;
let temp_path = path.with_extension("tmp");
let mut file = fs::File::create(&temp_path)
.with_context(|| format!("Failed to create temp file {}", temp_path.display()))?;
file.write_all(content.as_bytes())
.with_context(|| format!("Failed to write to temp file {}", temp_path.display()))?;
file.flush()
.with_context(|| format!("Failed to flush temp file {}", temp_path.display()))?;
drop(file);
fs::rename(&temp_path, &path)
.with_context(|| format!("Failed to rename temp file to {}", path.display()))?;
Ok(())
}