Skip to content

Commit eeb3714

Browse files
thomas-k-camerondavid-perez
authored andcommitted
Refactor aws smithy types (#2638)
## Motivation and Context This PR refactors `aws-smithy-types` crate. `Blob`, `Datetime`, `Number` and `Document` structs now goes to it's own files. No changes on feature is introduced. This is a child-PR of #2616 . However, it is completely independent. PR that introduces same changes were previously merged to `unstable-serde` branch, however, it has not been merged to main branch. ## Testing NA ## Checklist This PR does not introduce API changes. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 529d7d9 commit eeb3714

File tree

4 files changed

+556
-537
lines changed

4 files changed

+556
-537
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/// Binary Blob Type
7+
///
8+
/// Blobs represent protocol-agnostic binary content.
9+
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
10+
pub struct Blob {
11+
inner: Vec<u8>,
12+
}
13+
14+
impl Blob {
15+
/// Creates a new blob from the given `input`.
16+
pub fn new<T: Into<Vec<u8>>>(input: T) -> Self {
17+
Blob {
18+
inner: input.into(),
19+
}
20+
}
21+
22+
/// Consumes the `Blob` and returns a `Vec<u8>` with its contents.
23+
pub fn into_inner(self) -> Vec<u8> {
24+
self.inner
25+
}
26+
}
27+
28+
impl AsRef<[u8]> for Blob {
29+
fn as_ref(&self) -> &[u8] {
30+
&self.inner
31+
}
32+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
use crate::Number;
7+
use std::collections::HashMap;
8+
9+
/* ANCHOR: document */
10+
11+
/// Document Type
12+
///
13+
/// Document types represents protocol-agnostic open content that is accessed like JSON data.
14+
/// Open content is useful for modeling unstructured data that has no schema, data that can't be
15+
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
16+
/// The serialization format of a document is an implementation detail of a protocol.
17+
#[derive(Debug, Clone, PartialEq)]
18+
pub enum Document {
19+
/// JSON object
20+
Object(HashMap<String, Document>),
21+
/// JSON array
22+
Array(Vec<Document>),
23+
/// JSON number
24+
Number(Number),
25+
/// JSON string
26+
String(String),
27+
/// JSON boolean
28+
Bool(bool),
29+
/// JSON null
30+
Null,
31+
}
32+
33+
impl From<bool> for Document {
34+
fn from(value: bool) -> Self {
35+
Document::Bool(value)
36+
}
37+
}
38+
39+
impl From<String> for Document {
40+
fn from(value: String) -> Self {
41+
Document::String(value)
42+
}
43+
}
44+
45+
impl From<Vec<Document>> for Document {
46+
fn from(values: Vec<Document>) -> Self {
47+
Document::Array(values)
48+
}
49+
}
50+
51+
impl From<HashMap<String, Document>> for Document {
52+
fn from(values: HashMap<String, Document>) -> Self {
53+
Document::Object(values)
54+
}
55+
}
56+
57+
impl From<u64> for Document {
58+
fn from(value: u64) -> Self {
59+
Document::Number(Number::PosInt(value))
60+
}
61+
}
62+
63+
impl From<i64> for Document {
64+
fn from(value: i64) -> Self {
65+
Document::Number(Number::NegInt(value))
66+
}
67+
}
68+
69+
impl From<i32> for Document {
70+
fn from(value: i32) -> Self {
71+
Document::Number(Number::NegInt(value as i64))
72+
}
73+
}

0 commit comments

Comments
 (0)