Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-k-cameron committed Apr 28, 2023
1 parent 3fe6e92 commit 12b47e3
Showing 1 changed file with 56 additions and 53 deletions.
109 changes: 56 additions & 53 deletions rust-runtime/aws-smithy-types/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,72 +44,75 @@ impl AsRef<[u8]> for Blob {
}

#[cfg(all(aws_sdk_unstable, feature = "serde-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)
use serde_serialize::*;
#[cfg(all(aws_sdk_unstable, feature = "serde-serialize"))]
mod serde_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 = "serde-deserialize"))]
struct HumanReadableBlobVisitor;

use serde_deserialize::*;
#[cfg(all(aws_sdk_unstable, feature = "serde-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(E::custom(e)),
mod serde_deserialize {
struct HumanReadableBlobVisitor;
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(E::custom(e)),
}
}
}
}

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

#[cfg(all(aws_sdk_unstable, feature = "serde-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 })

struct NotHumanReadableBlobVisitor;
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 = "serde-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)

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)
}
}
}
}


#[cfg(test)]
#[cfg(all(
aws_sdk_unstable,
Expand Down

0 comments on commit 12b47e3

Please sign in to comment.