vrl/docs/
cmd.rs

1use crate::compiler::Function;
2use clap::Parser;
3use std::io;
4use std::path::PathBuf;
5
6use super::{build_functions_doc, document_functions_to_dir};
7
8/// Vector Remap Language Docs
9#[derive(Parser, Debug)]
10#[command(name = "VRL", about)]
11pub struct Opts {
12    /// Output directory to create JSON files. If unspecified output is written to stdout as a JSON
13    /// array
14    #[arg(short, long)]
15    output: Option<PathBuf>,
16
17    /// Whether to pretty-print or minify
18    #[arg(short, long, default_value_t = false)]
19    minify: bool,
20
21    /// File extension for generated files
22    #[arg(short, long, default_value = "json")]
23    extension: String,
24}
25
26#[must_use]
27pub fn docs(opts: &Opts, functions: &[Box<dyn Function>]) -> exitcode::ExitCode {
28    match run(opts, functions) {
29        Ok(()) => exitcode::OK,
30        Err(err) => {
31            #[allow(clippy::print_stderr)]
32            {
33                eprintln!("{err}");
34            }
35            exitcode::SOFTWARE
36        }
37    }
38}
39
40fn run(opts: &Opts, functions: &[Box<dyn Function>]) -> Result<(), io::Error> {
41    if let Some(output) = &opts.output {
42        document_functions_to_dir(functions, output, &opts.extension)
43    } else {
44        let built = build_functions_doc(functions);
45        #[allow(clippy::print_stdout)]
46        if opts.minify {
47            println!(
48                "{}",
49                serde_json::to_string(&built).expect("FunctionDoc serialization should not fail")
50            );
51        } else {
52            println!(
53                "{}",
54                serde_json::to_string_pretty(&built)
55                    .expect("FunctionDoc serialization should not fail")
56            );
57        }
58        Ok(())
59    }
60}