Skip to content

Commit fe1d5a2

Browse files
dependabot[bot]davidhewitt
authored andcommitted
Bump base64 from 0.13.1 to 0.21.2
Bumps [base64](https://github.com/marshallpierce/rust-base64) from 0.13.1 to 0.21.2. - [Changelog](https://github.com/marshallpierce/rust-base64/blob/master/RELEASE-NOTES.md) - [Commits](marshallpierce/rust-base64@v0.13.1...v0.21.2) --- updated-dependencies: - dependency-name: base64 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]>
1 parent 240fa38 commit fe1d5a2

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ahash = "0.8.0"
4040
url = "2.3.1"
4141
# idna is already required by url, added here to be explicit
4242
idna = "0.4.0"
43-
base64 = "0.13.1"
43+
base64 = "0.21.2"
4444
num-bigint = "0.4.3"
4545
python3-dll-a = "0.2.7"
4646

src/serializers/config.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::str::{from_utf8, FromStr, Utf8Error};
33

4+
use base64::Engine;
45
use pyo3::prelude::*;
56
use pyo3::types::{PyDelta, PyDict};
67
use pyo3::{intern, PyNativeType};
@@ -119,20 +120,22 @@ impl TimedeltaMode {
119120
}
120121

121122
#[derive(Default, Debug, Clone)]
122-
pub(crate) struct BytesMode {
123-
base64_config: Option<base64::Config>,
123+
pub(crate) enum BytesMode {
124+
#[default]
125+
Utf8,
126+
Base64,
124127
}
125128

126129
impl FromStr for BytesMode {
127130
type Err = PyErr;
128131

129132
fn from_str(s: &str) -> Result<Self, Self::Err> {
130-
let base64_config = match s {
131-
"utf8" => None,
132-
"base64" => Some(base64::Config::new(base64::CharacterSet::UrlSafe, true)),
133-
s => return py_schema_err!("Invalid bytes serialization mode: `{}`, expected `utf8` or `base64`", s),
134-
};
135-
Ok(Self { base64_config })
133+
match s {
134+
"utf8" => Ok(Self::Utf8),
135+
// "base64" => Ok(base64::Config::new(base64::CharacterSet::UrlSafe, true)),
136+
"base64" => Ok(Self::Base64),
137+
s => py_schema_err!("Invalid bytes serialization mode: `{}`, expected `utf8` or `base64`", s),
138+
}
136139
}
137140
}
138141

@@ -146,23 +149,21 @@ impl BytesMode {
146149
}
147150

148151
pub fn bytes_to_string<'py>(&self, py: Python, bytes: &'py [u8]) -> PyResult<Cow<'py, str>> {
149-
if let Some(config) = self.base64_config {
150-
Ok(Cow::Owned(base64::encode_config(bytes, config)))
151-
} else {
152-
from_utf8(bytes)
152+
match self {
153+
Self::Utf8 => from_utf8(bytes)
153154
.map_err(|err| utf8_py_error(py, err, bytes))
154-
.map(Cow::Borrowed)
155+
.map(Cow::Borrowed),
156+
Self::Base64 => Ok(Cow::Owned(base64::engine::general_purpose::URL_SAFE.encode(bytes))),
155157
}
156158
}
157159

158160
pub fn serialize_bytes<S: serde::ser::Serializer>(&self, bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
159-
if let Some(config) = self.base64_config {
160-
serializer.serialize_str(&base64::encode_config(bytes, config))
161-
} else {
162-
match from_utf8(bytes) {
161+
match self {
162+
Self::Utf8 => match from_utf8(bytes) {
163163
Ok(s) => serializer.serialize_str(s),
164164
Err(e) => Err(Error::custom(e.to_string())),
165-
}
165+
},
166+
Self::Base64 => serializer.serialize_str(&base64::engine::general_purpose::URL_SAFE.encode(bytes)),
166167
}
167168
}
168169
}

0 commit comments

Comments
 (0)