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
use core::borrow::Borrow;
use std::collections::HashMap;

use crate::dsl::schema::DocumentRoot;
use crate::dsl::schema::Schema;
use crate::dsl::schema::SchemaList;
use crate::output::UiObject;
use crate::output::UiObjectProperty;

impl<'a> From<&'a DocumentRoot> for UiObject<'a> {
    fn from(schema: &'a DocumentRoot) -> Self {
        match &schema.schema {
            Some(schema) => match &schema.children {
                Some(list) => list.into(),
                None => UiObject(HashMap::new()),
            },
            None => UiObject(HashMap::new()),
        }
    }
}

impl<'a> From<&'a SchemaList> for UiObject<'a> {
    fn from(list: &'a SchemaList) -> Self {
        let ui_object_entries: Vec<(&str, UiObjectProperty)> = list
            .entries()
            .iter()
            .filter_map(|entry| {
                let property: UiObjectProperty = entry.schema.borrow().into();
                if !property.is_empty() {
                    Some((entry.name.as_str(), entry.schema.borrow().into()))
                } else {
                    None
                }
            })
            .collect();

        let ui_object_entries: HashMap<&str, UiObjectProperty> = ui_object_entries.into_iter().collect();
        UiObject(ui_object_entries)
    }
}

impl<'a> From<&'a Schema> for UiObjectProperty<'a> {
    fn from(property: &'a Schema) -> Self {
        UiObjectProperty {
            help: property.annotations.help.as_ref().map(|string| string.as_ref()),
            warning: property.annotations.warning.as_ref().map(|string| string.as_str()),
            description: property.annotations.description.as_ref().map(|string| string.as_ref()),
        }
    }
}