diff --git a/src/server/config.rs b/src/server/config.rs index 2525eae..ff0f2fc 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -1,17 +1,42 @@ -use std::fmt::{Display, Formatter}; +use std::{ + collections::HashMap, + fmt::{Display, Formatter}, +}; use serde::Deserialize; use terrors::OneOf; use crate::filesystem::FileSystem; - #[derive(Debug, PartialEq, Eq, Deserialize)] pub struct Config { + forge: HashMap, +} + +#[derive(Debug, PartialEq, Eq, Deserialize)] +pub struct Forge { forge_type: ForgeType, hostname: String, user: String, // API Token // Private SSH Key Path + repos: HashMap, +} +impl Forge { + pub const fn forge_type(&self) -> &ForgeType { + &self.forge_type + } + + pub fn hostname(&self) -> &str { + &self.hostname + } + + pub fn user(&self) -> &str { + &self.user + } + + pub fn repos(&self) -> impl Iterator { + self.repos.iter() + } } #[derive(Debug, PartialEq, Eq, Deserialize)] @@ -34,19 +59,16 @@ impl Config { 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 hostname(&self) -> &str { - self.hostname.as_str() - } - pub fn user(&self) -> &str { - self.user.as_str() + + pub(crate) fn forges(&self) -> impl Iterator { + self.forge.iter() } } #[cfg(test)] mod tests { + use assert2::let_assert; + use crate::filesystem::FileSystem; use super::*; @@ -57,16 +79,22 @@ mod tests { fs.write_file( "git-next-server.toml", r#" + [forge.default] forge_type = "ForgeJo" hostname = "git.example.net" user = "Bob" + + [forge.default.repos] + hello = "user/world" "#, ) .map_err(OneOf::new)?; let config = Config::load(&fs)?; - assert_eq!(config.forge_type(), &ForgeType::ForgeJo); - assert_eq!(config.hostname(), "git.example.net".to_string()); - assert_eq!(config.user(), "Bob".to_string()); + let_assert!(Some(default) = config.forge.get("default")); + assert_eq!(default.forge_type, ForgeType::ForgeJo); + assert_eq!(default.hostname, "git.example.net".to_string()); + assert_eq!(default.user, "Bob".to_string()); + assert_eq!(default.repos.get("hello"), Some(&"user/world".to_string())); Ok(()) }