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
use crate::{
error::{Error, Result},
JsonValue,
};
use atelier_core::{
model::{
shapes::{AppliedTraits, HasTraits, MemberShape, Operation, ShapeKind},
values::{Number, Value as NodeValue},
HasIdentity, Identifier, Model, NamespaceID, ShapeID,
},
prelude::prelude_namespace_id,
};
use cfg_if::cfg_if;
use lazy_static::lazy_static;
use serde::{de::DeserializeOwned, Deserialize};
use std::str::FromStr;
const WASMCLOUD_MODEL_NAMESPACE: &str = "org.wasmcloud.model";
const WASMCLOUD_CORE_NAMESPACE: &str = "org.wasmcloud.core";
const TRAIT_CODEGEN_RUST: &str = "codegenRust";
const TRAIT_SERIALIZATION: &str = "serialization";
const TRAIT_WASMBUS: &str = "wasmbus";
const TRAIT_WASMBUS_DATA: &str = "wasmbusData";
lazy_static! {
static ref WASMCLOUD_MODEL_NAMESPACE_ID: NamespaceID =
NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE);
static ref WASMCLOUD_CORE_NAMESPACE_ID: NamespaceID =
NamespaceID::new_unchecked(WASMCLOUD_CORE_NAMESPACE);
static ref SERIALIZATION_TRAIT_ID: ShapeID = ShapeID::new(
NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
Identifier::from_str(TRAIT_SERIALIZATION).unwrap(),
None
);
static ref CODEGEN_RUST_TRAIT_ID: ShapeID = ShapeID::new(
NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
Identifier::from_str(TRAIT_CODEGEN_RUST).unwrap(),
None
);
static ref WASMBUS_TRAIT_ID: ShapeID = ShapeID::new(
NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
Identifier::from_str(TRAIT_WASMBUS).unwrap(),
None
);
static ref WASMBUS_DATA_TRAIT_ID: ShapeID = ShapeID::new(
NamespaceID::new_unchecked(WASMCLOUD_MODEL_NAMESPACE),
Identifier::from_str(TRAIT_WASMBUS_DATA).unwrap(),
None
);
}
pub fn wasmcloud_model_namespace() -> &'static NamespaceID {
&WASMCLOUD_MODEL_NAMESPACE_ID
}
#[cfg(feature = "wasmbus")]
pub fn wasmbus_trait() -> &'static ShapeID {
&WASMBUS_TRAIT_ID
}
#[allow(dead_code)]
#[cfg(feature = "wasmbus")]
pub fn wasmbus_data_trait() -> &'static ShapeID {
&WASMBUS_DATA_TRAIT_ID
}
pub fn serialization_trait() -> &'static ShapeID {
&SERIALIZATION_TRAIT_ID
}
pub fn codegen_rust_trait() -> &'static ShapeID {
&CODEGEN_RUST_TRAIT_ID
}
#[allow(dead_code)]
pub enum CommentKind {
Inner,
Documentation,
}
#[macro_export]
macro_rules! expect_empty {
($list:expr, $msg:expr) => {
if !$list.is_empty() {
return Err(Error::InvalidModel(format!(
"{}: {}",
$msg,
$list
.keys()
.map(|k| k.to_string())
.collect::<Vec<String>>()
.join(",")
)));
}
};
}
#[macro_export]
macro_rules! unsupported_shape {
($fn_name:ident, $shape_type:ty, $doc:expr) => {
#[allow(unused_variables)]
fn $fn_name(
&mut self,
id: &ShapeID,
traits: &AppliedTraits,
shape: &$shape_type,
) -> Result<()> {
return Err(crate::error::Error::UnsupportedShape(
id.to_string(),
$doc.to_string(),
));
}
};
}
pub fn is_opt_namespace(id: &ShapeID, ns: &Option<NamespaceID>) -> bool {
match ns {
Some(ns) => id.namespace() == ns,
None => true,
}
}
pub fn get_operation<'model>(
model: &'model Model,
operation_id: &'_ ShapeID,
service_id: &'_ Identifier,
) -> Result<(&'model Operation, &'model AppliedTraits)> {
let op = model
.shapes()
.filter(|t| t.id() == operation_id)
.find_map(|t| {
if let ShapeKind::Operation(op) = t.body() {
Some((op, t.traits()))
} else {
None
}
})
.ok_or_else(|| {
Error::Model(format!(
"missing operation {} for service {}",
&operation_id.to_string(),
&service_id.to_string()
))
})?;
Ok(op)
}
pub fn get_trait<T: DeserializeOwned>(traits: &AppliedTraits, id: &ShapeID) -> Result<Option<T>> {
match traits.get(id) {
Some(Some(val)) => match trait_value(val) {
Ok(obj) => Ok(Some(obj)),
Err(e) => Err(e),
},
Some(None) => Ok(None),
None => Ok(None),
}
}
pub fn trait_value<T: DeserializeOwned>(value: &NodeValue) -> Result<T> {
let json = value_to_json(value);
let obj = serde_json::from_value(json)?;
Ok(obj)
}
pub fn value_to_json(value: &NodeValue) -> JsonValue {
match value {
NodeValue::None => JsonValue::Null,
NodeValue::Array(v) => JsonValue::Array(v.iter().map(|v| value_to_json(v)).collect()),
NodeValue::Object(v) => {
let mut object = crate::JsonMap::default();
for (k, v) in v {
let _ = object.insert(k.clone(), value_to_json(v));
}
JsonValue::Object(object)
}
NodeValue::Number(v) => match v {
Number::Integer(v) => JsonValue::Number((*v).into()),
Number::Float(v) => JsonValue::Number(serde_json::Number::from_f64(*v).unwrap()),
},
NodeValue::Boolean(v) => JsonValue::Bool(*v),
NodeValue::String(v) => JsonValue::String(v.clone()),
}
}
pub fn resolve<'model>(model: &'model Model, shape: &'model ShapeID) -> &'model ShapeID {
if let Some(resolved) = model.shape(shape) {
resolved.id()
} else {
shape
}
}
pub fn has_default(model: &'_ Model, member: &MemberShape) -> bool {
let id = resolve(model, member.target());
if id.namespace().eq(prelude_namespace_id()) {
let name = id.shape_name().to_string();
cfg_if! { if #[cfg(feature = "BigInteger")] { &name == "bigInteger" || } }
cfg_if! { if #[cfg(feature = "BigDecimal")] { &name == "bigDecimal" || } }
cfg_if! { if #[cfg(feature = "Timestamp")] { &name == "timestamp" || } }
matches!(
name.as_str(),
"List" | "Set" | "Map"
| "Blob" | "Boolean" | "String" | "Byte" | "Short"
| "Integer" | "Long" | "Float" | "Double"
)
} else {
false
}
}
#[derive(Clone, Deserialize)]
pub struct PackageName {
pub namespace: String,
#[serde(rename = "crate")]
pub crate_name: String,
}