Skip to main content

wowlab_docgen_cli/infra/
mermaid.rs

1use wowlab_common::markdown;
2use wowlab_fs::path::Path;
3
4/// Include `{name}.mermaid` from `dir` as a fenced mermaid block, or empty if missing.
5#[must_use]
6pub fn build(workspace: &crate::WorkspaceIndex, dir: &Path, name: &str) -> String {
7    let path = dir.join(format!("{name}.mermaid"));
8
9    let Some(contents) = workspace.contents(&path) else {
10        return String::new();
11    };
12
13    let trimmed = contents.trim_end();
14
15    markdown::code_block("mermaid", trimmed)
16}
17
18#[cfg(test)]
19mod tests {
20    use googletest::prelude::*;
21    use wowlab_fs::path::Path;
22
23    use crate::{WorkspaceFile, WorkspaceIndex};
24
25    fn build(name: &str, contents: Option<&str>) -> String {
26        let root = Path::new("/ws");
27        let files = contents.into_iter().map(|contents| WorkspaceFile {
28            path: root.join(format!("{name}.mermaid")),
29            contents: Some(Box::from(contents)),
30        });
31        let workspace = WorkspaceIndex::new(root.to_path_buf(), files.collect());
32
33        super::build(&workspace, root, name)
34    }
35
36    #[gtest]
37    fn build_wraps_in_mermaid_block() {
38        let result = build("flow", Some("graph TD\n    A --> B\n    B --> C\n"));
39
40        insta::assert_snapshot!(result);
41    }
42
43    #[gtest]
44    fn build_trims_trailing_whitespace() {
45        let result = build("dia", Some("graph LR\n    X --> Y  \n\n\n"));
46
47        insta::assert_snapshot!(result);
48    }
49
50    #[gtest]
51    fn build_empty_for_missing_file() -> Result<()> {
52        let result = build("nonexistent", None);
53
54        verify_eq!(result, "")
55    }
56
57    #[gtest]
58    fn build_empty_content_file() -> Result<()> {
59        let result = build("empty", Some(""));
60
61        verify_eq!(result, "```mermaid\n\n```")
62    }
63}