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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use heck::MixedCase;
use regex::Regex;
use serde::de::Error;
use serde_yaml::Mapping;
use serde_yaml::Value;

use crate::dsl::schema::Annotations;
use crate::dsl::schema::deserialization::deserialize_schema;
use crate::dsl::schema::object_types::bounds::ArrayItemObjectBounds;
use crate::dsl::schema::object_types::bounds::ArrayObjectBounds;
use crate::dsl::schema::object_types::bounds::ArrayUniqueItemBound;
use crate::dsl::schema::object_types::bounds::BooleanObjectBounds;
use crate::dsl::schema::object_types::bounds::EnumerationValue;
use crate::dsl::schema::object_types::bounds::IntegerBound;
use crate::dsl::schema::object_types::bounds::IntegerObjectBounds;
use crate::dsl::schema::object_types::bounds::StringLength;
use crate::dsl::schema::object_types::bounds::StringObjectBounds;
use crate::dsl::schema::object_types::deserialization::deserialize_integer;
use crate::dsl::schema::Schema;

pub fn deserialize_string_object_bounds<E>(mapping: &Mapping) -> Result<Option<StringObjectBounds>, E>
where
    E: Error,
{
    let enumeration_values = deserialize_enumeration_values(&mapping)?;
    let constant_value = deserialize_constant_value(&mapping)?.map(|value| vec![value]);
    let pattern = deserialize_pattern(&mapping)?;
    let length = deserialize_length_bounds(&mapping)?;

    if (enumeration_values.is_some()) && constant_value.is_some() {
        return Err(Error::custom("cannot have both enum and const defined"));
    }

    let possible_values = enumeration_values.or(constant_value);

    if possible_values.is_some() && pattern.is_some() {
        return Err(Error::custom("cannot have both pattern set and enum/const bound"));
    }

    if possible_values.is_some() && length.is_some() {
        return Err(Error::custom("cannot have both length set and enum/const bound"));
    }

    let possible_values = possible_values.map(|mut list| {
        list.iter_mut()
            .map(|value| {
                if value.annotations.title.is_none() {
                    value.annotations.title = Some(value.value.clone());
                }
                value.clone()
            })
            .collect()
    });

    let result = {
        if let Some(values) = possible_values {
            Some(StringObjectBounds::PossibleValues(values))
        } else if let Some(pattern) = pattern {
            Some(StringObjectBounds::Pattern(pattern))
        } else if let Some(length) = length {
            Some(length)
        } else {
            None
        }
    };

    Ok(result)
}

pub fn deserialize_length_bounds<E>(mapping: &Mapping) -> Result<Option<StringObjectBounds>, E>
where
    E: Error,
{
    let max_length = deserialize_integer("maxLength", &mapping)?;
    let min_length = deserialize_integer("minLength", &mapping)?;
    if max_length.is_some() || min_length.is_some() {
        Ok(Some(StringObjectBounds::Length(StringLength {
            minimum: min_length,
            maximum: max_length,
        })))
    } else {
        Ok(None)
    }
}

pub fn deserialize_integer_bounds<E>(mapping: &Mapping) -> Result<Option<IntegerObjectBounds>, E>
where
    E: Error,
{
    let maximum = deserialize_integer_bound("maximum", mapping)?;
    let minimum = deserialize_integer_bound("minimum", mapping)?;
    let multiple_of = deserialize_integer("multipleOf", mapping)?;

    if maximum.is_some() || minimum.is_some() || multiple_of.is_some() {
        Ok(Some(IntegerObjectBounds {
            minimum,
            maximum,
            multiple_of,
        }))
    } else {
        Ok(None)
    }
}

pub fn deserialize_boolean_object_bounds<E>(mapping: &Mapping) -> Result<Option<BooleanObjectBounds>, E>
where
    E: Error,
{
    let default_key = Value::from("default");

    match mapping.get(&default_key) {
        Some(default) => match default {
            Value::Bool(value) => Ok(Some(BooleanObjectBounds::DefaultValue(*value))),
            _ => Err(Error::custom(format!(
                "cannot deserialize default value - {:#?} is not a boolean",
                default
            ))),
        },
        None => Ok(None),
    }
}

pub fn deserialize_array_object_bounds<E>(mapping: &Mapping) -> Result<Option<ArrayObjectBounds>, E>
where
    E: Error,
{
    let maximum_number_of_items = deserialize_integer("maxItems", mapping)?;
    let minimum_number_of_items = deserialize_integer("minItems", mapping)?;
    let items = deserialize_array_item_bounds(mapping)?;
    let unique_items = deserialize_array_unique_items_bounds(mapping)?;
    let additional_items = deserialize_array_additional_items_bounds(mapping)?;

    if maximum_number_of_items.is_some() || minimum_number_of_items.is_some() || items.is_some() {
        return Ok(Some(ArrayObjectBounds {
            minimum_number_of_items,
            maximum_number_of_items,
            items,
            unique_items,
            additional_items,
        }));
    }
    Ok(None)
}

fn deserialize_array_additional_items_bounds<E>(mapping: &Mapping) -> Result<Option<Schema>, E>
where
    E: Error,
{
    match mapping.get(&Value::from("additionalItems")) {
        None => Ok(None),
        Some(properties) => match properties {
            Value::Mapping(_) => Ok(Some(deserialize_schema(properties)?)),
            _ => Err(Error::custom("`additionalItems must be a schema`")),
        },
    }
}

fn deserialize_array_unique_items_bounds<E>(mapping: &Mapping) -> Result<Option<ArrayUniqueItemBound>, E>
where
    E: Error,
{
    match mapping.get(&Value::from("uniqueItems")) {
        None => Ok(None),
        Some(items) => match items {
            Value::Bool(value) => {
                if *value {
                    Ok(Some(ArrayUniqueItemBound::All))
                } else {
                    Ok(None)
                }
            }
            Value::Sequence(sequence) => {
                let names = sequence
                    .iter()
                    .map(|item| match item {
                        Value::String(name) => Ok(name.to_string()),
                        _ => Err(Error::custom(
                            "`uniqueItems` entry cannot be anything else than a string",
                        )),
                    })
                    .collect::<Result<Vec<String>, E>>();
                Ok(Some(ArrayUniqueItemBound::Specific(names?)))
            }
            _ => Err(Error::custom(format!(
                "unsupported shape of the `uniqueItems` {:#?}",
                items
            ))),
        },
    }
}

fn deserialize_array_item_bounds<E>(mapping: &Mapping) -> Result<Option<ArrayItemObjectBounds>, E>
where
    E: Error,
{
    match mapping.get(&Value::from("items")) {
        None => Ok(None),
        Some(properties) => match properties {
            Value::Mapping(_) => Ok(Some(ArrayItemObjectBounds::AllItems(Box::new(deserialize_schema(
                properties,
            )?)))),
            Value::Sequence(sequence) => Ok(Some(ArrayItemObjectBounds::RespectiveItems(
                sequence
                    .iter()
                    .map(|entry| deserialize_schema(entry))
                    .collect::<Result<Vec<Schema>, E>>()?,
            ))),
            _ => Err(Error::custom("`items` must be either a schema or array of schemas")),
        },
    }
}

fn deserialize_enumeration_values<E>(mapping: &Mapping) -> Result<Option<Vec<EnumerationValue>>, E>
where
    E: Error,
{
    let enum_key = Value::from("enum");
    match mapping.get(&enum_key) {
        Some(mapping) => match mapping {
            Value::Sequence(sequence) => {
                let result = sequence
                    .iter()
                    .map(|definition| enumeration_definition_to_enumeration_value(definition))
                    .collect::<Result<Vec<EnumerationValue>, E>>()?;
                if !result.is_empty() {
                    Ok(Some(result))
                } else {
                    Ok(None)
                }
            }
            _ => Err(Error::custom(format!("enum `{:#?}` is not a sequence", mapping))),
        },
        None => Ok(None),
    }
}

fn deserialize_pattern<E>(mapping: &Mapping) -> Result<Option<Regex>, E>
where
    E: Error,
{
    let pattern_key = Value::from("pattern");
    match mapping.get(&pattern_key) {
        Some(pattern) => match pattern {
            Value::String(string) => {
                Ok(Some(Regex::new(string).map_err(|e| {
                    Error::custom(format!("pattern `{:?}` is not a regex - {}", pattern, e))
                })?))
            }
            _ => Err(Error::custom(format!("pattern `{:#?}` must be a string", pattern))),
        },
        None => Ok(None),
    }
}

fn deserialize_constant_value<E>(mapping: &Mapping) -> Result<Option<EnumerationValue>, E>
where
    E: Error,
{
    let constant_key = Value::from("const");
    let value = mapping.get(&constant_key).map_or(Ok(None), |value| {
        serde_yaml::from_value(value.clone())
            .map_err(|e| Error::custom(format!("cannot deserialize constant specifier: {:?} - {}", value, e)))
    })?;
    let annotations = Annotations {
        title: None,
        help: None,
        warning: None,
        description: None,
    };
    match value {
        None => Ok(None),
        Some(value) => Ok(Some(EnumerationValue { value, annotations })),
    }
}

fn enumeration_definition_to_enumeration_value<E>(definition: &Value) -> Result<EnumerationValue, E>
where
    E: Error,
{
    match definition {
        Value::String(string) => Ok(string.as_str().into()),
        Value::Mapping(mapping) => Ok(mapping_to_enumeration_value(mapping)?),
        _ => Err(Error::custom(format!("no idea how to deserialize {:#?}", definition))),
    }
}

fn mapping_to_enumeration_value<E>(mapping: &Mapping) -> Result<EnumerationValue, E>
where
    E: Error,
{
    let value = mapping
        .get(&Value::from("value"))
        .map(|value| match value {
            Value::String(string) => Ok(string),
            _ => Err(Error::custom(format!("enum value `{:#?}` must be a string", value))),
        })
        .ok_or_else(|| Error::custom("when the enumeration is a mapping - expected 'value' to be present"))??;

    let title = mapping.get(&Value::from("title")).map(|value| match value {
        Value::String(string) => Ok(string),
        _ => Err(Error::custom(format!("enum title `{:#?}` must be a string", value))),
    });

    let title = match title {
        None => None,
        Some(result) => match result {
            Ok(value) => Some(value.to_string()),
            Err(e) => return Err(e),
        },
    };

    let annotations = Annotations {
        title,
        help: None,
        warning: None,
        description: None,
    };
    Ok(EnumerationValue {
        annotations,
        value: value.to_string(),
    })
}

fn deserialize_integer_bound<E>(name: &str, mapping: &Mapping) -> Result<Option<IntegerBound>, E>
where
    E: Error,
{
    let normal = deserialize_integer(name, mapping)?;
    let exclusive = deserialize_integer(&("exclusive ".to_string() + name).to_mixed_case(), mapping)?;
    if normal.is_some() && exclusive.is_some() {
        return Err(Error::custom("cannot have both {} and exclusive {} set"));
    }
    if let Some(normal) = normal {
        return Ok(Some(IntegerBound::Inclusive(normal)));
    }
    if let Some(exclusive) = exclusive {
        return Ok(Some(IntegerBound::Exclusive(exclusive)));
    }
    Ok(None)
}