Skip to content

Commit

Permalink
Refactor aws smithy types (#2638)
Browse files Browse the repository at this point in the history
## 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._
  • Loading branch information
thomas-k-cameron authored and david-perez committed May 22, 2023
1 parent 930c01c commit ecdaef5
Show file tree
Hide file tree
Showing 4 changed files with 556 additions and 537 deletions.
32 changes: 32 additions & 0 deletions rust-runtime/aws-smithy-types/src/blob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/// Binary Blob Type
///
/// Blobs represent protocol-agnostic binary content.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Blob {
inner: Vec<u8>,
}

impl Blob {
/// Creates a new blob from the given `input`.
pub fn new<T: Into<Vec<u8>>>(input: T) -> Self {
Blob {
inner: input.into(),
}
}

/// Consumes the `Blob` and returns a `Vec<u8>` with its contents.
pub fn into_inner(self) -> Vec<u8> {
self.inner
}
}

impl AsRef<[u8]> for Blob {
fn as_ref(&self) -> &[u8] {
&self.inner
}
}
73 changes: 73 additions & 0 deletions rust-runtime/aws-smithy-types/src/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

use crate::Number;
use std::collections::HashMap;

/* ANCHOR: document */

/// Document Type
///
/// Document types represents protocol-agnostic open content that is accessed like JSON data.
/// Open content is useful for modeling unstructured data that has no schema, data that can't be
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
/// The serialization format of a document is an implementation detail of a protocol.
#[derive(Debug, Clone, PartialEq)]
pub enum Document {
/// JSON object
Object(HashMap<String, Document>),
/// JSON array
Array(Vec<Document>),
/// JSON number
Number(Number),
/// JSON string
String(String),
/// JSON boolean
Bool(bool),
/// JSON null
Null,
}

impl From<bool> for Document {
fn from(value: bool) -> Self {
Document::Bool(value)
}
}

impl From<String> for Document {
fn from(value: String) -> Self {
Document::String(value)
}
}

impl From<Vec<Document>> for Document {
fn from(values: Vec<Document>) -> Self {
Document::Array(values)
}
}

impl From<HashMap<String, Document>> for Document {
fn from(values: HashMap<String, Document>) -> Self {
Document::Object(values)
}
}

impl From<u64> for Document {
fn from(value: u64) -> Self {
Document::Number(Number::PosInt(value))
}
}

impl From<i64> for Document {
fn from(value: i64) -> Self {
Document::Number(Number::NegInt(value))
}
}

impl From<i32> for Document {
fn from(value: i32) -> Self {
Document::Number(Number::NegInt(value as i64))
}
}
Loading

0 comments on commit ecdaef5

Please sign in to comment.