gen_completions/lib.rs
1//! This is a library for generating completions either by parsing manpages or
2//! from KDL or JSON config files. If you're looking for the CLI tool, head to
3//! <https://crates.io/crates/gen-completions>
4//!
5//! The [`parse_man`] module parses manpages, while the [`parse_deser`] module
6//! deserializes a KDL or JSON file to get command information. Both produce
7//! [`CommandInfo`]s that can then be used to generate shell completions using
8//! the [`gen`] module.
9
10pub mod gen;
11pub mod parse_deser;
12pub mod parse_man;
13
14use serde::{Deserialize, Serialize};
15
16/// Flags parsed from a command, as well as its parsed subcommands
17#[derive(Debug, Deserialize, Eq, Serialize, PartialEq)]
18pub struct CommandInfo {
19 pub name: String,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 pub desc: Option<String>,
22 #[serde(skip_serializing_if = "Vec::is_empty")]
23 pub flags: Vec<Flag>,
24 /// The types of the arguments to this command
25 #[serde(default, skip_serializing_if = "Vec::is_empty")]
26 pub args: Vec<ArgType>,
27 #[serde(default, skip_serializing_if = "Vec::is_empty")]
28 pub subcommands: Vec<CommandInfo>,
29}
30
31/// A parsed flag
32#[derive(Debug, Deserialize, Eq, Serialize, PartialEq)]
33pub struct Flag {
34 /// The different short and long forms of a flag
35 pub forms: Vec<String>,
36 /// Optional description for the flag
37 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub desc: Option<String>,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub typ: Option<ArgType>,
41}
42
43/// How to complete an argument
44#[derive(Clone, Debug, Deserialize, Eq, Serialize, PartialEq)]
45pub enum ArgType {
46 /// Complete using either file or directory paths
47 Path,
48
49 /// Complete using directory paths
50 Dir,
51
52 /// Complete by running a command
53 Run {
54 /// The command to run
55 cmd: String,
56 /// The separator to split on to get the value (first) and description
57 /// (second). If none, assumed to only return values
58 sep: Option<String>,
59 },
60
61 /// Only these strings are allowed. The second part of each tuple is an
62 /// optional description
63 Strings(Vec<(String, Option<String>)>),
64
65 /// Complete with the name of a command
66 CommandName,
67
68 /// Any of the given types work
69 Any(Vec<ArgType>),
70
71 Unknown,
72}