💾 Archived View for lists.flounder.online › patches › threads › 20210316220018.8546-3-johann@qwertqw… captured on 2022-04-28 at 19:25:13. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
From: johann@qwertqwefsday.eu
Date: Tue, 16 Mar 2021 23:00:17 +0100
Message-Id: 20210316220018.8546-3-johann@qwertqwefsday.eu
To: <~aw/patches@lists.sr.ht>
In-Reply-To: 20210316220018.8546-1-johann@qwertqwefsday.eu
Cc: "Johann150" <johann@qwertqwefsday.eu>
--------------------------------------
From: Johann150 <johann@qwertqwefsday.eu>
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.
---
src/main.rs | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index a19fd07..cdc88ea 100644
--- 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) => {
--
2.20.1