vector_vrl_doc_builder/
main.rs

1use anyhow::Result;
2use std::path::PathBuf;
3use vrl::docs::{build_functions_doc, document_functions_to_dir};
4
5/// Generate Vector-specific VRL function documentation as JSON files.
6///
7/// Two modes of operation:
8///   --output <DIR>              Write one JSON file per function into DIR (uses --extension).
9///   (no --output)               Print all functions to stdout as a JSON array (uses --minify).
10#[derive(clap::Parser, Debug)]
11#[command()]
12struct Cli {
13    /// Output directory to create JSON files. When omitted, output is written to stdout as a JSON
14    /// array.
15    #[arg(short, long, conflicts_with = "minify")]
16    output: Option<PathBuf>,
17
18    /// Whether to minify the JSON output (stdout mode only)
19    #[arg(short, long, default_value_t = false, conflicts_with = "output")]
20    minify: bool,
21
22    /// File extension for generated files (directory mode only)
23    #[arg(short, long, default_value = "json", requires = "output")]
24    extension: String,
25}
26
27#[allow(clippy::print_stdout)]
28fn main() -> Result<()> {
29    let cli = <Cli as clap::Parser>::parse();
30    let functions = vector_vrl_functions::all_without_vrl_stdlib();
31    if let Some(output) = &cli.output {
32        document_functions_to_dir(&functions, output, &cli.extension)?;
33    } else {
34        let built = build_functions_doc(&functions);
35        if cli.minify {
36            println!(
37                "{}",
38                serde_json::to_string(&built).expect("FunctionDoc serialization should not fail")
39            );
40        } else {
41            println!(
42                "{}",
43                serde_json::to_string_pretty(&built)
44                    .expect("FunctionDoc serialization should not fail")
45            );
46        }
47    }
48    Ok(())
49}