use serde::Deserialize; use terrors::OneOf; use crate::filesystem::FileSystem; #[derive(Debug, PartialEq, Eq, Deserialize)] pub struct Config { forge_type: ForgeType, url: String, user: String, // API Token // Private SSH Key Path } #[derive(Debug, PartialEq, Eq, Deserialize)] pub enum ForgeType { ForgeJo, // Gitea, // GitHub, // GitLab, // BitBucket, } #[allow(dead_code)] impl Config { pub(crate) fn load(fs: &FileSystem) -> Result> { let str = fs.read_file("git-next-server.toml").map_err(OneOf::new)?; toml::from_str(&str).map_err(OneOf::new) } pub const fn forge_type(&self) -> &ForgeType { &self.forge_type } pub fn url(&self) -> &str { self.url.as_str() } pub fn user(&self) -> &str { self.user.as_str() } } #[cfg(test)] mod tests { use crate::filesystem::FileSystem; use super::*; #[test] fn test_config_load() -> Result<(), OneOf<(std::io::Error, toml::de::Error)>> { let fs = FileSystem::new_temp().map_err(OneOf::new)?; fs.write_file( "git-next-server.toml", r#" forge_type = "ForgeJo" url = "https://forge.jo" user = "Bob" "#, ) .map_err(OneOf::new)?; let config = Config::load(&fs)?; assert_eq!(config.forge_type(), &ForgeType::ForgeJo); assert_eq!(config.url(), "https://forge.jo".to_string()); assert_eq!(config.user(), "Bob".to_string()); Ok(()) } }