Sign up form and other stuff
[recipes.git] / backend / src / config.rs
diff --git a/backend/src/config.rs b/backend/src/config.rs
new file mode 100644 (file)
index 0000000..393a03b
--- /dev/null
@@ -0,0 +1,32 @@
+use std::{fmt, fs::File};
+
+use ron::de::from_reader;
+use serde::Deserialize;
+
+use crate::consts;
+
+#[derive(Deserialize, Clone)]
+pub struct Config {
+    pub port: u16,
+    pub smtp_login: String,
+    pub smtp_password: String,
+}
+
+// Avoid to print passwords.
+impl fmt::Debug for Config {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("Config")
+         .field("port", &self.port)
+         .field("smtp_login", &self.smtp_login)
+         .field("smtp_password", &"*****")
+         .finish()
+    }
+}
+
+pub fn load() -> Config {
+    let f = File::open(consts::FILE_CONF).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
+    match from_reader(f) {
+        Ok(c) => c,
+        Err(e) => panic!("Failed to load config: {}", e)
+    }
+}
\ No newline at end of file