From dc829fc361ff4349de169f67c4ce20c47f961117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ioan=20Biz=C4=83u?= Date: Thu, 22 Aug 2024 23:09:39 +0300 Subject: [PATCH] Merge extra config of site and theme. --- src/main.rs | 1 + src/site.rs | 22 +++++++++++++++++++--- src/utils.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/utils.rs diff --git a/src/main.rs b/src/main.rs index 5b2a459..e5b151b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ mod sass; mod site; mod template; mod theme; +mod utils; use site::Site; use theme::Theme; diff --git a/src/site.rs b/src/site.rs index 52e6e74..61bcecf 100644 --- a/src/site.rs +++ b/src/site.rs @@ -18,6 +18,7 @@ use crate::{ content, nostr, resource::{ContentSource, Resource, ResourceKind}, template, + utils::merge, }; #[derive(Clone, Serialize, Deserialize)] @@ -71,6 +72,16 @@ impl SiteConfig { format!("{}/{}{}", self.base_url, path, trailing_bit) } } + + pub fn merge(&mut self, other: &SiteConfig) { + for (key, value) in &other.extra { + if !self.extra.contains_key(key) { + self.extra.insert(key.to_owned(), value.clone()); + continue; + } + merge(self.extra.get_mut(key).unwrap(), value).unwrap(); + } + } } fn load_templates(site_config: &SiteConfig) -> tera::Tera { @@ -448,9 +459,9 @@ pub fn load_sites() -> HashMap { let mut config = config.unwrap(); let theme_path = format!("./themes/{}", config.theme.as_ref().unwrap()); - let theme_config = load_config(&&format!("{}/config.toml", theme_path)); + let theme_config = load_config(&format!("{}/config.toml", theme_path)).unwrap(); - config.extra = theme_config.unwrap().extra; // TODO: merge rather than overwrite! + config.merge(&theme_config); let tera = load_templates(&config); @@ -488,7 +499,12 @@ pub fn create_site(domain: &str, admin_pubkey: Option) -> Site { ); fs::write(format!("./sites/{}/_config.toml", domain), &config_content).unwrap(); - let config = load_config(&format!("{}/_config.toml", path)).unwrap(); + let mut config = load_config(&format!("{}/_config.toml", path)).unwrap(); + + let theme_path = format!("./themes/{}", config.theme.as_ref().unwrap()); + let theme_config = load_config(&format!("{}/config.toml", theme_path)).unwrap(); + + config.merge(&theme_config); let tera = load_templates(&config); diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..0e77e3a --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,33 @@ +// * Code taken from [Zola](https://www.getzola.org/) and adapted. +// * Zola's MIT license applies. See: https://github.com/getzola/zola/blob/master/LICENSE + +use toml::Value as TomlValue; + +#[derive(Debug)] +pub struct MergeError; + +// https://github.com/getzola/zola/blob/master/components/config/src/config/mod.rs + +pub fn merge(into: &mut TomlValue, from: &TomlValue) -> Result<(), MergeError> { + match (from.is_table(), into.is_table()) { + (false, false) => { + // These are not tables so we have nothing to merge + Ok(()) + } + (true, true) => { + // Recursively merge these tables + let into_table = into.as_table_mut().unwrap(); + for (key, val) in from.as_table().unwrap() { + if !into_table.contains_key(key) { + // An entry was missing in the first table, insert it + into_table.insert(key.to_string(), val.clone()); + continue; + } + // Two entries to compare, recurse + merge(into_table.get_mut(key).unwrap(), val)?; + } + Ok(()) + } + _ => Err(MergeError), + } +}