1#![doc = include_str!("../README.md")]
2
3mod data_model;
4pub mod cpp;
5pub mod schema;
6
7pub use data_model::{DataModel, DataType, DataTypeData, Struct, StructField, DefaultType, Array, Variant, Enum, EnumType, ConstrainedType, SerializationModel};
8
9#[cfg(test)]
10mod tests {
11 use super::*;
12 use std::{
13 fs,
14 collections::HashMap,
15 };
16
17 #[test]
18 fn main_model() {
19 let model = DataModel {
20 namespace: vec!["my_namespace".to_string()],
21 macros: HashMap::from([("DEFAULT_COORDINATE".to_string(), SerializationModel::Value("0.0".to_string())), ("MESSAGE".to_string(), SerializationModel::Value("This is a macro message".to_string()))]),
22 headers: HashMap::from([("cpp-header".to_string(), "// My .h Header with message: $MESSAGE$".to_string()), ("cpp-source".to_string(), "// My .cpp Header and this is a dollar sign: $$".to_string())]),
23 footers: HashMap::from([("cpp-header".to_string(), "// My .h Footer".to_string()), ("cpp-source".to_string(), "// My .cpp Footer".to_string())]),
24 data_types: vec![
25 DataType {
26 name: "PositiveDouble".to_string(),
27 description: None,
28 data: DataTypeData::ConstrainedType(ConstrainedType {
29 data_type: "number".to_string(),
30 constraints: vec![
31 "x > 0.0".to_string(),
32 ],
33 }),
34 },
35 DataType {
36 name: "Point".to_string(),
37 description: Some("A point in 2D space".to_string()),
38 data: DataTypeData::Struct(Struct {
39 inherit: None,
40 fields: vec![
41 StructField {
42 name: "x".to_string(),
43 description: None,
44 data_type: "number".to_string(),
45 default: DefaultType::Default(SerializationModel::Value("0.0".to_string())),
46 },
47 StructField {
48 name: "y".to_string(),
49 description: None,
50 data_type: "number".to_string(),
51 default: DefaultType::Default(SerializationModel::Value("$DEFAULT_COORDINATE$".to_string())),
52 },
53 StructField {
54 name: "id".to_string(),
55 description: None,
56 data_type: "integer".to_string(),
57 default: DefaultType::Optional,
58 },
59 ],
60 }),
61 },
62 DataType {
63 name: "Size".to_string(),
64 description: Some("The size of a box".to_string()),
65 data: DataTypeData::Struct(Struct {
66 inherit: None,
67 fields: vec![
68 StructField {
69 name: "w".to_string(),
70 description: Some("The width".to_string()),
71 data_type: "PositiveDouble".to_string(),
72 default: DefaultType::Required,
73 },
74 StructField {
75 name: "h".to_string(),
76 description: Some("The height".to_string()),
77 data_type: "PositiveDouble".to_string(),
78 default: DefaultType::Required,
79 },
80 ],
81 }),
82 },
83 DataType {
84 name: "SizeVariant".to_string(),
85 description: Some("Is either a Size or just a PositiveDouble if it is a square".to_string()),
86 data: DataTypeData::Variant(Variant {
87 data_types: vec![
88 "PositiveDouble".to_string(),
89 "Size".to_string(),
90 ],
91 }),
92 },
93 DataType {
94 name: "SizeArray".to_string(),
95 description: None,
96 data: DataTypeData::Array(Array {
97 data_type: "SizeVariant".to_string(),
98 }),
99 },
100 DataType {
101 name: "Geometry".to_string(),
102 description: None,
103 data: DataTypeData::Enum(Enum {
104 types: vec![
105 EnumType {
106 name: "Nothing".to_string(),
107 description: Some("No geometry".to_string()),
108 data_type: None,
109 },
110 EnumType {
111 name: "Sizes".to_string(),
112 description: Some("A number of sizes".to_string()),
113 data_type: Some("SizeArray".to_string()),
114 },
115 EnumType {
116 name: "Point".to_string(),
117 description: Some("A point".to_string()),
118 data_type: Some("Point".to_string()),
119 },
120 ],
121 }),
122 },
123 DataType {
124 name: "NamedGeometry".to_string(),
125 description: None,
126 data: DataTypeData::Struct(Struct {
127 fields: vec![
128 StructField {
129 name: "geometry".to_string(),
130 description: Some("The geometry data".to_string()),
131 data_type: "Geometry".to_string(),
132 default: DefaultType::Default(SerializationModel::Map(HashMap::from([
133 ("Point".to_string(), SerializationModel::Map(HashMap::from([
134 ("x".to_string(), SerializationModel::Value("1.0".to_string())),
135 ("id".to_string(), SerializationModel::Value("0".to_string())),
136 ])))
137 ])))
138 },
139 StructField {
140 name: "name".to_string(),
141 description: Some("The name of the geometry".to_string()),
142 data_type: "string".to_string(),
143 default: DefaultType::Required,
144 }
145 ],
146 inherit: None,
147 })
148 },
149 ],
150 };
151
152 let import = fs::read_to_string("tests/yaml_import.yaml").unwrap();
156 let import_model = DataModel::import_yaml(&import).unwrap();
157
158 assert_eq!(model, import_model);
159 }
160}