diff --git a/src/server/config.rs b/src/server/config.rs new file mode 100644 index 00000000..dd401c37 --- /dev/null +++ b/src/server/config.rs @@ -0,0 +1,64 @@ +use serde::Deserialize; +use terrors::OneOf; + +use crate::filesystem::FileSystem; + +#[derive(Debug, PartialEq, Deserialize)] +pub(crate) struct Config { + r#type: ForgeType, + url: String, + user: String, + // API Token + // Private SSH Key Path +} + +#[derive(Debug, PartialEq, Deserialize)] +pub(crate) enum ForgeType { + ForgeJo, + // Gitea, + // GitHub, + // GitLab, + // BitBucket, +} +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 r#type(&self) -> &ForgeType { + &self.r#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#" + type = "ForgeJo" + url = "https://forge.jo" + user = "Bob" + "#, + ) + .map_err(OneOf::new)?; + let config = Config::load(&fs)?; + assert_eq!(config.r#type(), &ForgeType::ForgeJo); + assert_eq!(config.url(), "https://forge.jo".to_string()); + assert_eq!(config.user(), "Bob".to_string()); + + Ok(()) + } +} diff --git a/src/server/mod.rs b/src/server/mod.rs index ef5c2c21..a4050fa6 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,3 +1,5 @@ +mod config; + use std::path::PathBuf; use tracing::info; @@ -22,12 +24,13 @@ pub(crate) fn init(fs: FileSystem) { } } -pub(crate) fn start(_fs: FileSystem) { +pub(crate) fn start(fs: FileSystem) { let Ok(_) = init_logging() else { eprintln!("Failed to initialize logging."); return; }; info!("Starting Server..."); + let _config = config::Config::load(&fs); // todo!() }