git-next/src/server/config.rs

74 lines
1.7 KiB
Rust
Raw Normal View History

use std::fmt::{Display, Formatter};
2024-04-07 13:47:39 +01:00
use serde::Deserialize;
use terrors::OneOf;
use crate::filesystem::FileSystem;
2024-04-07 16:09:16 +01:00
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Config {
forge_type: ForgeType,
hostname: String,
2024-04-07 13:47:39 +01:00
user: String,
// API Token
// Private SSH Key Path
}
2024-04-07 16:09:16 +01:00
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub enum ForgeType {
2024-04-07 13:47:39 +01:00
ForgeJo,
// Gitea,
// GitHub,
// GitLab,
// BitBucket,
}
impl Display for ForgeType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
2024-04-07 16:09:16 +01:00
#[allow(dead_code)]
2024-04-07 13:47:39 +01:00
impl Config {
pub(crate) fn load(fs: &FileSystem) -> Result<Self, OneOf<(std::io::Error, toml::de::Error)>> {
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
2024-04-07 13:47:39 +01:00
}
pub fn hostname(&self) -> &str {
self.hostname.as_str()
2024-04-07 13:47:39 +01:00
}
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"
hostname = "git.example.net"
2024-04-07 13:47:39 +01:00
user = "Bob"
"#,
)
.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());
2024-04-07 13:47:39 +01:00
assert_eq!(config.user(), "Bob".to_string());
Ok(())
}
}