Skip to main content

wowlab_docgen_cli/infra/
walk.rs

1use wowlab_fs::path::{Path, PathBuf};
2
3const TEMPLATE_SUFFIX: &str = ".md.in";
4
5#[cfg(test)]
6fn is_template(path: &Path) -> bool {
7    path.file_name()
8        .is_some_and(|n| n.to_string_lossy().ends_with(TEMPLATE_SUFFIX))
9}
10
11/// Derive the output path by stripping the `.in` suffix.
12#[must_use]
13pub fn output_path(template: &Path) -> PathBuf {
14    let Some(name) = template.file_name() else {
15        return template.to_path_buf();
16    };
17    let name_str = name.to_string_lossy();
18    let out_name = name_str.strip_suffix(".in").unwrap_or(&name_str);
19
20    template.with_file_name(out_name)
21}
22
23/// Extract the output stem (e.g. "README" from "README.md.in").
24#[must_use]
25pub fn output_stem(template: &Path) -> String {
26    let Some(name) = template.file_name() else {
27        return String::new();
28    };
29
30    name.to_string_lossy()
31        .strip_suffix(TEMPLATE_SUFFIX)
32        .unwrap_or(&name.to_string_lossy())
33        .to_string()
34}
35
36#[cfg(test)]
37mod tests {
38    use googletest::prelude::*;
39
40    use super::*;
41
42    #[gtest]
43    fn output_path_strips_in() -> Result<()> {
44        verify_eq!(
45            output_path(Path::new("/foo/bar/README.md.in")),
46            PathBuf::from("/foo/bar/README.md")
47        )
48    }
49
50    #[gtest]
51    fn output_path_claude() -> Result<()> {
52        verify_eq!(
53            output_path(Path::new("/foo/CLAUDE.md.in")),
54            PathBuf::from("/foo/CLAUDE.md")
55        )
56    }
57
58    #[gtest]
59    fn stem_extraction() -> Result<()> {
60        verify_eq!(output_stem(Path::new("README.md.in")), "README")?;
61
62        verify_eq!(output_stem(Path::new("CLAUDE.md.in")), "CLAUDE")
63    }
64
65    #[gtest]
66    fn template_detection() -> Result<()> {
67        verify_true!(is_template(Path::new("README.md.in")))?;
68        verify_true!(is_template(Path::new("CLAUDE.md.in")))?;
69        verify_false!(is_template(Path::new("README.md")))?;
70
71        verify_false!(is_template(Path::new("foo.rs")))
72    }
73}