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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use super::{NumberType, PrimitiveType, Value};
use strum::ParseError;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum ValueType {
List(Box<ValueType>),
Map(Box<ValueType>),
Optional(Box<ValueType>),
Primitive(PrimitiveType),
Text,
Custom,
}
impl ValueType {
pub fn is_primitive_type(&self) -> bool {
matches!(self, Self::Primitive(_))
}
pub fn to_primitive_type(&self) -> Option<PrimitiveType> {
match self {
Self::Primitive(x) => Some(*x),
_ => None,
}
}
pub fn from_type_name(name: &str) -> Result<Self, ParseError> {
if name.is_empty() {
return Err(ParseError::VariantNotFound);
}
let mut tokens = name.split(|c| c == '<');
let maybe_outer_str = tokens.next();
let inner_str = {
let mut x = tokens.collect::<Vec<&str>>().join("<");
if x.ends_with('>') {
x.pop();
}
x
};
match maybe_outer_str
.unwrap()
.split(|c| c == ':')
.last()
.unwrap()
.to_lowercase()
.as_str()
{
"hashmap" | "btreemap" => {
let mut items = inner_str.split(|c| c == ',');
if let Some(s) = items.next() {
if s.trim().to_lowercase().as_str() != "string" {
return Err(ParseError::VariantNotFound);
}
}
let rest = items.collect::<String>();
Ok(ValueType::Map(Box::from(Self::from_type_name(
&rest.trim(),
)?)))
}
"vec" | "vecdeque" | "linkedlist" | "hashset" | "btreeset" | "binaryheap" => Ok(
ValueType::List(Box::from(Self::from_type_name(&inner_str)?)),
),
"option" => Ok(ValueType::Optional(Box::from(Self::from_type_name(
&inner_str,
)?))),
"string" => Ok(ValueType::Text),
x => Ok(ValueType::Primitive(PrimitiveType::from_type_name(x)?)),
}
}
}
impl Default for ValueType {
fn default() -> Self {
Self::Primitive(Default::default())
}
}
impl std::str::FromStr for ValueType {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn opt_to_err(maybe_type: Option<ValueType>) -> Result<ValueType, ParseError> {
match maybe_type {
Some(t) => Ok(t),
None => Err(ParseError::VariantNotFound),
}
}
fn from_tokens<'a>(
mut it: impl Iterator<Item = &'a str>,
) -> Result<Option<ValueType>, ParseError> {
match it.next() {
Some("number") => from_tokens(it),
Some(token) => {
let maybe_inner = from_tokens(it)?;
match token {
"list" => Ok(Some(ValueType::List(Box::from(opt_to_err(maybe_inner)?)))),
"map" => Ok(Some(ValueType::Map(Box::from(opt_to_err(maybe_inner)?)))),
"optional" => Ok(Some(ValueType::Optional(Box::from(opt_to_err(
maybe_inner,
)?)))),
"primitive" => Ok(Some(ValueType::Primitive(
opt_to_err(maybe_inner)?
.to_primitive_type()
.ok_or(ParseError::VariantNotFound)?,
))),
"text" => Ok(Some(ValueType::Text)),
x => Ok(Some(ValueType::Primitive(PrimitiveType::from_str(x)?))),
}
}
None => Ok(None),
}
}
match from_tokens(s.split(':')) {
Ok(Some(value_type)) => Ok(value_type),
Ok(None) => Err(ParseError::VariantNotFound),
Err(x) => Err(x),
}
}
}
impl std::fmt::Display for ValueType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::List(t) => write!(f, "list:{}", t),
Self::Map(t) => write!(f, "map:{}", t),
Self::Optional(t) => write!(f, "optional:{}", t),
Self::Primitive(t) => write!(f, "{}", t),
Self::Text => write!(f, "text"),
Self::Custom => write!(f, "custom"),
}
}
}
impl From<Value> for ValueType {
fn from(value: Value) -> Self {
Self::from(&value)
}
}
impl<'a> From<&'a Value> for ValueType {
fn from(v: &'a Value) -> Self {
match v {
Value::List(x) => Self::List(Box::from(
x.iter().next().map(ValueType::from).unwrap_or_default(),
)),
Value::Map(x) => Self::Map(Box::from(
x.values().next().map(ValueType::from).unwrap_or_default(),
)),
Value::Optional(x) => Self::Optional(Box::from(
x.as_ref()
.map(Box::as_ref)
.map(ValueType::from)
.unwrap_or_default(),
)),
Value::Primitive(x) => Self::Primitive(PrimitiveType::from(x)),
Value::Text(_) => Self::Text,
}
}
}
impl From<PrimitiveType> for ValueType {
fn from(t: PrimitiveType) -> Self {
Self::Primitive(t)
}
}
impl From<NumberType> for ValueType {
fn from(t: NumberType) -> Self {
Self::Primitive(PrimitiveType::Number(t))
}
}