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
//! A module containing output generator for the JSON Schema & UI Object
use std::collections::HashMap;

use serde_derive::Serialize;

use crate::dsl::schema::Schema;
use crate::dsl::schema::Widget;
use crate::dsl::schema::KeysSchema;

pub mod generator;
mod serialization;

/// JSON Schema wrapper
pub struct JsonSchema<'a> {
    schema_url: &'a str,
    root: Schema,
}

/// It's different than UiObject as the root is nameless in the output
#[derive(Clone, Debug, Serialize)]
pub struct UiObjectRoot(Option<UiObjectProperty>);

/// UI Object wrapper
#[derive(Clone, Debug, Serialize)]
pub struct UiObject(HashMap<String, UiObjectProperty>);

#[derive(Clone, Debug, PartialEq, Serialize)]
struct UiOptions {
    removable: bool,
    addable: bool,
    orderable: bool,
}

#[derive(Clone, Debug, Serialize)]
struct UiObjectProperty {
    #[serde(rename = "ui:help", skip_serializing_if = "Option::is_none")]
    help: Option<String>,
    #[serde(rename = "ui:warning", skip_serializing_if = "Option::is_none")]
    warning: Option<String>,
    #[serde(rename = "ui:description", skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(rename = "ui:placeholder", skip_serializing_if = "Option::is_none")]
    placeholder: Option<String>,
    #[serde(rename = "ui:widget", skip_serializing_if = "Option::is_none")]
    widget: Option<Widget>,
    #[serde(flatten)]
    properties: Option<UiObject>,
    #[serde(flatten)]
    keys: Option<KeysSchema>,
    #[serde(rename = "ui:options", skip_serializing_if = "Option::is_none")]
    ui_options: Option<UiOptions>,
    #[serde(rename = "ui:readonly", skip_serializing_if = "Option::is_none")]
    readonly: Option<bool>,
    #[serde(rename = "ui:order", skip_serializing_if = "Option::is_none")]
    order: Option<Vec<String>>,
}

impl UiObjectProperty {
    /// Checks if an UI Object is empty
    // FIXME: this is hard to maintain - need to remember to add things here
    pub fn is_empty(&self) -> bool {
        self.help.is_none()
            && self.warning.is_none()
            && self.description.is_none()
            && self.widget.is_none()
            && self.properties.is_none()
            && self.keys.is_none()
            && self.ui_options.is_none()
            && self.placeholder.is_none()
            && self.readonly.is_none()
    }
}

impl UiObjectRoot {
    pub fn is_empty(&self) -> bool {
        match &self.0 {
            None => true,
            Some(property) => property.is_empty(),
        }
    }
}

impl UiObject {
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl Default for UiOptions {
    fn default() -> Self {
        UiOptions {
            removable: true,
            addable: true,
            orderable: true,
        }
    }
}