Skip to content

Bump base64 from 0.13.1 to 0.21.2 #805

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

Merged
merged 1 commit into from
Jul 24, 2023
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ahash = "0.8.0"
url = "2.3.1"
# idna is already required by url, added here to be explicit
idna = "0.4.0"
base64 = "0.13.1"
base64 = "0.21.2"
num-bigint = "0.4.3"
python3-dll-a = "0.2.7"

Expand Down
36 changes: 18 additions & 18 deletions src/serializers/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::str::{from_utf8, FromStr, Utf8Error};

use base64::Engine;
use pyo3::prelude::*;
use pyo3::types::{PyDelta, PyDict};
use pyo3::{intern, PyNativeType};
Expand Down Expand Up @@ -119,20 +120,21 @@ impl TimedeltaMode {
}

#[derive(Default, Debug, Clone)]
pub(crate) struct BytesMode {
base64_config: Option<base64::Config>,
pub(crate) enum BytesMode {
#[default]
Utf8,
Base64,
}

impl FromStr for BytesMode {
type Err = PyErr;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let base64_config = match s {
"utf8" => None,
"base64" => Some(base64::Config::new(base64::CharacterSet::UrlSafe, true)),
s => return py_schema_err!("Invalid bytes serialization mode: `{}`, expected `utf8` or `base64`", s),
};
Ok(Self { base64_config })
match s {
"utf8" => Ok(Self::Utf8),
"base64" => Ok(Self::Base64),
s => py_schema_err!("Invalid bytes serialization mode: `{}`, expected `utf8` or `base64`", s),
}
}
}

Expand All @@ -146,23 +148,21 @@ impl BytesMode {
}

pub fn bytes_to_string<'py>(&self, py: Python, bytes: &'py [u8]) -> PyResult<Cow<'py, str>> {
if let Some(config) = self.base64_config {
Ok(Cow::Owned(base64::encode_config(bytes, config)))
} else {
from_utf8(bytes)
match self {
Self::Utf8 => from_utf8(bytes)
.map_err(|err| utf8_py_error(py, err, bytes))
.map(Cow::Borrowed)
.map(Cow::Borrowed),
Self::Base64 => Ok(Cow::Owned(base64::engine::general_purpose::URL_SAFE.encode(bytes))),
}
}

pub fn serialize_bytes<S: serde::ser::Serializer>(&self, bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
if let Some(config) = self.base64_config {
serializer.serialize_str(&base64::encode_config(bytes, config))
} else {
match from_utf8(bytes) {
match self {
Self::Utf8 => match from_utf8(bytes) {
Ok(s) => serializer.serialize_str(s),
Err(e) => Err(Error::custom(e.to_string())),
}
},
Self::Base64 => serializer.serialize_str(&base64::engine::general_purpose::URL_SAFE.encode(bytes)),
}
}
}
Expand Down