Skip to main content

wowlab_docgen_cli/context/
children.rs

1use serde::Serialize;
2
3use crate::{HasName, RenderCtx, infra::metadata, sort_by_name};
4
5/// A child directory entry for index tables.
6#[derive(Debug, Serialize)]
7pub(super) struct Child {
8    pub name: String,
9    pub description: String,
10}
11
12impl HasName for Child {
13    fn name(&self) -> &str {
14        &self.name
15    }
16}
17
18/// Gather child directories that have a README (template or generated).
19pub(super) fn gather(ctx: &RenderCtx<'_>) -> Vec<Child> {
20    let mut children: Vec<Child> = ctx
21        .workspace
22        .child_dirs(ctx.dir)
23        .filter_map(|path| {
24            let name = path.file_name()?.to_string_lossy().to_string();
25
26            if !ctx.workspace.contains(&path.join("README.md.in"))
27                && !ctx.workspace.contains(&path.join("README.md"))
28            {
29                return None;
30            }
31
32            let meta = metadata::load(&ctx.workspace, path);
33
34            Some(Child {
35                name,
36                description: meta.description.clone().unwrap_or_default(),
37            })
38        })
39        .collect();
40
41    sort_by_name(&mut children);
42
43    children
44}