fix(config): parse the server file with sections

This commit is contained in:
Paul Campbell 2024-04-07 18:58:08 +01:00
parent 376830c8a6
commit f1041d6fe6

View file

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