Improve config loading

main
trivernis 1 year ago
parent b7b262718d
commit 9613035188
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -7,6 +7,9 @@ use clap::{Parser, Subcommand};
pub struct Args { pub struct Args {
#[command(subcommand)] #[command(subcommand)]
pub command: Command, pub command: Command,
#[clap(default_value = ".")]
pub directory: PathBuf,
} }
#[derive(Clone, Debug, Subcommand)] #[derive(Clone, Debug, Subcommand)]
@ -16,7 +19,4 @@ pub enum Command {
} }
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]
pub struct BuildArgs { pub struct BuildArgs {}
#[clap(default_value = ".")]
pub directory: PathBuf,
}

@ -2,17 +2,24 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Page { pub enum Page {
Data(PageMetadata), Data(PageData),
Content(String), Content(String),
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PageMetadata { pub struct PageData {
/// template used to render this page /// Metadata for this page
pub template: Option<String>, #[serde(default)]
pub metadata: PageMetadata,
/// remaining data of this page /// remaining data of this page
/// passed to the templates when rendering /// passed to the templates when rendering
#[serde(flatten)] #[serde(flatten)]
pub data: toml::Value, pub data: toml::Value,
} }
#[derive(Default, Clone, Debug, Deserialize, Serialize)]
pub struct PageMetadata {
/// template used to render this page
pub template: Option<String>,
}

@ -19,17 +19,17 @@ async fn main() -> Result<()> {
let args: Args = Args::parse(); let args: Args = Args::parse();
init_tracing(); init_tracing();
match args.command { match &args.command {
args::Command::Build(build_args) => { args::Command::Build(build_args) => {
let cfg = read_config(&build_args.directory).await?; let cfg = read_config(&args.directory).await?;
build(cfg, build_args).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 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 content_dir = base_path.join(folders.content.unwrap_or("content".into()));
let template_dir = base_path.join(folders.templates.unwrap_or("templates".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())); let out_dir = base_path.join(folders.output.unwrap_or("public".into()));

@ -45,7 +45,7 @@ impl ContentRenderer {
match page { match page {
crate::data::Page::Data(data) => { crate::data::Page::Data(data) => {
if let Some(tmpl) = data.template { if let Some(tmpl) = data.metadata.template {
template_name = tmpl; template_name = tmpl;
} }
context.insert("data", &data.data); context.insert("data", &data.data);

Loading…
Cancel
Save