smithy4rs-core 0.0.1

Core functionality for smithy4rs.
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use std::{
    any::type_name,
    error::Error as StdError,
    fmt::{Debug, Display, Formatter},
};

use serde::ser::{Error as SerdeError, SerializeMap, SerializeSeq, SerializeStruct};
use static_str_ops::staticize;

use crate::{
    BigDecimal, BigInt, ByteBuffer, Instant,
    prelude::{JsonNameTrait, XmlAttributeTrait, XmlNameTrait},
    schema::{Document, SchemaRef},
    serde::{
        se::{ListSerializer, MapSerializer, SerializeWithSchema, StructSerializer},
        serializers::{Error, Serializer},
    },
};

//========================================================================
// Errors
//========================================================================

/// Wrapper type that bridges `serde` and `smithy` Serialization error types.
#[derive(Debug)]
#[repr(transparent)]
pub struct SerdeErrorWrapper<E: SerdeError>(E);
impl<E: SerdeError> SerdeErrorWrapper<E> {
    pub fn inner(self) -> E {
        self.0
    }
}
impl<E: SerdeError> Display for SerdeErrorWrapper<E> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, f)
    }
}
impl<E: SerdeError> StdError for SerdeErrorWrapper<E> {}
impl<E: SerdeError> Error for SerdeErrorWrapper<E> {
    #[inline]
    fn custom<T: Display>(msg: T) -> Self {
        SerdeErrorWrapper(E::custom(msg))
    }
}
impl<E: SerdeError> From<E> for SerdeErrorWrapper<E> {
    #[inline]
    fn from(e: E) -> Self {
        SerdeErrorWrapper(e)
    }
}

//========================================================================
// Serialization Adapter
//========================================================================

// Traits to support (could these be global??):
// 1.
// 2. xmlName - Applied to shape names and to member names
// 3.
// 4.
//
// Should be able to add additional features in the future!

/// Adapter that bridges between `serde` serialization and schema-guided
/// serialization.
///
/// ## Supported Protocol traits
/// By default we support the following built-in protocol traitsL
/// 1. `jsonName` - Renames members in `json` serde serializers
/// 2. `xmlName` - Renames shapes and members in `xml` serde serializers
/// 3. `xmlAttribute` - Treats a member as an attribute for `xml` serde serializers (appends: `@:` to member name)
///
/// **NOTE**: The `xmlNamespace` trait is not supported by this adapter. This
/// is because `serde`'s XML implementation requires namespaces to be set in
/// the serializer config directly.
///
/// ## Generated Shapes
/// This structure is used inside generated `serde::serialize` implementations
/// when the `serde-adapter` feature is enabled.
pub struct SerAdapter<S: serde::Serializer> {
    serializer: S,
    mapper: NameMapper,
}
impl<S: serde::Serializer> SerAdapter<S> {
    /// Create a new schema-guided serialization adapter for a [`serde::Serializer`]
    pub fn new(serializer: S) -> Self {
        SerAdapter {
            serializer,
            mapper: NameMapper::new::<S>(),
        }
    }
}

/// Applies name mapping to support default protocol traits.
enum NameMapper {
    Json,
    Xml,
    Default,
}
impl NameMapper {
    fn new<T: serde::Serializer>() -> Self {
        if type_name::<T>().contains("json") {
            NameMapper::Json
        } else if type_name::<T>().contains("xml") {
            NameMapper::Xml
        } else {
            NameMapper::Default
        }
    }

    fn get_struct_name(&self, schema: &SchemaRef) -> &'static str {
        if matches!(self, NameMapper::Xml)
            && let Some(xml_name) = schema.get_trait_as::<XmlNameTrait>()
        {
            return staticize(xml_name.name());
        }
        staticize(schema.id().name())
    }

    fn get_member_name<S: StructSerializer>(
        &self,
        schema: &SchemaRef,
    ) -> Result<&'static str, S::Error> {
        let Some(me) = schema.as_member() else {
            return Err(S::Error::custom(
                "Expected member schema when serializing struct field",
            ));
        };
        match self {
            NameMapper::Json => {
                // Rename based on JSON Traits, if present
                Ok(schema
                    .get_trait_as::<JsonNameTrait>()
                    .map_or_else(|| staticize(&me.name), |val| staticize(val.name())))
            }
            NameMapper::Xml => {
                // Rename based on JSON Traits
                let name = schema
                    .get_trait_as::<XmlNameTrait>()
                    .map_or_else(|| me.name.as_str(), |val| val.name());
                // Add attribute prefix if applicable
                if schema.contains_type::<XmlAttributeTrait>() {
                    return Ok(staticize(format!("@{name}")));
                }
                Ok(staticize(name))
            }
            NameMapper::Default => Ok(staticize(&me.name)),
        }
    }
}

impl<S: serde::Serializer> Serializer for SerAdapter<S> {
    type Error = SerdeErrorWrapper<S::Error>;
    type Ok = S::Ok;
    type SerializeList = ListSerializeAdapter<S>;
    type SerializeMap = MapSerializerAdapter<S>;
    type SerializeStruct = StructSerializerAdapter<S>;

    #[inline]
    fn write_struct(
        self,
        schema: &SchemaRef,
        len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        let struct_name = self.mapper.get_struct_name(schema);
        let struct_ser = self.serializer.serialize_struct(struct_name, len)?;
        Ok(StructSerializerAdapter::new(struct_ser, self.mapper))
    }

    #[inline]
    fn write_map(self, _schema: &SchemaRef, len: usize) -> Result<Self::SerializeMap, Self::Error> {
        let map_ser = self.serializer.serialize_map(Some(len))?;
        Ok(MapSerializerAdapter::new(map_ser))
    }

    #[inline]
    fn write_list(
        self,
        _schema: &SchemaRef,
        len: usize,
    ) -> Result<Self::SerializeList, Self::Error> {
        let list_ser = self.serializer.serialize_seq(Some(len))?;
        Ok(ListSerializeAdapter::new(list_ser))
    }

    #[inline]
    fn write_boolean(self, _: &SchemaRef, value: bool) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_bool(value)?)
    }

    #[inline]
    fn write_byte(self, _: &SchemaRef, value: i8) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_i8(value)?)
    }

    #[inline]
    fn write_short(self, _: &SchemaRef, value: i16) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_i16(value)?)
    }

    #[inline]
    fn write_integer(self, _: &SchemaRef, value: i32) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_i32(value)?)
    }

    #[inline]
    fn write_long(self, _: &SchemaRef, value: i64) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_i64(value)?)
    }

    #[inline]
    fn write_float(self, _: &SchemaRef, value: f32) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_f32(value)?)
    }

    #[inline]
    fn write_double(self, _: &SchemaRef, value: f64) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_f64(value)?)
    }

    #[inline]
    fn write_big_integer(
        self,
        _schema: &SchemaRef,
        _value: &BigInt,
    ) -> Result<Self::Ok, Self::Error> {
        todo!()
    }

    #[inline]
    fn write_big_decimal(
        self,
        _schema: &SchemaRef,
        _value: &BigDecimal,
    ) -> Result<Self::Ok, Self::Error> {
        todo!()
    }

    #[inline]
    fn write_string(self, _: &SchemaRef, value: &str) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_str(value)?)
    }

    #[inline]
    fn write_blob(self, _: &SchemaRef, _value: &ByteBuffer) -> Result<Self::Ok, Self::Error> {
        // TODO(streams): How to support data streams?
        todo!()
    }

    #[inline]
    fn write_timestamp(self, _: &SchemaRef, _value: &Instant) -> Result<Self::Ok, Self::Error> {
        // TODO(timestamp formatting): How to write timestamps with formatting traits?
        todo!()
    }

    #[inline]
    fn write_document(
        self,
        _: &SchemaRef,
        _value: &Box<dyn Document>,
    ) -> Result<Self::Ok, Self::Error> {
        // TODO(document serde): Finish implementing for document types.
        todo!()
    }

    #[inline]
    fn write_null(self, _: &SchemaRef) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_none()?)
    }

    fn skip(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.serialize_none()?)
    }
}

#[doc(hidden)]
pub struct ListSerializeAdapter<S: serde::Serializer> {
    serializer: S::SerializeSeq,
}
impl<S: serde::Serializer> ListSerializeAdapter<S> {
    const fn new(serializer: S::SerializeSeq) -> Self {
        Self { serializer }
    }
}
impl<S: serde::Serializer> ListSerializer for ListSerializeAdapter<S> {
    type Error = SerdeErrorWrapper<S::Error>;
    type Ok = S::Ok;

    #[inline]
    fn serialize_element<T>(
        &mut self,
        value_schema: &SchemaRef,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: SerializeWithSchema,
    {
        Ok(self
            .serializer
            .serialize_element(&ValueWrapper(value_schema, value))?)
    }

    #[inline]
    fn end(self, _: &SchemaRef) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.end()?)
    }
}

#[doc(hidden)]
pub struct MapSerializerAdapter<S: serde::Serializer> {
    serializer: S::SerializeMap,
}
impl<S: serde::Serializer> MapSerializerAdapter<S> {
    const fn new(serializer: S::SerializeMap) -> Self {
        Self { serializer }
    }
}
impl<S: serde::Serializer> MapSerializer for MapSerializerAdapter<S> {
    type Error = SerdeErrorWrapper<S::Error>;
    type Ok = S::Ok;

    #[inline]
    fn serialize_entry<K, V>(
        &mut self,
        key_schema: &SchemaRef,
        value_schema: &SchemaRef,
        key: &K,
        value: &V,
    ) -> Result<(), Self::Error>
    where
        K: SerializeWithSchema,
        V: SerializeWithSchema,
    {
        Ok(self.serializer.serialize_entry(
            &ValueWrapper(key_schema, key),
            &ValueWrapper(value_schema, value),
        )?)
    }

    #[inline]
    fn end(self, _: &SchemaRef) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.end()?)
    }
}

#[doc(hidden)]
pub struct StructSerializerAdapter<S: serde::Serializer> {
    serializer: S::SerializeStruct,
    mapper: NameMapper,
}
impl<S: serde::Serializer> StructSerializerAdapter<S> {
    const fn new(serializer: S::SerializeStruct, mapper: NameMapper) -> Self {
        Self { serializer, mapper }
    }
}
impl<S: serde::Serializer> StructSerializer for StructSerializerAdapter<S> {
    type Error = SerdeErrorWrapper<S::Error>;
    type Ok = S::Ok;

    #[inline]
    fn serialize_member<T>(
        &mut self,
        member_schema: &SchemaRef,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: SerializeWithSchema,
    {
        let name = self.mapper.get_member_name::<Self>(member_schema)?;
        Ok(self
            .serializer
            .serialize_field(name, &ValueWrapper(member_schema, value))?)
    }

    #[inline]
    fn end(self, _: &SchemaRef) -> Result<Self::Ok, Self::Error> {
        Ok(self.serializer.end()?)
    }
}

//========================================================================
// Value Wrapper
// -------------
// Wraps inner values of
//========================================================================

struct ValueWrapper<'a, T: SerializeWithSchema>(&'a SchemaRef, &'a T);
impl<T: SerializeWithSchema> serde::Serialize for ValueWrapper<'_, T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.1
            .serialize_with_schema(self.0, SerAdapter::new(serializer))
            .map_err(|wrapper| wrapper.0)
    }
}

#[cfg(test)]
mod tests {
    use crate::{IndexMap, derive::SmithyShape, schema::prelude::*, smithy};

    smithy!("com.example#Map": {
        map MAP_SCHEMA {
            key: STRING
            value: STRING
        }
    });
    smithy!("com.example#List": {
        list LIST_SCHEMA {
            member: STRING
        }
    });
    smithy!("com.example#Test": {
        structure SCHEMA {
            A: STRING = "a"
            B: STRING = "b"
            MAP: MAP_SCHEMA = "map"
            LIST: LIST_SCHEMA = "list"
        }
    });

    #[derive(SmithyShape)]
    #[smithy_schema(SCHEMA)]
    pub struct Test {
        #[smithy_schema(A)]
        a: String,
        #[smithy_schema(B)]
        b: String,
        #[smithy_schema(LIST)]
        member_list: Vec<String>,
        #[smithy_schema(MAP)]
        member_map: IndexMap<String, String>,
    }

    fn get_test_shape() -> Test {
        let mut map = IndexMap::new();
        map.insert(String::from("a"), String::from("b"));
        map.insert(String::from("c"), String::from("d"));
        Test {
            a: "a".to_string(),
            b: "b".to_string(),
            member_list: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            member_map: map,
        }
    }

    #[test]
    fn can_use_serde_json() {
        let test = get_test_shape();
        let expected = r#"{
  "a": "a",
  "b": "b",
  "list": [
    "a",
    "b",
    "c"
  ],
  "map": {
    "a": "b",
    "c": "d"
  }
}"#;
        assert_eq!(serde_json::to_string_pretty(&test).unwrap(), expected);
    }

    #[test]
    fn can_use_serde_xml() {
        let test = get_test_shape();
        let expected = r#"<?xml version="1.0" encoding="UTF-8"?><Test><a>a</a><b>b</b><list>a</list><list>b</list><list>c</list><map><a>b</a><c>d</c></map></Test>"#;
        assert_eq!(expected, serde_xml_rs::to_string(&test).unwrap());
    }

    // --------------------------------------------------------------------
    // JSON Trait support tests
    // --------------------------------------------------------------------

    smithy!("com.example#Rename": {
        structure RENAME {
            @JsonNameTrait::new("renamed");
            A: STRING = "a"
        }
    });
    #[derive(SmithyShape)]
    #[smithy_schema(RENAME)]
    pub struct TestRename {
        #[smithy_schema(A)]
        a: String,
    }

    #[test]
    fn json_name_works() {
        let rename = TestRename { a: "a".to_string() };
        let expected = r#"{
  "renamed": "a"
}"#;
        assert_eq!(serde_json::to_string_pretty(&rename).unwrap(), expected);
    }

    // --------------------------------------------------------------------
    // XML Trait support tests
    // --------------------------------------------------------------------

    smithy!("com.example#Rename": {
        structure XML_TRAITS {
            @XmlNameTrait::new("renamed");
            @XmlAttributeTrait;
            A: STRING = "a"
            @XmlNameTrait::new("int");
            B: STRING = "b"
        }
    });
    #[derive(SmithyShape)]
    #[smithy_schema(XML_TRAITS)]
    pub struct TestXml {
        #[smithy_schema(A)]
        a: String,
        #[smithy_schema(B)]
        b: i32,
    }

    #[test]
    fn xml_traits_work() {
        let rename = TestXml {
            a: "a".to_string(),
            b: 2,
        };
        let expected =
            r#"<?xml version="1.0" encoding="UTF-8"?><Rename renamed="a"><int>2</int></Rename>"#;
        assert_eq!(serde_xml_rs::to_string(&rename).unwrap(), expected);
    }
}