fix(config): parse the server file with sections
This commit is contained in:
parent
376830c8a6
commit
f1041d6fe6
1 changed files with 41 additions and 13 deletions
|
@ -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<String, Forge>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Deserialize)]
|
||||
pub struct Forge {
|
||||
forge_type: ForgeType,
|
||||
hostname: String,
|
||||
user: String,
|
||||
// API Token
|
||||
// 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)]
|
||||
|
@ -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<Item = (&String, &Forge)> {
|
||||
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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue