Skip to main content

wowlab_docgen_cli/context/collectors/
aliases.rs

1use serde::Serialize;
2
3use super::{ContextEntry, to_value};
4use crate::{HasName, RenderCtx, sort_by_name};
5
6/// A cargo alias entry for template rendering.
7#[derive(Debug, Serialize)]
8pub struct Alias {
9    pub name: String,
10    pub command: String,
11}
12
13impl HasName for Alias {
14    fn name(&self) -> &str {
15        &self.name
16    }
17}
18
19/// Collect cargo aliases from `.cargo/config.toml` when rendering at the workspace root.
20#[must_use]
21pub fn collect(ctx: &RenderCtx<'_>) -> Vec<ContextEntry> {
22    if !ctx.is_root() {
23        return Vec::new();
24    }
25
26    let config_path = ctx.root.join(".cargo/config.toml");
27    let table = ctx
28        .workspace
29        .contents(&config_path)
30        .and_then(|content| content.parse::<toml::Table>().ok());
31    let Some(aliases) = table
32        .as_ref()
33        .and_then(|t| t.get("alias"))
34        .and_then(|v| v.as_table())
35    else {
36        return Vec::new();
37    };
38
39    let mut result: Vec<Alias> = aliases
40        .iter()
41        .filter_map(|(name, value)| {
42            value.as_str()?;
43
44            Some(Alias {
45                name: name.clone(),
46                command: format!("cargo {name}"),
47            })
48        })
49        .collect();
50
51    sort_by_name(&mut result);
52
53    vec![("aliases", to_value(&result))]
54}