LINE1 10 20 30 40 50 60 70 80 901use anyhow::{Context, Result};2use serde::{Deserialize, Serialize};3use std::path::PathBuf;45#[derive(Debug, Serialize, Deserialize, Default, Clone)]6pub struct Config {7 #[serde(default)]8 pub default_org: Option<String>,9 #[serde(default)]10 pub default_project: Option<String>,11 #[serde(default)]12 pub favorite_repos: Vec<String>,13}1415impl Config {16 pub fn dir() -> Result<PathBuf> {17 dirs::config_dir()18 .map(|d| d.join("pike"))19 .context("Could not determine config directory")20 }2122 pub fn load() -> Result<Self> {23 let path = Self::dir()?.join("config.json");24 if !path.exists() {25 return Ok(Self::default());26 }27 let content = std::fs::read_to_string(&path).context("Failed to read config file")?;28 serde_json::from_str(&content).context("Failed to parse config file")29 }3031 pub fn save(&self) -> Result<()> {32 let dir = Self::dir()?;33 std::fs::create_dir_all(&dir).context("Failed to create config directory")?;34 let path = dir.join("config.json");35 let content = serde_json::to_string_pretty(self).context("Failed to serialize config")?;36 std::fs::write(&path, content).context("Failed to write config file")37 }38}3940#[cfg(test)]41mod tests {42 use super::*;43 use tempfile::TempDir;4445 fn with_temp_config_dir<F>(f: F)46 where47 F: FnOnce(PathBuf),48 {49 let tmp = TempDir::new().unwrap();50 let dir = tmp.path().join("pike");51 f(dir);52 }5354 #[test]55 fn config_default_values() {56 let config = Config::default();57 assert!(config.default_org.is_none());58 assert!(config.default_project.is_none());59 assert!(config.favorite_repos.is_empty());60 }6162 #[test]63 fn config_roundtrip() {64 let config = Config {65 default_org: Some("my-org".to_string()),66 default_project: Some("Roadmap".to_string()),67 favorite_repos: vec!["my-org/repo1".to_string(), "my-org/repo2".to_string()],68 };6970 let json = serde_json::to_string_pretty(&config).unwrap();71 let deserialized: Config = serde_json::from_str(&json).unwrap();7273 assert_eq!(deserialized.default_org, Some("my-org".to_string()));74 assert_eq!(deserialized.default_project, Some("Roadmap".to_string()));75 assert_eq!(76 deserialized.favorite_repos,77 vec!["my-org/repo1", "my-org/repo2"]78 );79 }8081 #[test]82 fn config_save_and_load() {83 with_temp_config_dir(|dir| {84 let config = Config {85 default_org: Some("test-org".to_string()),86 default_project: None,87 favorite_repos: vec![],88 };8990 std::fs::create_dir_all(&dir).unwrap();91 let path = dir.join("config.json");92 let content = serde_json::to_string_pretty(&config).unwrap();93 std::fs::write(&path, &content).unwrap();9495 let loaded: Config =96 serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();97 assert_eq!(loaded.default_org, Some("test-org".to_string()));98 assert!(loaded.default_project.is_none());99 });100 }101102 #[test]103 fn config_deserialize_missing_fields() {104 let json = r#"{}"#;105 let config: Config = serde_json::from_str(json).unwrap();106 assert!(config.default_org.is_none());107 assert!(config.default_project.is_none());108 assert!(config.favorite_repos.is_empty());109 }110}