Skip to main content

intent_render/
lib.rs

1pub mod format;
2pub mod html;
3pub mod markdown;
4
5use intent_parser::ast;
6
7/// Format a [`Literal`] as a human-readable string.
8pub fn format_literal(lit: &ast::Literal) -> String {
9    match lit {
10        ast::Literal::Null => "null".to_string(),
11        ast::Literal::Bool(b) => b.to_string(),
12        ast::Literal::Int(n) => n.to_string(),
13        ast::Literal::Decimal(s) => s.clone(),
14        ast::Literal::String(s) => format!("\"{}\"", s),
15    }
16}
17
18/// Format a [`TypeExpr`] as a human-readable string.
19pub fn format_type(ty: &ast::TypeExpr) -> String {
20    let base = match &ty.ty {
21        ast::TypeKind::Simple(name) => name.clone(),
22        ast::TypeKind::Union(variants) => variants
23            .iter()
24            .map(|v| match v {
25                ast::TypeKind::Simple(n) => n.clone(),
26                _ => "...".to_string(),
27            })
28            .collect::<Vec<_>>()
29            .join(" | "),
30        ast::TypeKind::List(inner) => format!("List<{}>", format_type(inner)),
31        ast::TypeKind::Set(inner) => format!("Set<{}>", format_type(inner)),
32        ast::TypeKind::Map(k, v) => format!("Map<{}, {}>", format_type(k), format_type(v)),
33        ast::TypeKind::Parameterized { name, params } => {
34            let ps: Vec<String> = params
35                .iter()
36                .map(|p| format!("{}: {}", p.name, format_literal(&p.value)))
37                .collect();
38            format!("{}({})", name, ps.join(", "))
39        }
40    };
41    if ty.optional {
42        format!("{}?", base)
43    } else {
44        base
45    }
46}