markupwright 0.1.3

Template-driven workspace documentation generator
Documentation
use crate::RenderCtx;

pub(super) fn build(ctx: &RenderCtx<'_>) -> String {
    if ctx.is_root() {
        return String::new();
    }

    let parts: Vec<&str> = ctx.rel_dir.split('/').filter(|s| !s.is_empty()).collect();

    if parts.is_empty() {
        return String::new();
    }

    let depth = parts.len();
    let root_name = ctx
        .workspace
        .metadata(ctx.root)
        .and_then(|metadata| metadata.name.as_deref())
        .unwrap_or("workspace");
    let document = ctx.workspace.config().navigation_document();
    let mut crumbs = vec![format!("[{root_name}]({}{document})", "../".repeat(depth))];
    let mut ancestors = Vec::with_capacity(depth.saturating_sub(1));

    for (index, part) in parts.iter().take(depth.saturating_sub(1)).enumerate() {
        ancestors.push(*part);
        let directory = ctx.root.join(ancestors.join("/"));

        if ctx.workspace.has_navigation_document(&directory) {
            let ups = "../".repeat(depth - index - 1);

            crumbs.push(format!("[{part}]({ups}{document})"));
        }
    }

    crumbs.join(" / ")
}

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

    use super::*;
    use crate::{WorkspaceFile, WorkspaceIndex, domain::Metadata, test_helpers::workspace_at};

    #[gtest]
    fn root_returns_empty() -> Result<()> {
        let meta = Metadata {
            kind: "directory",
            ..Default::default()
        };
        let ctx = RenderCtx {
            workspace: workspace_at(Path::new("/ws")),
            root: Path::new("/ws"),
            dir: Path::new("/ws"),
            rel_dir: ".",
            metadata: &meta,
        };

        verify_eq!(build(&ctx), "")
    }

    #[gtest]
    fn depth_one_no_intermediate() -> Result<()> {
        let meta = Metadata {
            kind: "directory",
            ..Default::default()
        };
        let ctx = RenderCtx {
            workspace: workspace_at(Path::new("/nonexistent-ws")),
            root: Path::new("/nonexistent-ws"),
            dir: Path::new("/nonexistent-ws/deploy"),
            rel_dir: "deploy",
            metadata: &meta,
        };

        verify_eq!(build(&ctx), "[nonexistent-ws](../README.md)")
    }

    #[gtest]
    fn depth_two_with_intermediate() -> Result<()> {
        let root = Path::new("/ws");
        let dir = root.join("crates/sentinel");
        let workspace = WorkspaceIndex::new(
            root.to_path_buf(),
            vec![WorkspaceFile {
                path: root.join("crates/README.md"),
                contents: Some(Box::from("# crates")),
            }],
        );
        let meta = Metadata {
            kind: "directory",
            ..Default::default()
        };
        let ctx = RenderCtx {
            workspace,
            root,
            dir: &dir,
            rel_dir: "crates/sentinel",
            metadata: &meta,
        };
        let result = build(&ctx);

        verify_that!(result.as_str(), contains_substring("[ws](../../README.md)"))?;
        verify_that!(
            result.as_str(),
            contains_substring("[crates](../README.md)")
        )?;
        verify_that!(result.as_str(), contains_substring(" / "))?;

        Ok(())
    }

    #[gtest]
    fn depth_two_without_intermediate_readme() -> Result<()> {
        let meta = Metadata {
            kind: "directory",
            ..Default::default()
        };
        let ctx = RenderCtx {
            workspace: workspace_at(Path::new("/nonexistent-ws")),
            root: Path::new("/nonexistent-ws"),
            dir: Path::new("/nonexistent-ws/crates/sentinel"),
            rel_dir: "crates/sentinel",
            metadata: &meta,
        };

        verify_eq!(build(&ctx), "[nonexistent-ws](../../README.md)")
    }
}