Skip to content

Commit

Permalink
Merge pull request #6 from lklimek/deps/base64-0.21
Browse files Browse the repository at this point in the history
chore!: upgrade base64 dependency to 0.21.4
  • Loading branch information
cfsamson authored Oct 18, 2023
2 parents d8d3ee4 + 64653d2 commit 15f236a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 96 deletions.
58 changes: 30 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde-bytes-repr"
version = "0.1.5"
version = "0.2.0"
authors = ["cfsamson <[email protected]>"]
description = "Serde adapter for controlling the representation of bytes"
repository = "https://github.com/cfsamson/rfi-serde-byte-repr"
Expand All @@ -16,7 +16,7 @@ edition = "2018"

[dependencies]
serde = "1.0.117"
base64 = "0.13.0"
base64 = "0.21.4"
hex = "0.4.2"

[dev-dependencies]
Expand Down
56 changes: 33 additions & 23 deletions src/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{ByteFmtDeserializer, ByteFormat};
use base64::Engine;
use serde::de;
use std::fmt;

Expand Down Expand Up @@ -278,34 +279,43 @@ impl<V> Visitor<V> {
}
}

fn decode<E>(&self, v: &[u8]) -> Result<Vec<u8>, E> where E: de::Error {
fn decode<E>(&self, v: &[u8]) -> Result<Vec<u8>, E>
where
E: de::Error,
{
match self.fmt {
ByteFormat::Base64(cfg) => match base64::decode_config(v, cfg) {
Ok(bytes) => Ok(bytes),
Err(base64::DecodeError::InvalidByte(index, b))
=> Err(E::invalid_value(
de::Unexpected::Char(b.into()),
&format!("valid base64 character at index {}", index).as_str()
)),
Err(base64::DecodeError::InvalidLength)
=> Err(E::invalid_length(v.len(), &"valid base64 length")),
Err(base64::DecodeError::InvalidLastSymbol(_,b))
=> Err(E::invalid_value(
de::Unexpected::Char(b.into()),
&"valid character ending base64 string"
)),
},
ByteFormat::Base64(ref alphabet, config) => {
match base64::engine::GeneralPurpose::new(alphabet, config).decode(v) {
Ok(bytes) => Ok(bytes),
Err(base64::DecodeError::InvalidByte(index, b)) => Err(E::invalid_value(
de::Unexpected::Char(b.into()),
&format!("valid base64 character at index {}", index).as_str(),
)),
Err(base64::DecodeError::InvalidLength) => {
Err(E::invalid_length(v.len(), &"valid base64 length"))
}
Err(base64::DecodeError::InvalidLastSymbol(_, b)) => Err(E::invalid_value(
de::Unexpected::Char(b.into()),
&"valid character ending base64 string",
)),
Err(base64::DecodeError::InvalidPadding) => Err(E::invalid_value(
de::Unexpected::Other("invalid padding"),
&"valid padding",
)),
}
}
ByteFormat::Hex => match hex::decode(v) {
Ok(bytes) => Ok(bytes),
Err(hex::FromHexError::OddLength)
=> Err(E::invalid_length(v.len(), &"even length")),
Err(hex::FromHexError::InvalidHexCharacter{c, index})
=> Err(E::invalid_value(
Err(hex::FromHexError::OddLength) => {
Err(E::invalid_length(v.len(), &"even length"))
}
Err(hex::FromHexError::InvalidHexCharacter { c, index }) => Err(E::invalid_value(
de::Unexpected::Char(c),
&format!("valid hex character at index {}", index).as_str()
&format!("valid hex character at index {}", index).as_str(),
)),
Err(hex::FromHexError::InvalidStringLength) => Err(E::custom(
"Imposible to reach due to unrestricted return length",
)),
Err(hex::FromHexError::InvalidStringLength)
=> Err(E::custom("Imposible to reach due to unrestricted return length")),
},
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
//!
//! let mut out = vec![];
//! let mut ser = serde_json::Serializer::new(&mut out);
//! let base64_config = base64::Config::new(base64::CharacterSet::UrlSafe, true);
//! let ser = ByteFmtSerializer::base64(&mut ser, base64_config);
//! let base64_config = base64::engine::GeneralPurposeConfig::new();
//! let ser = ByteFmtSerializer::base64(&mut ser, base64::alphabet::URL_SAFE, base64_config);
//! demo.serialize(ser).unwrap();
//!
//! let serialized = String::from_utf8(out).unwrap();
Expand All @@ -57,21 +57,23 @@
//!
//! let json = br#"{"bytes":"dGVzdGluZw=="}"#;
//! let mut json_de = serde_json::Deserializer::from_slice(json);
//! let base64_config = base64::Config::new(base64::CharacterSet::UrlSafe, true);
//! let bytefmt_json_de = ByteFmtDeserializer::new_base64(&mut json_de, base64_config);
//! let base64_config = base64::engine::GeneralPurposeConfig::new();
//! let bytefmt_json_de = ByteFmtDeserializer::new_base64(&mut json_de, base64::alphabet::URL_SAFE, base64_config);
//! let demo: Demo = Demo::deserialize(bytefmt_json_de).unwrap();
//!
//! let deserialized = String::from_utf8(demo.bytes).unwrap();
//! assert_eq!("testing", deserialized.as_str());
//! # }
//! ```
use base64::{alphabet::Alphabet, engine::GeneralPurposeConfig, Engine};

mod deserializer;
mod serializer;

#[derive(Clone)]
enum ByteFormat {
Base64(base64::Config),
Base64(Alphabet, GeneralPurposeConfig),
Hex,
}

Expand All @@ -85,10 +87,10 @@ impl<S> ByteFmtSerializer<S> {
/// Crates an adapter which serializes to and from a Base64 representation.
/// Provide a configuration from the `base64` crate specifying the specifics
/// on how you want the bytes encoded.
pub fn base64(ser: S, cfg: base64::Config) -> Self {
pub fn base64(ser: S, alphabet: Alphabet, config: GeneralPurposeConfig) -> Self {
Self {
inner: ser,
encode_kind: ByteFormat::Base64(cfg),
encode_kind: ByteFormat::Base64(alphabet, config),
}
}

Expand All @@ -102,8 +104,10 @@ impl<S> ByteFmtSerializer<S> {

fn encode(&self, v: &[u8]) -> String {
match self.encode_kind {
ByteFormat::Base64(cfg) => base64::encode_config(&v, cfg),
ByteFormat::Hex => hex::encode(&v),
ByteFormat::Base64(ref alphabet, config) => {
base64::engine::GeneralPurpose::new(alphabet, config).encode(v)
}
ByteFormat::Hex => hex::encode(v),
}
}
}
Expand All @@ -118,10 +122,11 @@ impl<D> ByteFmtDeserializer<D> {
/// Crates an adapter which deserializes from a Base64 representation. Provide a
/// configuration from the `base64` crate specifying the specifics on how you want the bytes
/// encoded.
pub fn new_base64(deserializer: D, config: base64::Config) -> Self {
///
pub fn new_base64(deserializer: D, alphabet: Alphabet, config: GeneralPurposeConfig) -> Self {
ByteFmtDeserializer {
inner: deserializer,
fmt: ByteFormat::Base64(config),
fmt: ByteFormat::Base64(alphabet, config),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub struct BytesSerialize<'a, T: ?Sized> {

impl<'a, T: ?Sized> BytesSerialize<'a, T> {
fn new(value: &'a T, fmt: ByteFormat) -> Self {
BytesSerialize { value, fmt: fmt }
BytesSerialize { value, fmt }
}
}

Expand Down Expand Up @@ -293,7 +293,7 @@ struct BytesSerializeSized<T> {

impl<T> BytesSerializeSized<T> {
fn new(value: T, fmt: ByteFormat) -> Self {
BytesSerializeSized { value, fmt: fmt }
BytesSerializeSized { value, fmt }
}
}

Expand Down
Loading

0 comments on commit 15f236a

Please sign in to comment.