1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::Reference;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub const VERSION: &str = "0.2.1";
#[allow(missing_docs)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Document<'a> {
pub openautocompletion: OpenAutoCompletion,
pub components: Components,
pub cli: Cli<'a>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct OpenAutoCompletion {
pub version: String,
}
impl Default for OpenAutoCompletion {
fn default() -> Self {
Self {
version: String::from(VERSION),
}
}
}
#[allow(missing_docs)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Components {
pub arguments: std::option::Option<HashMap<String, Argument>>,
pub options: std::option::Option<HashMap<String, Option>>,
pub commands: std::option::Option<HashMap<String, Command>>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Argument {
pub name: String,
pub description: std::option::Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Option {
pub names_short: std::option::Option<Vec<String>>,
pub names_long: std::option::Option<Vec<String>>,
pub description: std::option::Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Command {
pub names: Vec<String>,
pub description: std::option::Option<String>,
}
#[allow(missing_docs)]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum Pattern<'a> {
Command {
command: Reference<'a, Command>,
#[serde(flatten)]
common: PatternCommon,
},
Argument {
argument: Reference<'a, Argument>,
#[serde(flatten)]
common: PatternCommon,
},
Option {
option: Reference<'a, Option>,
argument: std::option::Option<Box<Pattern<'a>>>,
#[serde(flatten)]
common: PatternCommon,
},
Group {
#[serde(default)]
exclusive: bool,
patterns: Vec<Pattern<'a>>,
#[serde(flatten)]
common: PatternCommon,
},
}
#[derive(Debug, Deserialize, Serialize)]
pub struct PatternCommon {
#[serde(default)]
pub repeated: bool,
#[serde(default)]
pub optional: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Cli<'a> {
pub name: String,
#[serde(default = "default_option_separators_long")]
pub option_separators_long: Vec<String>,
#[serde(default = "default_option_separators_short")]
pub option_separators_short: Vec<String>,
#[serde(default = "default_option_prefix_long")]
pub option_prefix_long: String,
#[serde(default = "default_option_prefix_short")]
pub option_prefix_short: String,
pub pattern_groups: Vec<Pattern<'a>>,
}
fn default_option_separators_long() -> Vec<String> {
vec!["=".into(), " ".into()]
}
fn default_option_separators_short() -> Vec<String> {
vec![" ".into(), "".into()]
}
fn default_option_prefix_long() -> String {
String::from("--")
}
fn default_option_prefix_short() -> String {
String::from("-")
}