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
use std::{
collections::HashMap,
hash::{Hash, Hasher},
};
use crate::{ConfigOption, Source};
pub(super) fn parse_subcommand<T>(
args: impl Iterator<Item = String>,
am: &clap::ArgMatches,
) -> Result<Option<T>, crate::Error>
where
T: clap::Subcommand,
{
let subname = match am.subcommand_name() {
None => return Ok(None),
Some(name) => name,
};
let vars = args.collect::<Vec<_>>();
let (_, subcommand) = vars.split_at(
vars.iter()
.position(|arg| arg.to_lowercase() == subname)
.ok_or_else(|| {
crate::Error::ExternalError("not found subcommand name in std::env::args()".into())
})?,
);
let subcommand = [&["".to_string()], subcommand].concat();
#[derive(clap::Parser)]
struct Supporting<S: clap::Subcommand> {
#[clap(subcommand)]
inner: S,
}
<Supporting<T> as clap::Parser>::try_parse_from(subcommand)
.map(|supporting| Some(supporting.inner))
.map_err(|err| {
crate::Error::ExternalError(format!("wrong parsing in parse_subcommand: {err}"))
})
}
pub(super) fn find_field_in_table(
config: &HashMap<String, config::Value>,
table: Option<String>,
field_name: String,
) -> Result<Option<String>, crate::Error> {
let mut field_segs = deconstruct_table_path(&field_name).collect::<Vec<_>>();
let field = field_segs.pop().ok_or_else(|| {
crate::Error::FailedParse(format!("Empty path segments of the field: {field_name}"))
})?;
let sub_config = match table {
None => match find_sub_table(config, field_segs.into_iter())? {
None => return Ok(None),
Some(sub_config) => sub_config,
},
Some(table) => match find_sub_table(
config,
deconstruct_table_path(&table).chain(field_segs.into_iter()),
)? {
None => return Ok(None),
Some(sub_config) => sub_config,
},
};
if let Some(value) = sub_config.get(&field) {
from_config_to_string(value.clone()).map(Some)
} else {
Ok(None)
}
}
fn find_sub_table(
parent_config: &HashMap<String, config::Value>,
mut table: impl Iterator<Item = String>,
) -> Result<Option<&HashMap<String, config::Value>>, crate::Error> {
let first_segment = match table.next() {
None => return Ok(Some(parent_config)),
Some(seg) => seg,
};
match &parent_config.get(&first_segment) {
None => Ok(None),
Some(sub_table) => {
if let config::ValueKind::Table(sub_table) = &sub_table.kind {
find_sub_table(sub_table, table)
} else {
Err(crate::Error::FailedParse(format!(
"Field {first_segment} is found in configuration files but it is not a table"
)))
}
}
}
}
fn deconstruct_table_path(table: &str) -> impl Iterator<Item = String> + '_ {
table.split(|dot| dot == '.').map(ToString::to_string)
}
fn from_config_to_string(initial: config::Value) -> Result<String, super::Error> {
fn from_config_to_serde_json(
initial: config::ValueKind,
) -> Result<serde_json::Value, super::Error> {
match initial {
config::ValueKind::Nil => Ok(serde_json::Value::Null),
config::ValueKind::Boolean(b) => Ok(serde_json::Value::Bool(b)),
config::ValueKind::I64(i) => Ok(serde_json::Value::Number(
serde_json::value::Number::from(i),
)),
config::ValueKind::U64(u) => Ok(serde_json::Value::Number(
serde_json::value::Number::from(u),
)),
config::ValueKind::Float(f) => Ok(serde_json::Value::Number(
serde_json::value::Number::from_f64(f).ok_or_else(|| {
super::Error::FailedParse(format!(
"failed to convert to serde_json Number from from f64 {}",
f
))
})?,
)),
config::ValueKind::String(s) => Ok(serde_json::Value::String(s)),
config::ValueKind::Table(tbl) => {
Ok(serde_json::Value::Object(serde_json::Map::from_iter({
let mut res = vec![];
for (k, v) in tbl {
res.push((k, from_config_to_serde_json(v.kind)?));
}
res.into_iter()
})))
}
config::ValueKind::Array(arr) => Ok(serde_json::Value::Array({
let mut res = vec![];
for v in arr {
res.push(from_config_to_serde_json(v.kind)?);
}
res
})),
config::ValueKind::I128(num) => Ok(serde_json::Value::Number(
::std::str::FromStr::from_str(&num.to_string()).map_err(|_| {
super::Error::FailedParse(format!(
"can't convert to serde_json::value::Number from I128, value: {})",
num
))
})?,
)),
config::ValueKind::U128(num) => Ok(serde_json::Value::Number(
::std::str::FromStr::from_str(&num.to_string()).map_err(|_| {
super::Error::FailedParse(format!(
"can't convert to serde_json::value::Number from U128, value: {})",
num
))
})?,
)),
}
}
match initial.kind {
config::ValueKind::I128(num) => Ok(num.to_string()),
config::ValueKind::U128(num) => Ok(num.to_string()),
value => Ok(from_config_to_serde_json(value).map(|value| value.to_string())?),
}
}
impl PartialEq for ConfigOption {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(ConfigOption::EnvPrefix(_), ConfigOption::EnvPrefix(_))
| (
ConfigOption::ExplicitSource(Source::Clap(_)),
ConfigOption::ExplicitSource(Source::Clap(_)),
)
| (
ConfigOption::ExplicitSource(Source::ConfigFiles(_)),
ConfigOption::ExplicitSource(Source::ConfigFiles(_)),
)
| (
ConfigOption::ExplicitSource(Source::Env(_)),
ConfigOption::ExplicitSource(Source::Env(_)),
)
)
}
}
impl Eq for ConfigOption {}
impl Hash for ConfigOption {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
ConfigOption::EnvPrefix(_) => state.write_u8(1),
ConfigOption::ExplicitSource(Source::Clap(_)) => state.write_u8(2),
ConfigOption::ExplicitSource(Source::ConfigFiles(_)) => state.write_u8(3),
ConfigOption::ExplicitSource(Source::Env(_)) => state.write_u8(4),
}
}
}