Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base64 for ByteBuf when serializer is human-readable #53

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ alloc = ["serde/alloc"]

[dependencies]
serde = { version = "1.0.166", default-features = false }
base64 = "0.21.4"

[dev-dependencies]
bincode = "1.3.3"
Expand Down
59 changes: 57 additions & 2 deletions src/bytebuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use core::fmt::{self, Debug};
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};

pub use base64::DecodeError;
use base64::prelude::{Engine as _, BASE64_STANDARD};

#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -85,6 +88,10 @@ impl ByteBuf {
pub fn into_iter(self) -> <Vec<u8> as IntoIterator>::IntoIter {
self.bytes.into_iter()
}

fn decode(bytes: &[u8]) -> Result<Self, DecodeError> {
Ok(Self { bytes: BASE64_STANDARD.decode(bytes)? })
}
}

impl Debug for ByteBuf {
Expand Down Expand Up @@ -193,7 +200,12 @@ impl Serialize for ByteBuf {
where
S: Serializer,
{
serializer.serialize_bytes(&self.bytes)
if serializer.is_human_readable() {
let encoded = BASE64_STANDARD.encode(&self.bytes);
String::serialize(&encoded, serializer)
} else {
serializer.serialize_bytes(&self.bytes)
}
}
}

Expand Down Expand Up @@ -249,11 +261,54 @@ impl<'de> Visitor<'de> for ByteBufVisitor {
}
}

struct Base64Visitor;

impl<'de> Visitor<'de> for Base64Visitor {
type Value = ByteBuf;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("base64-encoded string")
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<ByteBuf, E>
where
E: Error,
{
ByteBuf::decode(v).map_err(serde::de::Error::custom)
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<ByteBuf, E>
where
E: Error,
{
ByteBuf::decode(v.as_slice()).map_err(serde::de::Error::custom)
}

fn visit_str<E>(self, v: &str) -> Result<ByteBuf, E>
where
E: Error,
{
ByteBuf::decode(v.as_bytes()).map_err(serde::de::Error::custom)
}

fn visit_string<E>(self, v: String) -> Result<ByteBuf, E>
where
E: Error,
{
ByteBuf::decode(v.as_bytes()).map_err(serde::de::Error::custom)
}
}


impl<'de> Deserialize<'de> for ByteBuf {
fn deserialize<D>(deserializer: D) -> Result<ByteBuf, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_byte_buf(ByteBufVisitor)
if deserializer.is_human_readable() {
deserializer.deserialize_str(Base64Visitor)
} else {
deserializer.deserialize_byte_buf(ByteBufVisitor)
}
}
}
Loading