markupwright 0.1.3

Template-driven workspace documentation generator
Documentation
use std::{collections::BTreeMap, path::Path};

use serde::{Deserialize, Serialize};

use crate::domain::{ContextEntry, collection::Scope};

#[derive(Debug)]
pub(crate) struct Domain;

#[derive(Debug, Deserialize)]
#[serde(transparent)]
pub(crate) struct Config(BTreeMap<String, Collection>);

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct Collection {
    path: crate::config::RepositoryPath,
    #[serde(default)]
    scope: Scope,
}

#[derive(Debug, Serialize)]
struct Variable {
    name: String,
    default: String,
}

impl crate::domain::registry::Domain for Domain {
    type Config = Config;
    type State = ();

    const NAME: &'static str = "dotenv";

    fn context(
        config: &Self::Config,
        _state: &Self::State,
        context: &crate::RenderCtx<'_>,
    ) -> Result<Vec<ContextEntry>, crate::domain::Error> {
        let mut output = Vec::new();

        for (namespace, collection) in &config.0 {
            let Some(base) = collection.scope.base(context) else {
                continue;
            };
            let path = base.join(collection.path.as_path());
            let Some(source) =
                crate::domain::workspace_source(&context.workspace, &path, "dotenv")?
            else {
                continue;
            };
            let variables = parse(source)
                .map_err(|error| crate::domain::Error::parse(&path, "dotenv", error))?;

            if !variables.is_empty() {
                output.push((
                    namespace.clone().into_boxed_str(),
                    crate::domain::to_value(&variables),
                ));
            }
        }

        Ok(output)
    }

    fn is_source_input(
        config: &Self::Config,
        _state: &Self::State,
        root: &Path,
        path: &Path,
    ) -> bool {
        config.0.values().any(|collection| {
            crate::domain::collection::path_matches(
                root,
                collection.scope,
                collection.path.as_path(),
                path,
            )
        })
    }

    fn namespaces(config: &Self::Config) -> Vec<Box<str>> {
        config
            .0
            .keys()
            .map(|namespace| namespace.clone().into_boxed_str())
            .collect()
    }
}

fn parse(source: &str) -> Result<Vec<Variable>, dotenvy::Error> {
    dotenvy::from_read_iter(source.as_bytes())
        .map(|entry| entry.map(|(name, default)| Variable { name, default }))
        .collect()
}

crate::domain::registry::register_domain!(Domain);

#[cfg(test)]
mod tests {
    use googletest::prelude::*;

    use super::*;

    #[gtest]
    fn parses_dotenv() -> Result<()> {
        let variables =
            parse("# comment\nAPI_URL=\"https://example.com\"\nEMPTY=\nexport TOKEN='abc'\n")
                .or_fail()?;

        verify_that!(&variables, len(eq(3)))?;
        verify_eq!(variables[0].name, "API_URL")?;
        verify_eq!(variables[0].default, "https://example.com")?;
        verify_eq!(variables[1].default, "")?;
        verify_eq!(variables[2].default, "abc")
    }
}