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
use crate::VecStringWrapper;
use darling::FromMeta;
use proc_macro_error::abort;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Default, FromMeta, Clone, Serialize, Deserialize)]
#[darling(default)]
pub struct ApiInputConfig {
pub arg_name: Option<String>,
pub ty: Option<String>,
pub long: Option<String>,
pub short: Option<char>,
pub no_short: Option<bool>,
pub heading: Option<String>,
pub help: Option<String>,
pub long_help: Option<String>,
pub possible_values: Option<VecStringWrapper>,
pub required: Option<bool>,
pub num_args: Option<String>,
}
lazy_static! {
static ref CONFIGMAP: HashMap<String, ApiInputConfig> = {
let mut m = HashMap::new();
m.insert(
"output_file".into(),
ApiInputConfig {
arg_name: Some("output_file".into()),
ty: Some("String".into()),
long: Some("output".into()),
short:Some( 'o'),
help:Some( "Output file. (default: stdout)".into()),
long_help: Some( "Output file to save the result in. (default: stdout)".into()),
heading: Some("Options".into()),
required:Some(false),
..Default::default()
},
);
m.insert(
"input_file".into(),
ApiInputConfig {
arg_name: Some("input_file".into()),
ty: Some("String".into()),
long: Some("input".into()),
short: Some('i'),
help: Some("Read the data from file ('-' for stdin)".into()),
long_help: Some("Read the data from a JSON file ('-' for stdin)".into()),
heading: Some("Options".into()),
possible_values: None,
required:Some(false),
..Default::default()
},
);
m.insert(
"input_template".into(),
ApiInputConfig {
arg_name: Some("input_template".into()),
ty: Some("bool".into()),
num_args:None,
long: Some("template".into()),
short: Some('t'),
help: Some("Generate an input template".into()),
long_help: Some("Generate an input template to use with the --input option".into()),
heading: Some("Options".into()),
possible_values: None,
required:Some(false),
..Default::default()
},
);
m.insert(
"output_format".into(),
ApiInputConfig {
arg_name: Some( "output_format".into()),
long: Some("format".into()),
short: Some('f'),
heading: Some("Formatting".into()),
..Default::default()
},
);
m
};
}
#[allow(dead_code)]
pub fn arg_config(k: &str, local_config: &[ApiInputConfig]) -> ApiInputConfig {
let global = CONFIGMAP.get(k);
let local = local_config
.iter()
.find(|&c| k == c.arg_name.as_ref().unwrap());
match (global, local) {
(None, None) => abort!(k, format!("Can't find '{}' configuration", k)),
(None, Some(l)) => l.to_owned(),
(Some(g), None) => g.to_owned(),
(Some(g), Some(l)) => ApiInputConfig {
arg_name: l.arg_name.to_owned().or_else(|| g.arg_name.to_owned()),
ty: l.ty.to_owned().or_else(|| g.ty.to_owned()),
long: l.long.to_owned().or_else(|| g.long.to_owned()),
short: l.short.to_owned().or_else(|| g.short.to_owned()),
no_short: l.no_short.to_owned().or_else(|| g.no_short.to_owned()),
heading: l.heading.to_owned().or_else(|| g.heading.to_owned()),
help: l.help.to_owned().or_else(|| g.help.to_owned()),
long_help: l.long_help.to_owned().or_else(|| g.long_help.to_owned()),
possible_values: l
.possible_values
.to_owned()
.or_else(|| g.possible_values.to_owned()),
required: l.required.to_owned().or_else(|| g.required.to_owned()),
num_args: l.num_args.to_owned().or_else(|| g.num_args.to_owned()),
},
}
}