From 104fa1d268d705d08a7835301c0dfd7a1d41aa71 Mon Sep 17 00:00:00 2001 From: berrysweet Date: Wed, 5 Jul 2023 22:08:54 +0200 Subject: [PATCH] feat: add loading config files --- Cargo.toml | 2 ++ src/data/formats.rs | 23 +++++++++++++++++++++++ src/main.rs | 19 ++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/data/formats.rs diff --git a/Cargo.toml b/Cargo.toml index 0274868..3ff4829 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +confy = "0.5.1" +serde = { version = "1.0.164", features = ["derive"] } \ No newline at end of file diff --git a/src/data/formats.rs b/src/data/formats.rs new file mode 100644 index 0000000..78701f3 --- /dev/null +++ b/src/data/formats.rs @@ -0,0 +1,23 @@ +use serde::Serialize; +use serde::Deserialize; + +#[derive(Debug, Serialize, Deserialize)] +pub struct RenderConfig { + pub site_name: String, + pub base_url: String, + pub author: String, + pub template: String, + pub site_description: String, +} + +impl ::std::default::Default for RenderConfig { + fn default() -> Self { + Self { + site_name: String::from("livejrnl-rs"), + base_url: String::from("localhost"), + author: String::from("me"), + template: String::from("/dev/null"), + site_description:String::from("This is a site rendered with livejrnl-rs.").into() + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..8cdfcec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,20 @@ +mod data { + pub mod formats; +} + +const APP_NAME: &str = "livejrnl-rs"; + fn main() { - println!("Hello, world!"); + let config_data = load_config(); + match config_data { + Ok(config_data) => { + println!("{:#?}", config_data); + }, + Err(error) => println!("{:#?}", error) + }; } + +fn load_config() -> Result { + let cfg: data::formats::RenderConfig = confy::load(APP_NAME, "config")?; + Ok(cfg) +} \ No newline at end of file -- libgit2 1.7.2