Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust-runtime/aws-smithy-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ version = "1"
features = ["derive"]
optional = true


[dev-dependencies]
base64 = "0.13.0"
lazy_static = "1.4"
Expand All @@ -28,6 +27,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
criterion = "0.4"
rand = "0.8.4"
ciborium = "0.2.0"

[package.metadata.docs.rs]
all-features = true
Expand Down
107 changes: 107 additions & 0 deletions rust-runtime/aws-smithy-types/src/blob/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#[cfg(all(test, aws_sdk_unstable, feature = "serialize", feature = "deserialize"))]
mod test;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind moving the tests into a module in this same file?


#[cfg(all(aws_sdk_unstable, feature = "serialize"))]
use serde::Serialize;
#[cfg(all(aws_sdk_unstable, feature = "deserialize"))]
use serde::{de::Visitor, Deserialize};

/// Binary Blob Type
///
/// Blobs represent protocol-agnostic binary content.
#[derive(Debug, PartialEq, 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
}
}

#[cfg(all(aws_sdk_unstable, feature = "serialize"))]
impl Serialize for Blob {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
serializer.serialize_str(&crate::base64::encode(&self.inner))
} else {
serializer.serialize_bytes(&self.inner)
}
}
}

#[cfg(all(aws_sdk_unstable, feature = "deserialize"))]
struct HumanReadableBlobVisitor;

#[cfg(all(aws_sdk_unstable, feature = "deserialize"))]
impl<'de> Visitor<'de> for HumanReadableBlobVisitor {
type Value = Blob;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("expected base64 encoded string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match base64::decode(v) {
Ok(inner) => Ok(Blob { inner }),
Err(e) => Err(serde::de::Error::custom(e)),
}
}
}

#[cfg(all(aws_sdk_unstable, feature = "deserialize"))]
struct NotHumanReadableBlobVisitor;

#[cfg(all(aws_sdk_unstable, feature = "deserialize"))]
impl<'de> Visitor<'de> for NotHumanReadableBlobVisitor {
type Value = Blob;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("expected base64 encoded string")
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Blob { inner: v })
}
}

#[cfg(all(aws_sdk_unstable, feature = "deserialize"))]
impl<'de> Deserialize<'de> for Blob {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
if deserializer.is_human_readable() {
deserializer.deserialize_str(HumanReadableBlobVisitor)
} else {
deserializer.deserialize_byte_buf(NotHumanReadableBlobVisitor)
}
}
}
46 changes: 46 additions & 0 deletions rust-runtime/aws-smithy-types/src/blob/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

use crate::*;
use serde::{Deserialize, Serialize};
use std::ffi::CString;

#[derive(Deserialize, Serialize, Debug, PartialEq)]
struct ForTest {
blob: Blob,
}

#[test]
fn human_readable_blob() {
let aws_in_base64 = r#"{"blob":"QVdT"}"#;
let for_test = ForTest {
blob: Blob {
inner: vec![b'A', b'W', b'S'],
},
};
assert_eq!(for_test, serde_json::from_str(aws_in_base64).unwrap());
assert_eq!(serde_json::to_string(&for_test).unwrap(), aws_in_base64);
}

#[test]
fn not_human_readable_blob() {
let for_test = ForTest {
blob: Blob {
inner: vec![b'A', b'W', b'S'],
},
};
let mut buf = vec![];
let res = ciborium::ser::into_writer(&for_test, &mut buf);
assert!(res.is_ok());

// checks whether the bytes are deserialiezd properly
let n: HashMap<String, CString> =
ciborium::de::from_reader(std::io::Cursor::new(buf.clone())).unwrap();
assert!(n.get("blob").is_some());
assert!(n.get("blob") == CString::new([65, 87, 83]).ok().as_ref());

let de: ForTest = ciborium::de::from_reader(std::io::Cursor::new(buf)).unwrap();
assert_eq!(for_test, de);
}
37 changes: 6 additions & 31 deletions rust-runtime/aws-smithy-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
)]

use crate::error::{TryFromNumberError, TryFromNumberErrorKind};

mod blob;
pub use blob::Blob;

pub mod base64;
pub mod date_time;
mod document;
Expand All @@ -22,39 +26,10 @@ pub mod error;
pub mod primitive;
pub mod retry;
pub mod timeout;

pub use crate::date_time::DateTime;
pub use crate::document::Document;
pub use date_time::DateTime;
pub use document::Document;
pub use error::Error;

/// Binary Blob Type
///
/// Blobs represent protocol-agnostic binary content.
#[derive(Debug, PartialEq, 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
}
}

/// A number type that implements Javascript / JSON semantics, modeled on serde_json:
/// <https://docs.serde.rs/src/serde_json/number.rs.html#20-22>
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down