codeint_config/
cli_schema.rs1use std::collections::{BTreeMap, BTreeSet};
2
3use clap::{Arg, Command as ClapCommand, CommandFactory};
4use serde::Serialize;
5
6use crate::cli::Arguments;
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
9pub struct CliSchema {
10 pub name: String,
11 pub version: String,
12 pub description: String,
13 pub commands: Vec<CliCommandSchema>,
14 pub output_formats: Vec<OutputFormatSchema>,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
18pub struct CliCommandSchema {
19 pub name: String,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 pub description: Option<String>,
22 pub arguments: Vec<CliArgumentSchema>,
23 pub flags: Vec<CliFlagSchema>,
24 #[serde(skip_serializing_if = "Vec::is_empty")]
25 pub subcommands: Vec<CliCommandSchema>,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
29pub struct CliArgumentSchema {
30 pub name: String,
31 pub value_name: String,
32 pub required: bool,
33 #[serde(skip_serializing_if = "Option::is_none")]
34 pub description: Option<String>,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
38pub struct CliFlagSchema {
39 #[serde(skip_serializing_if = "Option::is_none")]
40 pub short: Option<String>,
41 #[serde(skip_serializing_if = "Option::is_none")]
42 pub long: Option<String>,
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub value_name: Option<String>,
45 pub takes_value: bool,
46 pub required: bool,
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub default_value: Option<String>,
49 #[serde(skip_serializing_if = "Vec::is_empty")]
50 pub possible_values: Vec<String>,
51 #[serde(skip_serializing_if = "Option::is_none")]
52 pub description: Option<String>,
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
56pub struct OutputFormatSchema {
57 pub name: String,
58 pub commands: Vec<String>,
59}
60
61pub fn cli_schema() -> CliSchema {
62 let root = Arguments::command();
63 let commands: Vec<CliCommandSchema> = root
64 .get_subcommands()
65 .filter(|sub| !sub.is_hide_set() && sub.get_name() != "help")
66 .map(describe_command)
67 .collect();
68 let output_formats = collect_output_formats(&commands);
69 CliSchema {
70 name: root.get_name().to_owned(),
71 version: root
72 .get_version()
73 .unwrap_or(env!("CARGO_PKG_VERSION"))
74 .to_owned(),
75 description: root
76 .get_about()
77 .map(ToString::to_string)
78 .unwrap_or_default(),
79 commands,
80 output_formats,
81 }
82}
83
84fn describe_command(command: &ClapCommand) -> CliCommandSchema {
85 let mut arguments = Vec::new();
86 let mut flags = Vec::new();
87 for arg in command.get_arguments().filter(|arg| !arg.is_hide_set()) {
88 if arg.is_positional() {
89 arguments.push(describe_argument(arg));
90 } else {
91 flags.push(describe_flag(arg));
92 }
93 }
94 let subcommands = command
95 .get_subcommands()
96 .filter(|sub| !sub.is_hide_set() && sub.get_name() != "help")
97 .map(describe_command)
98 .collect();
99 CliCommandSchema {
100 name: command.get_name().to_owned(),
101 description: command.get_about().map(ToString::to_string),
102 arguments,
103 flags,
104 subcommands,
105 }
106}
107
108fn describe_argument(arg: &Arg) -> CliArgumentSchema {
109 let name = arg.get_id().as_str().to_owned();
110 let value_name = first_value_name(arg).unwrap_or_else(|| name.to_ascii_uppercase());
111 CliArgumentSchema {
112 name,
113 value_name,
114 required: arg.is_required_set(),
115 description: arg.get_help().map(ToString::to_string),
116 }
117}
118
119fn describe_flag(arg: &Arg) -> CliFlagSchema {
120 let takes_value = arg.get_action().takes_values();
121 let value_name = takes_value.then(|| first_value_name(arg)).flatten();
122 let default_value = takes_value
123 .then(|| {
124 arg.get_default_values()
125 .first()
126 .and_then(|value| value.to_str())
127 .map(str::to_owned)
128 })
129 .flatten();
130 let possible_values = if takes_value {
131 arg.get_possible_values()
132 .into_iter()
133 .filter(|value| !value.is_hide_set())
134 .map(|value| value.get_name().to_owned())
135 .collect()
136 } else {
137 Vec::new()
138 };
139 CliFlagSchema {
140 short: arg.get_short().map(|short| format!("-{short}")),
141 long: arg.get_long().map(|long| format!("--{long}")),
142 value_name,
143 takes_value,
144 required: arg.is_required_set(),
145 default_value,
146 possible_values,
147 description: arg.get_help().map(ToString::to_string),
148 }
149}
150
151fn first_value_name(arg: &Arg) -> Option<String> {
152 arg.get_value_names()
153 .and_then(|names| names.first())
154 .map(|name| name.as_str().to_owned())
155}
156
157fn collect_output_formats(commands: &[CliCommandSchema]) -> Vec<OutputFormatSchema> {
158 let mut by_format: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
159 record_command_formats(commands, "", &mut by_format);
160 by_format
161 .into_iter()
162 .map(|(name, commands)| OutputFormatSchema {
163 name,
164 commands: commands.into_iter().collect(),
165 })
166 .collect()
167}
168
169fn record_command_formats(
170 commands: &[CliCommandSchema],
171 prefix: &str,
172 by_format: &mut BTreeMap<String, BTreeSet<String>>,
173) {
174 for command in commands {
175 let full_name = if prefix.is_empty() {
176 command.name.clone()
177 } else {
178 format!("{prefix} {}", command.name)
179 };
180 for flag in &command.flags {
181 if flag.long.as_deref() == Some("--format") && full_name != "init" {
182 for format in &flag.possible_values {
183 by_format
184 .entry(format.clone())
185 .or_default()
186 .insert(full_name.clone());
187 }
188 }
189 }
190 record_command_formats(&command.subcommands, &full_name, by_format);
191 }
192}