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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use clap;
use toml::Value;
use serde::{Serialize, Deserialize};
use std::str::FromStr;
use std::error::Error;
use std::collections::BTreeMap;
use blockchain::Service;
use self::internal::NotFoundInMap;
pub use self::builder::NodeBuilder;
pub use self::details::{Run, Finalize, GenerateNodeConfig, GenerateCommonConfig, GenerateTestnet};
pub use self::shared::{AbstractConfig, NodePublicConfig, CommonConfigTemplate, NodePrivateConfig};
mod shared;
mod builder;
mod details;
mod internal;
mod clap_backend;
pub const DEFAULT_EXONUM_LISTEN_PORT: u16 = 6333;
pub type CommandName = &'static str;
#[derive(Clone, Copy, Debug)]
pub struct NamedArgument {
pub short_name: Option<&'static str>,
pub long_name: &'static str,
pub multiple: bool,
}
#[derive(Clone, Copy, Debug)]
pub enum ArgumentType {
Positional,
Named(NamedArgument),
}
#[derive(Clone, Copy, Debug)]
pub struct Argument {
pub name: &'static str,
pub argument_type: ArgumentType,
pub required: bool,
pub help: &'static str,
}
impl Argument {
pub fn new_named<T>(
name: &'static str,
required: bool,
help: &'static str,
short_name: T,
long_name: &'static str,
multiple: bool,
) -> Argument
where
T: Into<Option<&'static str>>,
{
Argument {
argument_type: ArgumentType::Named(NamedArgument {
short_name: short_name.into(),
long_name,
multiple,
}),
name,
help,
required,
}
}
pub fn new_positional(name: &'static str, required: bool, help: &'static str) -> Argument {
Argument {
argument_type: ArgumentType::Positional,
name,
help,
required,
}
}
}
#[derive(PartialEq, Debug, Clone, Default)]
pub struct Context {
args: BTreeMap<String, String>,
multiple_args: BTreeMap<String, Vec<String>>,
variables: BTreeMap<String, Value>,
}
impl Context {
fn new_from_args(args: &[Argument], matches: &clap::ArgMatches) -> Context {
let mut context = Context::default();
for arg in args {
match arg.argument_type {
ArgumentType::Named(detail) if detail.multiple => {
if let Some(values) = matches.values_of(&arg.name) {
let values: Vec<String> = values.map(|e| e.to_owned()).collect();
if context
.multiple_args
.insert(arg.name.to_owned(), values)
.is_some()
{
panic!("Duplicated argument: {}", arg.name);
}
continue;
}
}
_ => (),
};
if let Some(value) = matches.value_of(&arg.name) {
if context
.args
.insert(arg.name.to_owned(), value.to_string())
.is_some()
{
panic!("Duplicated argument: {}", arg.name);
}
} else if arg.required {
panic!("Required argument is not found: {}", arg.name)
}
}
context
}
pub fn arg<T: FromStr>(&self, key: &str) -> Result<T, Box<Error>>
where
<T as FromStr>::Err: Error + 'static,
{
if let Some(v) = self.args.get(key) {
Ok(v.parse()?)
} else {
Err(Box::new(NotFoundInMap))
}
}
pub fn arg_multiple<T: FromStr>(&self, key: &str) -> Result<Vec<T>, Box<Error>>
where
<T as FromStr>::Err: Error + 'static,
{
if let Some(values) = self.multiple_args.get(key) {
values.iter().map(|v| Ok(v.parse()?)).collect()
} else {
Err(Box::new(NotFoundInMap))
}
}
pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T, Box<Error>> {
if let Some(v) = self.variables.get(key) {
Ok(v.clone().try_into()?)
} else {
Err(Box::new(NotFoundInMap))
}
}
pub fn set<T: Serialize>(&mut self, key: &'static str, value: T) -> Option<Value> {
let value: Value = Value::try_from(value).expect("could not convert value into toml");
self.variables.insert(key.to_owned(), value)
}
}
pub trait CommandExtension {
fn args(&self) -> Vec<Argument>;
fn execute(&self, context: Context) -> Result<Context, Box<Error>>;
}
pub trait ServiceFactory: 'static {
#[allow(unused_variables)]
fn command(&mut self, command: CommandName) -> Option<Box<CommandExtension>> {
None
}
fn make_service(&mut self, run_context: &Context) -> Box<Service>;
}