serde: Wrap base64::DecodeError to make base64 a private dependency

This commit is contained in:
Jonas Platte 2022-02-12 00:36:20 +01:00
parent 8d15c3d0f9
commit 2f84fdbd76
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 24 additions and 5 deletions

View File

@ -83,8 +83,8 @@ impl<C: Base64Config> Base64<C> {
} }
/// Parse some base64-encoded data to create a `Base64` instance. /// Parse some base64-encoded data to create a `Base64` instance.
pub fn parse(encoded: impl AsRef<[u8]>) -> Result<Self, base64::DecodeError> { pub fn parse(encoded: impl AsRef<[u8]>) -> Result<Self, Base64DecodeError> {
base64::decode_config(encoded, C::CONF.0).map(Self::new) base64::decode_config(encoded, C::CONF.0).map(Self::new).map_err(Base64DecodeError)
} }
} }
@ -118,3 +118,21 @@ impl<C: Base64Config, B: AsRef<[u8]>> Serialize for Base64<C, B> {
serializer.serialize_str(&self.encode()) serializer.serialize_str(&self.encode())
} }
} }
/// An error that occurred while decoding a base64 string.
#[derive(Clone)]
pub struct Base64DecodeError(base64::DecodeError);
impl fmt::Debug for Base64DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Display for Base64DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for Base64DecodeError {}

View File

@ -22,7 +22,7 @@ pub mod test;
pub mod urlencoded; pub mod urlencoded;
pub use self::{ pub use self::{
base64::Base64, base64::{Base64, Base64DecodeError},
buf::{json_to_buf, slice_to_buf}, buf::{json_to_buf, slice_to_buf},
can_be_empty::{is_empty, CanBeEmpty}, can_be_empty::{is_empty, CanBeEmpty},
canonical_json::{ canonical_json::{

View File

@ -1,4 +1,5 @@
use ruma_identifiers::{EventId, RoomVersionId, ServerName}; use ruma_identifiers::{EventId, RoomVersionId, ServerName};
use ruma_serde::Base64DecodeError;
use thiserror::Error; use thiserror::Error;
/// `ruma-signature`'s error type, wraps a number of other error types. /// `ruma-signature`'s error type, wraps a number of other error types.
@ -228,7 +229,7 @@ pub enum ParseError {
string: String, string: String,
/// The originating error. /// The originating error.
#[source] #[source]
source: base64::DecodeError, source: Base64DecodeError,
}, },
} }
@ -254,7 +255,7 @@ impl ParseError {
pub(crate) fn base64<T1: Into<String>, T2: Into<String>>( pub(crate) fn base64<T1: Into<String>, T2: Into<String>>(
of_type: T1, of_type: T1,
string: T2, string: T2,
source: base64::DecodeError, source: Base64DecodeError,
) -> Error { ) -> Error {
Self::Base64 { of_type: of_type.into(), string: string.into(), source }.into() Self::Base64 { of_type: of_type.into(), string: string.into(), source }.into()
} }