atelier_core 0.2.22

Rust native core model for the AWS Smithy IDL.
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*!
This module provides a *line-oriented* format for the Smithy model. This representation is such
that it is always ordered and has no whitespace or other ambiguities and so can be directly
compared as a whole. This is valuable for testing but can also be extremely fast for parsing
tools.

# Example

```rust
use atelier_core::io::ModelWriter;
use atelier_core::io::lines::LineOrientedWriter;
use atelier_core::model::Model;
# fn make_model() -> Model { Model::default() }
let model = make_model();

let mut writer = LineOrientedWriter::default();
let result = writer.write(&mut std::io::stdout(), &model);
assert!(result.is_ok())
```

# Representation Format

The following is a description of the production of the format, note there are a number of
separator strings used; 1) "`::`" the *segment* separator, 2) "`=>`" the *target* operator,
and "`<=`" the *value assignment* operator.

```text
 1. {shape_type}::{shape_id}
 2. {shape_type}::{shape_id}::trait::{shape_id}
 3. {shape_type}::{shape_id}::trait::{shape_id}<={value...}
 4. {shape_type}::{shape_id}::{identifier}=>{shape_id}
 5. {shape_type}::{shape_id}::{identifier}::trait::{shape_id}
 6. {shape_type}::{shape_id}::{identifier}::trait::{shape_id}<={value...}
 7. {shape_type}::{shape_id}::identifier::{identifier}=>{shape_id}
 8. {shape_type}::{shape_id}::rename::{shape_id}<={identifier}
 9. meta::{identifier}<={value...}
10. ()
11. {simple_value}
12. [{integer}]={value...}
13. {{identifier}}={value...}
```

* For each top-level shape:
  * emit the name of the shape type, a segment separator, and the shape's fully qualified name (1).
  * For each trait applied to this shape:
    * append to the above string the value "`::trait`" and the trait's fully qualified name (2),
    * if the trait has a value, append the "`<=`" and follow the value production rules below (3).
  * For each member of the shape:
    * emit a line with the member identifier, "`=>`", and the target's fully qualified name (4),
      * for array-valued members the member name emitted is the singular form (error for errors, etc.).
      * If the shape is a resource; emit the "`identifiers`" map-valued member:
        * append an additional "`::identifier::`" string,
        * emit each key followed by "`=>`", and the target's fully qualified name (7),
      * If the shape is a service; emit the "`renames`" map-valued member:
        * append an additional "`::rename::`" string,
        * emit each key followed by "`<=`", and the value (8),
    * For each trait applied to the member:
      * append to the above string the value "`::trait`" and the trait's fully qualified name (5),
      * if the trait has a value, append the "`<=`" and follow the value production rules below (6).

* For each value in the model metadata map:
  * use the string "`meta`" as if it where a shape name followed by "`::`"
  * append the key name, the string "`<=`" and follow the value production rules below (9).

* For null values simply emit the string "`()`" (10).
* For boolean, numeric, and string values emit their natural form (11).
  * Ensure string values are correctly quoted.
* For arrays:
  * emit a line per index, with "`[`", the index as a zero-based integer, "`]`", the operator
    "`<=`" and follow these same value production rules (12),
  * an empty array is denoted with the string "`[]`".
* For objects:
  * emit a line per key, with "`{`", the key name, "`}`", the operator "`<=`" and follow
    these same value production rules (13),
  * an empty object is denoted with the string "`{}`".

Finally, all lines must be sorted to ensure the overall output can be compared.

## Examples

A simple string shape with a trait applied.

```text
// "pattern" is a trait.
@pattern("^[A-Za-z0-9 ]+$")
string CityId
```

```text
string::example.weather#CityId
string::example.weather#CityId::trait::smithy.api#pattern<="^[A-Za-z0-9 ]+$"
```

An operation, note the rename of "errors" to "error as the member identifier.

```text
@readonly
operation GetCity {
    input: GetCityInput
    output: GetCityOutput
    errors: [NoSuchResource]
}
```

```text
operation::example.weather#GetCity
operation::example.weather#GetCity::error=>example.weather#NoSuchResource
operation::example.weather#GetCity::input=>example.weather#GetCityInput
operation::example.weather#GetCity::output=>example.weather#GetCityInput
operation::example.weather#GetCity::trait::smithy.api#readonly
```

A service, note the object-based trait "paginated" and the comment that has been turned into a
documentation trait.

```text
/// Provides weather forecasts.
@paginated(inputToken: "nextToken", outputToken: "nextToken",
           pageSize: "pageSize")
service Weather {
    version: "2006-03-01"
    resources: [City]
    operations: [GetCurrentTime]
    rename: {
        "foo.example#Widget": "FooWidget"
    }
}
```

```text
service::example.weather#Weather
service::example.weather#Weather::operation=>example.weather#GetCurrentTime
service::example.weather#Weather::resource=>example.weather#City
service::example.weather#Weather::rename::foo.example#Widget<=FooWidget
service::example.weather#Weather::trait::smithy.api#documentation<="Provides weather forecasts."
service::example.weather#Weather::trait::smithy.api#paginated<={inputToken}="nextToken"
service::example.weather#Weather::trait::smithy.api#paginated<={outputToken}="nextToken"
service::example.weather#Weather::trait::smithy.api#paginated<={pageSize}="pageSize"
service::example.weather#Weather::version<="2006-03-01"
```
*/

use crate::io::ModelWriter;
use crate::model::shapes::{HasTraits, MemberShape, ShapeKind, TopLevelShape};
use crate::model::values::Value;
use crate::model::{HasIdentity, Model, ShapeID};
use std::collections::HashMap;
use std::io::Write;

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// This type implements the `ModelWriter` trait and will output the line-oriented format when
/// `ModelWriter::write` is called.
///
/// See the [module-level](index.html) documentation for details of this format.
///
#[derive(Debug)]
pub struct LineOrientedWriter {}

// ------------------------------------------------------------------------------------------------
// Private Types
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

///
/// Make a line-oriented version of the provided model and return as a list of strings.
///
/// See the [module-level](index.html) documentation for details of this format.
///
pub fn make_line_oriented_form(model: &Model) -> Vec<String> {
    let mut strings = Default::default();
    for (key, value) in model.metadata() {
        value_into_strings(
            &format!("{}{}{}{}", META_PREFIX, SEGMENT_SEP, key, VALUE_SEP),
            value,
            &mut strings,
        );
    }
    for shape in model.shapes() {
        shape_into_strings(shape, &mut strings)
    }
    strings.sort();
    strings
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Default for LineOrientedWriter {
    fn default() -> Self {
        Self {}
    }
}

impl ModelWriter for LineOrientedWriter {
    fn write(&mut self, w: &mut impl Write, model: &Model) -> crate::error::Result<()> {
        for line in make_line_oriented_form(model) {
            writeln!(w, "{}", line)?;
        }
        Ok(())
    }
}

// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------

const META_PREFIX: &str = "meta";
const TRAIT_PREFIX: &str = "trait";
const SEGMENT_SEP: &str = "::";
const TARGET_SEP: &str = "=>";
const VALUE_SEP: &str = "<=";

fn line_prefix(shape_type: &str, shape: &TopLevelShape) -> String {
    format!("{}{}{}", shape_type, SEGMENT_SEP, shape.id())
}
fn shape_into_strings(shape: &TopLevelShape, strings: &mut Vec<String>) {
    let prefix = match shape.body() {
        ShapeKind::Simple(v) => {
            let prefix = line_prefix(&v.to_string(), shape);
            strings.push(prefix.clone());
            prefix
        }
        ShapeKind::List(v) => {
            let prefix = line_prefix("list", shape);
            strings.push(prefix.clone());
            member_into_strings(&prefix, v.member(), strings);
            prefix
        }
        ShapeKind::Set(v) => {
            let prefix = line_prefix("set", shape);
            strings.push(prefix.clone());
            member_into_strings(&prefix, v.member(), strings);
            prefix
        }
        ShapeKind::Map(v) => {
            let prefix = line_prefix("map", shape);
            strings.push(prefix.clone());
            member_into_strings(&prefix, v.key(), strings);
            member_into_strings(&prefix, v.value(), strings);
            prefix
        }
        ShapeKind::Structure(v) => {
            let prefix = line_prefix("structure", shape);
            strings.push(prefix.clone());
            for member in v.members() {
                member_into_strings(&prefix, member, strings);
            }
            prefix
        }
        ShapeKind::Union(v) => {
            let prefix = line_prefix("union", shape);
            strings.push(prefix.clone());
            for member in v.members() {
                member_into_strings(&prefix, member, strings);
            }
            prefix
        }
        ShapeKind::Service(v) => {
            let prefix = line_prefix("service", shape);
            strings.push(prefix.clone());
            strings.push(format!(
                "{}{}version{}{:?}",
                prefix,
                SEGMENT_SEP,
                VALUE_SEP,
                v.version()
            ));
            for id in v.operations() {
                strings.push(format!("{}{}", prefix, member_target("operation", id)));
            }
            for id in v.resources() {
                strings.push(format!("{}{}", prefix, member_target("resource", id)));
            }
            for (id, ln) in v.renames() {
                strings.push(format!(
                    "{}{}rename{}{}{}{}",
                    prefix, SEGMENT_SEP, SEGMENT_SEP, id, VALUE_SEP, ln
                ));
            }
            prefix
        }
        ShapeKind::Operation(v) => {
            let prefix = line_prefix("operation", shape);
            strings.push(prefix.clone());
            if let Some(id) = v.input() {
                strings.push(format!("{}{}", prefix, member_target("input", id)));
            }
            if let Some(id) = v.output() {
                strings.push(format!("{}{}", prefix, member_target("output", id)));
            }
            for id in v.errors() {
                strings.push(format!("{}{}", prefix, member_target("error", id)));
            }
            prefix
        }
        ShapeKind::Resource(v) => {
            let prefix = line_prefix("resource", shape);
            strings.push(prefix.clone());
            let ident_prefix = format!("{}identifier", SEGMENT_SEP);
            for (k, id) in v.identifiers() {
                strings.push(format!(
                    "{}{}{}",
                    prefix,
                    ident_prefix,
                    member_target(&k.to_string(), id)
                ));
            }
            if let Some(id) = v.create() {
                strings.push(format!("{}{}", prefix, member_target("create", id)));
            }
            if let Some(id) = v.put() {
                strings.push(format!("{}{}", prefix, member_target("put", id)));
            }
            for id in v.read() {
                strings.push(format!("{}{}", prefix, member_target("read", id)));
            }
            if let Some(id) = v.update() {
                strings.push(format!("{}{}", prefix, member_target("update", id)));
            }
            if let Some(id) = v.delete() {
                strings.push(format!("{}{}", prefix, member_target("delete", id)));
            }
            for id in v.list() {
                strings.push(format!("{}{}", prefix, member_target("list", id)));
            }
            for id in v.operations() {
                strings.push(format!("{}{}", prefix, member_target("operation", id)));
            }
            for id in v.collection_operations() {
                strings.push(format!(
                    "{}{}",
                    prefix,
                    member_target("collection_operation", id)
                ));
            }
            for id in v.resources() {
                strings.push(format!("{}{}", prefix, member_target("resource", id)));
            }
            prefix
        }
        ShapeKind::Unresolved => {
            let prefix = line_prefix("unresolved", shape);
            strings.push(prefix.clone());
            prefix
        }
    };
    let prefix = format!("{}{}{}{}", prefix, SEGMENT_SEP, TRAIT_PREFIX, SEGMENT_SEP);
    traits_into_strings(&prefix, shape.traits(), strings);
}

fn member_target(member_name: &str, id: &ShapeID) -> String {
    format!("{}{}{}{}", SEGMENT_SEP, member_name, TARGET_SEP, id)
}

fn value_into_strings(prefix: &str, value: &Value, strings: &mut Vec<String>) {
    match value {
        Value::Array(vs) => {
            if vs.is_empty() {
                strings.push(format!("{}[]", prefix));
            } else {
                for (i, v) in vs.iter().enumerate() {
                    let prefix = format!("{}[{}]=", prefix, i);
                    value_into_strings(&prefix, v, strings);
                }
            }
        }
        Value::Object(vo) => {
            if vo.is_empty() {
                strings.push(format!("{}{{}}", prefix));
            } else {
                for (k, v) in vo {
                    let prefix = format!("{}{{{}}}=", prefix, k);
                    value_into_strings(&prefix, v, strings);
                }
            }
        }
        Value::Number(v) => strings.push(format!("{}{}", prefix, v)),
        Value::Boolean(v) => strings.push(format!("{}{}", prefix, v)),
        Value::String(v) => strings.push(format!("{}\"{}\"", prefix, v)),
        Value::None => strings.push(format!("{}()", prefix)),
    }
}

fn member_into_strings(prefix: &str, member: &MemberShape, strings: &mut Vec<String>) {
    let prefix = format!("{}{}{}", prefix, SEGMENT_SEP, member.id());
    strings.push(format!("{}{}{}", prefix, TARGET_SEP, member.target()));
    let prefix = format!("{}{}{}{}", prefix, SEGMENT_SEP, TRAIT_PREFIX, SEGMENT_SEP);
    traits_into_strings(&prefix, member.traits(), strings);
}

fn traits_into_strings(
    prefix: &str,
    traits: &HashMap<ShapeID, Option<Value>>,
    strings: &mut Vec<String>,
) {
    for (shape_id, value) in traits {
        match value {
            None => {
                strings.push(format!("{}{}", prefix, shape_id));
            }
            Some(value) => {
                value_into_strings(
                    &format!("{}{}{}", prefix, shape_id, VALUE_SEP),
                    value,
                    strings,
                );
            }
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------