diff --git a/crates/ruma-events/src/room.rs b/crates/ruma-events/src/room.rs index 41466973..fa6fe087 100644 --- a/crates/ruma-events/src/room.rs +++ b/crates/ruma-events/src/room.rs @@ -6,7 +6,7 @@ use std::collections::BTreeMap; use js_int::UInt; use ruma_identifiers::MxcUri; -use ruma_serde::Base64; +use ruma_serde::{base64::UrlSafe, Base64}; use serde::{Deserialize, Serialize}; pub mod aliases; @@ -193,7 +193,7 @@ pub struct JsonWebKey { pub alg: String, /// The key, encoded as url-safe unpadded base64. - pub k: Base64, + pub k: Base64, /// Extractable. /// @@ -225,7 +225,7 @@ pub struct JsonWebKeyInit { pub alg: String, /// The key, encoded as url-safe unpadded base64. - pub k: Base64, + pub k: Base64, /// Extractable. /// diff --git a/crates/ruma-serde/src/base64.rs b/crates/ruma-serde/src/base64.rs index 1ab0b1ab..bf13387a 100644 --- a/crates/ruma-serde/src/base64.rs +++ b/crates/ruma-serde/src/base64.rs @@ -2,15 +2,39 @@ use std::fmt; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; -/// DOCS -// generic? +/// A wrapper around `B` (usually `Vec`) that (de)serializes from / to a base64 string. +/// +/// The base64 character set (and miscellaneous other encoding / decoding options) can be customized +/// through the generic parameter `C`. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] -pub struct Base64> { +pub struct Base64> { bytes: B, } -// See https://github.com/matrix-org/matrix-doc/issues/3211 -const BASE64_CONFIG: base64::Config = base64::STANDARD_NO_PAD.decode_allow_trailing_bits(true); +pub trait Base64Config { + const CONF: base64::Config; +} + +/// Standard base64 character set without padding. +/// +/// Allows trailing bits in decoding for maximum compatibility. +#[non_exhaustive] +pub struct Standard; + +impl Base64Config for Standard { + // See https://github.com/matrix-org/matrix-doc/issues/3211 + const CONF: base64::Config = base64::STANDARD_NO_PAD.decode_allow_trailing_bits(true); +} + +/// Url-safe base64 character set without padding. +/// +/// Allows trailing bits in decoding for maximum compatibility. +#[non_exhaustive] +pub struct UrlSafe; + +impl Base64Config for UrlSafe { + const CONF: base64::Config = base64::URL_SAFE_NO_PAD.decode_allow_trailing_bits(true); +} impl> Base64 { /// Create a `Base64` instance from raw bytes, to be base64-encoded in serialialization. diff --git a/crates/ruma-serde/src/lib.rs b/crates/ruma-serde/src/lib.rs index 759f7f40..f7081ea5 100644 --- a/crates/ruma-serde/src/lib.rs +++ b/crates/ruma-serde/src/lib.rs @@ -6,7 +6,7 @@ use serde_json::Value as JsonValue; -mod base64; +pub mod base64; mod buf; pub mod can_be_empty; mod canonical_json;