tera_former/
lib.rs

1//! tera-former: a stupid simple static site generator
2
3#![forbid(unsafe_code)]
4#![warn(missing_docs, unreachable_pub, unused_crate_dependencies)]
5#![warn(clippy::all, clippy::cargo, clippy::nursery, clippy::pedantic)]
6#![warn(clippy::unwrap_used)]
7
8use entrypoint::prelude::*;
9
10use std::path::PathBuf;
11
12/// CLI options
13#[derive(clap::Parser, DotEnvDefault, LoggerDefault, Clone, Debug)]
14#[command(version, about, long_about = None)]
15pub struct CLIArgs {
16    /// directory to save generated output
17    #[clap(long, env)]
18    output_directory: Option<PathBuf>,
19    /// template source directory
20    #[clap(long, env)]
21    source_directory: Option<PathBuf>,
22}
23
24/// required config
25#[derive(Clone, Debug)]
26struct Config {
27    /// directory to save generated output
28    output_directory: PathBuf,
29    /// template source directory
30    source_directory: PathBuf,
31}
32
33impl TryFrom<CLIArgs> for Config {
34    type Error = entrypoint::anyhow::Error;
35
36    fn try_from(item: CLIArgs) -> entrypoint::anyhow::Result<Self> {
37        let output_directory: PathBuf = if let Ok(dir) = std::env::var("OUTPUT_DIRECTORY") {
38            dir.into()
39        } else {
40            item.output_directory
41                .context("no output_directory supplied")?
42        };
43
44        let source_directory: PathBuf = if let Ok(dir) = std::env::var("SOURCE_DIRECTORY") {
45            dir.into()
46        } else {
47            item.source_directory
48                .context("no source_directory supplied")?
49        };
50
51        Ok(Self {
52            output_directory,
53            source_directory,
54        })
55    }
56}
57
58/// generate the static site
59pub fn generate(args: CLIArgs) -> entrypoint::anyhow::Result<()> {
60    let config: Config = args.try_into()?;
61
62    info!("{:#?}", config);
63
64    Ok(())
65}