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
use std::collections::HashMap;
use balena_temen::ast::Expression;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use crate::dsl::schema::object_types::ObjectType;
use crate::dsl::schema::when::DependencyGraph;
pub mod deserialization;
pub mod compiler;
pub mod object_types;
pub mod when;
#[derive(Clone, Debug)]
pub struct DocumentRoot {
pub version: u64,
pub schema: Option<Schema>,
pub dependencies: Option<DependencyGraph>,
}
#[derive(Clone, Debug)]
pub struct SchemaList {
entries: Vec<NamedSchema>,
}
#[derive(Clone, Debug, Serialize)]
pub struct NamedSchema {
pub name: String,
#[serde(flatten)]
pub schema: Schema,
}
#[derive(Clone, Debug)]
pub struct Schema {
pub types: Option<Vec<ObjectType>>,
pub annotations: Annotations,
pub children: Option<SchemaList>,
pub mapping: Option<serde_yaml::Mapping>, pub when: Option<Expression>,
}
#[derive(Clone, Default, Debug, Deserialize)]
pub struct Annotations {
pub title: Option<String>,
pub help: Option<String>,
pub warning: Option<String>,
pub description: Option<String>,
}
impl NamedSchema {
pub fn unpack(&self) -> (&str, &Schema) {
(self.name.as_ref(), &self.schema)
}
}
impl SchemaList {
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn all_as_map(&self) -> HashMap<&str, &Schema> {
self.entries().iter().map(|schema| schema.unpack()).collect()
}
pub fn entries(&self) -> &Vec<NamedSchema> {
&self.entries
}
pub fn dependent_schemas(&self) -> Vec<&NamedSchema> {
self.entries
.iter()
.filter(|named_schema| named_schema.schema.when.is_some()) .collect()
}
pub fn independent_schemas(&self) -> Vec<&NamedSchema> {
self.entries
.iter()
.filter(|named_schema| named_schema.schema.when.is_none()) .collect()
}
pub fn independent_as_map(&self) -> HashMap<&str, &Schema> {
self.independent_schemas()
.iter()
.map(|schema| schema.unpack())
.collect()
}
pub fn independent_schema_names(&self) -> Vec<&str> {
self.entries
.iter()
.filter(|named_schema| named_schema.schema.when.is_none()) .map(|named_schema| named_schema.name.as_str())
.collect()
}
pub fn required_schema_names(&self) -> Vec<&str> {
self.entries
.iter()
.filter(|named_schema| named_schema.schema.when.is_none()) .filter_map(|named_schema| match &named_schema.schema.types {
Some(type_list) => {
if type_list.iter().any(|type_spec| match type_spec {
ObjectType::Required(_) => true,
ObjectType::Optional(_) => false,
}) {
Some(named_schema.name.as_str())
} else {
None
}
}
None => None,
})
.collect()
}
}