From a38f78e2d3f2ee9af80d0fcf23f822dc13075357 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Sep 2021 00:11:13 +0200 Subject: [PATCH] identifiers: Move opaque identifier declarations out of macro --- crates/ruma-identifiers/src/client_secret.rs | 15 ++-- crates/ruma-identifiers/src/device_id.rs | 51 ++++++------- crates/ruma-identifiers/src/device_key_id.rs | 11 ++- crates/ruma-identifiers/src/event_id.rs | 75 ++++++++++---------- crates/ruma-identifiers/src/key_name.rs | 15 ++-- crates/ruma-identifiers/src/macros.rs | 20 +----- crates/ruma-identifiers/src/room_name.rs | 13 ++-- crates/ruma-identifiers/src/server_name.rs | 9 ++- crates/ruma-identifiers/src/session_id.rs | 15 ++-- 9 files changed, 103 insertions(+), 121 deletions(-) diff --git a/crates/ruma-identifiers/src/client_secret.rs b/crates/ruma-identifiers/src/client_secret.rs index f81c0ee9..abfa8cd4 100644 --- a/crates/ruma-identifiers/src/client_secret.rs +++ b/crates/ruma-identifiers/src/client_secret.rs @@ -1,14 +1,13 @@ //! Client secret identifier. -use ruma_identifiers_validation::client_secret::validate; +/// A client secret. +/// +/// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must +/// must not exceed 255 characters. +#[repr(transparent)] +pub struct ClientSecret(str); -opaque_identifier_validated! { - /// A client secret. - /// - /// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must - /// must not exceed 255 characters. - pub type ClientSecret [ validate ]; -} +opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate); #[cfg(test)] mod tests { diff --git a/crates/ruma-identifiers/src/device_id.rs b/crates/ruma-identifiers/src/device_id.rs index fa705f3f..d7d3675d 100644 --- a/crates/ruma-identifiers/src/device_id.rs +++ b/crates/ruma-identifiers/src/device_id.rs @@ -1,31 +1,32 @@ #[cfg(feature = "rand")] use crate::generate_localpart; -opaque_identifier! { - /// A Matrix key ID. - /// - /// Device identifiers in Matrix are completely opaque character sequences. This type is - /// provided simply for its semantic value. - /// - /// # Example - /// - /// ``` - /// use ruma_identifiers::{DeviceId, device_id}; - /// - /// let random_id = DeviceId::new(); - /// assert_eq!(random_id.as_str().len(), 8); - /// - /// let static_id = device_id!("01234567"); - /// assert_eq!(static_id.as_str(), "01234567"); - /// - /// let ref_id: &DeviceId = "abcdefghi".into(); - /// assert_eq!(ref_id.as_str(), "abcdefghi"); - /// - /// let owned_id: Box = "ijklmnop".into(); - /// assert_eq!(owned_id.as_str(), "ijklmnop"); - /// ``` - pub type DeviceId; -} +/// A Matrix key ID. +/// +/// Device identifiers in Matrix are completely opaque character sequences. This type is provided +/// simply for its semantic value. +/// +/// # Example +/// +/// ``` +/// use ruma_identifiers::{DeviceId, device_id}; +/// +/// let random_id = DeviceId::new(); +/// assert_eq!(random_id.as_str().len(), 8); +/// +/// let static_id = device_id!("01234567"); +/// assert_eq!(static_id.as_str(), "01234567"); +/// +/// let ref_id: &DeviceId = "abcdefghi".into(); +/// assert_eq!(ref_id.as_str(), "abcdefghi"); +/// +/// let owned_id: Box = "ijklmnop".into(); +/// assert_eq!(owned_id.as_str(), "ijklmnop"); +/// ``` +#[repr(transparent)] +pub struct DeviceId(str); + +opaque_identifier!(DeviceId); impl DeviceId { /// Generates a random `DeviceId`, suitable for assignment to a new device. diff --git a/crates/ruma-identifiers/src/device_key_id.rs b/crates/ruma-identifiers/src/device_key_id.rs index 7af7d118..da791ff6 100644 --- a/crates/ruma-identifiers/src/device_key_id.rs +++ b/crates/ruma-identifiers/src/device_key_id.rs @@ -1,13 +1,12 @@ //! Identifiers for device keys for end-to-end encryption. -use ruma_identifiers_validation::device_key_id::validate; - use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId}; -opaque_identifier_validated! { - /// A key algorithm and a device id, combined with a ':'. - pub type DeviceKeyId [ validate ]; -} +/// A key algorithm and a device id, combined with a ':'. +#[repr(transparent)] +pub struct DeviceKeyId(str); + +opaque_identifier_validated!(DeviceKeyId, ruma_identifiers_validation::device_key_id::validate); impl DeviceKeyId { /// Create a `DeviceKeyId` from a `DeviceKeyAlgorithm` and a `DeviceId`. diff --git a/crates/ruma-identifiers/src/event_id.rs b/crates/ruma-identifiers/src/event_id.rs index 53c7e500..7366ec02 100644 --- a/crates/ruma-identifiers/src/event_id.rs +++ b/crates/ruma-identifiers/src/event_id.rs @@ -2,46 +2,45 @@ use std::convert::TryInto; -use ruma_identifiers_validation::event_id::validate; - use crate::ServerName; -opaque_identifier_validated! { - /// A Matrix event ID. - /// - /// An `EventId` is generated randomly or converted from a string slice, and can be converted - /// back into a string as needed. - /// - /// # Room versions - /// - /// Matrix specifies multiple [room versions](https://matrix.org/docs/spec/#room-versions) and - /// the format of event identifiers differ between them. The original format used by room - /// versions 1 and 2 uses a short pseudorandom "localpart" followed by the hostname and port of - /// the originating homeserver. Later room versions change event identifiers to be a hash of the - /// event encoded with Base64. Some of the methods provided by `EventId` are only relevant to - /// the original event format. - /// - /// ``` - /// # use std::convert::TryFrom; - /// # use ruma_identifiers::EventId; - /// // Original format - /// assert_eq!( - /// <&EventId>::try_from("$h29iv0s8:example.com").unwrap(), - /// "$h29iv0s8:example.com" - /// ); - /// // Room version 3 format - /// assert_eq!( - /// <&EventId>::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap(), - /// "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk" - /// ); - /// // Room version 4 format - /// assert_eq!( - /// <&EventId>::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap(), - /// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg" - /// ); - /// ``` - pub type EventId [ validate ]; -} +/// A Matrix event ID. +/// +/// An `EventId` is generated randomly or converted from a string slice, and can be converted back +/// into a string as needed. +/// +/// # Room versions +/// +/// Matrix specifies multiple [room versions](https://matrix.org/docs/spec/#room-versions) and the +/// format of event identifiers differ between them. The original format used by room versions 1 and +/// 2 uses a short pseudorandom "localpart" followed by the hostname and port of the originating +/// homeserver. Later room versions change event identifiers to be a hash of the event encoded with +/// Base64. Some of the methods provided by `EventId` are only relevant to the original event +/// format. +/// +/// ``` +/// # use std::convert::TryFrom; +/// # use ruma_identifiers::EventId; +/// // Original format +/// assert_eq!( +/// <&EventId>::try_from("$h29iv0s8:example.com").unwrap(), +/// "$h29iv0s8:example.com" +/// ); +/// // Room version 3 format +/// assert_eq!( +/// <&EventId>::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap(), +/// "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk" +/// ); +/// // Room version 4 format +/// assert_eq!( +/// <&EventId>::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap(), +/// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg" +/// ); +/// ``` +#[repr(transparent)] +pub struct EventId(str); + +opaque_identifier_validated!(EventId, ruma_identifiers_validation::event_id::validate); impl EventId { /// Attempts to generate an `EventId` for the given origin server with a localpart consisting diff --git a/crates/ruma-identifiers/src/key_name.rs b/crates/ruma-identifiers/src/key_name.rs index e45d3a68..9e21ddde 100644 --- a/crates/ruma-identifiers/src/key_name.rs +++ b/crates/ruma-identifiers/src/key_name.rs @@ -1,7 +1,8 @@ -opaque_identifier! { - /// A Matrix key identifier. - /// - /// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is - /// provided simply for its semantic value. - pub type KeyName; -} +/// A Matrix key identifier. +/// +/// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is +/// provided simply for its semantic value. +#[repr(transparent)] +pub struct KeyName(str); + +opaque_identifier!(KeyName); diff --git a/crates/ruma-identifiers/src/macros.rs b/crates/ruma-identifiers/src/macros.rs index 676f57ad..d9d9bc18 100644 --- a/crates/ruma-identifiers/src/macros.rs +++ b/crates/ruma-identifiers/src/macros.rs @@ -266,14 +266,7 @@ macro_rules! opaque_identifier_common_impls { } macro_rules! opaque_identifier { - ( - $( #[doc = $docs:literal] )* - $vis:vis type $id:ident; - ) => { - $( #[doc = $docs] )* - #[repr(transparent)] - pub struct $id(str); - + ($id:ident) => { opaque_identifier_common_impls!($id); impl<'a> From<&'a str> for &'a $id { @@ -313,14 +306,7 @@ macro_rules! opaque_identifier { } macro_rules! opaque_identifier_validated { - ( - $( #[doc = $docs:literal] )* - $vis:vis type $id:ident [ $validate_id:ident ]; - ) => { - $( #[doc = $docs] )* - #[repr(transparent)] - pub struct $id(str); - + ($id:ident, $validate_id:expr) => { opaque_identifier_common_impls!($id); impl From> for String { @@ -386,5 +372,5 @@ macro_rules! opaque_identifier_validated { try_from(s) } } - } + }; } diff --git a/crates/ruma-identifiers/src/room_name.rs b/crates/ruma-identifiers/src/room_name.rs index 09153c47..0bd8d491 100644 --- a/crates/ruma-identifiers/src/room_name.rs +++ b/crates/ruma-identifiers/src/room_name.rs @@ -1,10 +1,9 @@ //! Matrix room name. -use ruma_identifiers_validation::room_name::validate; +/// The name of a room. +/// +/// It can't exceed 255 bytes or be empty. +#[repr(transparent)] +pub struct RoomName(str); -opaque_identifier_validated! { - /// The name of a room. - /// - /// It can't exceed 255 bytes or be empty. - pub type RoomName [ validate ]; -} +opaque_identifier_validated!(RoomName, ruma_identifiers_validation::room_name::validate); diff --git a/crates/ruma-identifiers/src/server_name.rs b/crates/ruma-identifiers/src/server_name.rs index d47353b1..8de7bb92 100644 --- a/crates/ruma-identifiers/src/server_name.rs +++ b/crates/ruma-identifiers/src/server_name.rs @@ -1,11 +1,10 @@ //! Matrix-spec compliant server names. -use ruma_identifiers_validation::server_name::validate; +/// A Matrix-spec compliant server name. +#[repr(transparent)] +pub struct ServerName(str); -opaque_identifier_validated! { - /// A Matrix-spec compliant server name. - pub type ServerName [ validate ]; -} +opaque_identifier_validated!(ServerName, ruma_identifiers_validation::server_name::validate); #[cfg(test)] mod tests { diff --git a/crates/ruma-identifiers/src/session_id.rs b/crates/ruma-identifiers/src/session_id.rs index f8130f9c..78ae90be 100644 --- a/crates/ruma-identifiers/src/session_id.rs +++ b/crates/ruma-identifiers/src/session_id.rs @@ -1,11 +1,10 @@ //! Matrix session ID. -use ruma_identifiers_validation::session_id::validate; +/// A session ID. +/// +/// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must +/// must not exceed 255 characters. +#[repr(transparent)] +pub struct SessionId(str); -opaque_identifier_validated! { - /// A session ID. - /// - /// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must - /// must not exceed 255 characters. - pub type SessionId [ validate ]; -} +opaque_identifier_validated!(SessionId, ruma_identifiers_validation::session_id::validate);