LINE1 10 20 30 40 50 60 70 80 90 1001use anyhow::{Context, Result};2use serde::{Deserialize, Serialize};3use std::collections::HashMap;4use std::path::PathBuf;56#[derive(Debug, Serialize, Deserialize, Default, Clone)]7pub struct Usage {8 9 #[serde(default)]10 pub repos: HashMap<String, u32>,11 12 #[serde(default)]13 pub projects: HashMap<String, u32>,14}1516#[derive(Debug, Serialize, Deserialize, Default, Clone)]17pub struct Cache {18 pub repos: Vec<Repo>,19 pub projects: Vec<Project>,20 #[serde(default)]21 pub usage: Usage,22}2324#[derive(Debug, Serialize, Deserialize, Clone)]25pub struct Repo {26 pub name: String,27 pub full_name: String,28}2930#[derive(Debug, Serialize, Deserialize, Clone)]31pub struct Project {32 pub id: String,33 pub title: String,34 pub status_field_id: String,35 pub status_options: Vec<StatusOption>,36}3738#[derive(Debug, Serialize, Deserialize, Clone)]39pub struct StatusOption {40 pub id: String,41 pub name: String,42}4344impl Cache {45 pub fn dir() -> Result<PathBuf> {46 dirs::cache_dir()47 .map(|d| d.join("pike"))48 .context("Could not determine cache directory")49 }5051 pub fn load() -> Result<Self> {52 let path = Self::dir()?.join("cache.json");53 if !path.exists() {54 return Ok(Self::default());55 }56 let content = std::fs::read_to_string(&path).context("Failed to read cache file")?;57 serde_json::from_str(&content).context("Failed to parse cache file")58 }5960 61 62 pub fn record_issue(&mut self, repo_full_name: &str, project_id: Option<&str>) -> Result<()> {63 *self64 .usage65 .repos66 .entry(repo_full_name.to_string())67 .or_insert(0) += 1;68 if let Some(pid) = project_id {69 *self.usage.projects.entry(pid.to_string()).or_insert(0) += 1;70 }71 self.save()72 }7374 pub fn save(&self) -> Result<()> {75 let dir = Self::dir()?;76 std::fs::create_dir_all(&dir).context("Failed to create cache directory")?;77 let path = dir.join("cache.json");78 let content = serde_json::to_string_pretty(self).context("Failed to serialize cache")?;79 std::fs::write(&path, content).context("Failed to write cache file")80 }81}8283#[cfg(test)]84mod tests {85 use super::*;8687 #[test]88 fn cache_default() {89 let cache = Cache::default();90 assert!(cache.repos.is_empty());91 assert!(cache.projects.is_empty());92 }9394 #[test]95 fn cache_roundtrip() {96 let cache = Cache {97 repos: vec![98 Repo {99 name: "repo1".to_string(),100 full_name: "org/repo1".to_string(),101 },102 Repo {103 name: "repo2".to_string(),104 full_name: "org/repo2".to_string(),105 },106 ],107 projects: vec![Project {108 id: "PVT_123".to_string(),109 title: "My Project".to_string(),110 status_field_id: "PVTFL_456".to_string(),111 status_options: vec![112 StatusOption {113 id: "opt1".to_string(),114 name: "Todo".to_string(),115 },116 StatusOption {117 id: "opt2".to_string(),118 name: "Done".to_string(),119 },120 ],121 }],122 };123124 let json = serde_json::to_string_pretty(&cache).unwrap();125 let deserialized: Cache = serde_json::from_str(&json).unwrap();126127 assert_eq!(deserialized.repos.len(), 2);128 assert_eq!(deserialized.repos[0].full_name, "org/repo1");129 assert_eq!(deserialized.projects.len(), 1);130 assert_eq!(deserialized.projects[0].title, "My Project");131 assert_eq!(deserialized.projects[0].status_options.len(), 2);132 assert_eq!(deserialized.projects[0].status_options[0].name, "Todo");133 }134135 #[test]136 fn cache_save_and_load() {137 let dir = std::env::temp_dir().join("pike_test_cache");138 std::fs::create_dir_all(&dir).unwrap();139 let path = dir.join("cache.json");140141 let cache = Cache {142 repos: vec![Repo {143 name: "test".to_string(),144 full_name: "org/test".to_string(),145 }],146 projects: vec![],147 };148149 let content = serde_json::to_string_pretty(&cache).unwrap();150 std::fs::write(&path, &content).unwrap();151152 let loaded: Cache = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();153 assert_eq!(loaded.repos.len(), 1);154 assert_eq!(loaded.repos[0].full_name, "org/test");155156 std::fs::remove_dir_all(&dir).ok();157 }158159 #[test]160 fn repo_equality() {161 let repo1 = Repo {162 name: "pike".to_string(),163 full_name: "anomalyco/pike".to_string(),164 };165 let repo2 = Repo {166 name: "pike".to_string(),167 full_name: "anomalyco/pike".to_string(),168 };169 assert_eq!(repo1.name, repo2.name);170 assert_eq!(repo1.full_name, repo2.full_name);171 }172173 #[test]174 fn status_option_deserialize() {175 let json = r#"{"id": "abc", "name": "In Progress"}"#;176 let opt: StatusOption = serde_json::from_str(json).unwrap();177 assert_eq!(opt.id, "abc");178 assert_eq!(opt.name, "In Progress");179 }180}