From 9613035188c5f2d3acff5e4203f6f045a3c95fd6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 28 Jun 2023 13:10:56 +0200 Subject: [PATCH] Improve config loading --- src/args.rs | 8 ++++---- src/data/page.rs | 15 +++++++++++---- src/main.rs | 10 +++++----- src/rendering/mod.rs | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/args.rs b/src/args.rs index f80aef4..c57357c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -7,6 +7,9 @@ use clap::{Parser, Subcommand}; pub struct Args { #[command(subcommand)] pub command: Command, + + #[clap(default_value = ".")] + pub directory: PathBuf, } #[derive(Clone, Debug, Subcommand)] @@ -16,7 +19,4 @@ pub enum Command { } #[derive(Clone, Debug, Parser)] -pub struct BuildArgs { - #[clap(default_value = ".")] - pub directory: PathBuf, -} +pub struct BuildArgs {} diff --git a/src/data/page.rs b/src/data/page.rs index 7606c51..98a017c 100644 --- a/src/data/page.rs +++ b/src/data/page.rs @@ -2,17 +2,24 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Deserialize, Serialize)] pub enum Page { - Data(PageMetadata), + Data(PageData), Content(String), } #[derive(Clone, Debug, Deserialize, Serialize)] -pub struct PageMetadata { - /// template used to render this page - pub template: Option, +pub struct PageData { + /// Metadata for this page + #[serde(default)] + pub metadata: PageMetadata, /// remaining data of this page /// passed to the templates when rendering #[serde(flatten)] pub data: toml::Value, } + +#[derive(Default, Clone, Debug, Deserialize, Serialize)] +pub struct PageMetadata { + /// template used to render this page + pub template: Option, +} diff --git a/src/main.rs b/src/main.rs index 7e7ce2b..574ec51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,17 +19,17 @@ async fn main() -> Result<()> { let args: Args = Args::parse(); init_tracing(); - match args.command { + match &args.command { args::Command::Build(build_args) => { - let cfg = read_config(&build_args.directory).await?; - build(cfg, build_args).await + let cfg = read_config(&args.directory).await?; + build(&args, &build_args, cfg).await } } } -async fn build(cfg: Config, args: BuildArgs) -> Result<()> { +async fn build(args: &Args, _build_args: &BuildArgs, cfg: Config) -> Result<()> { let folders = cfg.folders; - let base_path = args.directory; + let base_path = &args.directory; let content_dir = base_path.join(folders.content.unwrap_or("content".into())); let template_dir = base_path.join(folders.templates.unwrap_or("templates".into())); let out_dir = base_path.join(folders.output.unwrap_or("public".into())); diff --git a/src/rendering/mod.rs b/src/rendering/mod.rs index 1c75e20..bf2659b 100644 --- a/src/rendering/mod.rs +++ b/src/rendering/mod.rs @@ -45,7 +45,7 @@ impl ContentRenderer { match page { crate::data::Page::Data(data) => { - if let Some(tmpl) = data.template { + if let Some(tmpl) = data.metadata.template { template_name = tmpl; } context.insert("data", &data.data);