mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit eb512011fc03a7e7166622148f38f6b49bd32f9d
parent 67b63145c37528102bc073da4bedb61fcff40241
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Tue, 16 Mar 2021 23:00:17 +0100

add defaults for configuration

Because there are defaults for everything, we can now also handle
an empty configuratino file without failing. A warning is emitted
if the configuration file does not exist.

Diffstat:
Msrc/main.rs | 31+++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -12,13 +12,38 @@ use tide::Request; #[derive(Deserialize, Debug)] pub struct Config { + #[serde(default = "defaults::port")] port: u16, + #[serde(default = "defaults::repo_directory")] projectroot: String, + #[serde(default = "String::new")] emoji_favicon: String, + #[serde(default = "defaults::site_name")] site_name: String, + #[serde(default = "defaults::export_ok")] export_ok: String, } +/// Defaults for the configuration options +// FIXME: simplify if https://github.com/serde-rs/serde/issues/368 is resolved +mod defaults { + pub fn port() -> u16 { + 80 + } + + pub fn repo_directory() -> String { + "repos".to_string() + } + + pub fn site_name() -> String { + "grifter".to_string() + } + + pub fn export_ok() -> String { + "git-daemon-export-ok".to_string() + } +} + const HELP: &str = "\ mygit @@ -40,8 +65,10 @@ fn args() -> Config { std::process::exit(0); } - let toml_text = - fs::read_to_string("mygit.toml").expect("expected configuration file mygit.toml"); + let toml_text = fs::read_to_string("mygit.toml").unwrap_or_else(|_| { + tide::log::warn!("mygit.toml configuration file not found, using defaults"); + String::new() + }); match toml::from_str(&toml_text) { Ok(config) => config, Err(e) => {