identifiers: Move opaque identifier declarations out of macro

This commit is contained in:
Jonas Platte 2021-09-18 00:11:13 +02:00
parent 52608cc72c
commit a38f78e2d3
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
9 changed files with 103 additions and 121 deletions

View File

@ -1,14 +1,13 @@
//! Client secret identifier. //! 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! { opaque_identifier_validated!(ClientSecret, 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.
pub type ClientSecret [ validate ];
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -1,31 +1,32 @@
#[cfg(feature = "rand")] #[cfg(feature = "rand")]
use crate::generate_localpart; use crate::generate_localpart;
opaque_identifier! { /// A Matrix key ID.
/// A Matrix key ID. ///
/// /// Device identifiers in Matrix are completely opaque character sequences. This type is provided
/// Device identifiers in Matrix are completely opaque character sequences. This type is /// simply for its semantic value.
/// provided simply for its semantic value. ///
/// /// # Example
/// # Example ///
/// /// ```
/// ``` /// use ruma_identifiers::{DeviceId, device_id};
/// use ruma_identifiers::{DeviceId, device_id}; ///
/// /// let random_id = DeviceId::new();
/// let random_id = DeviceId::new(); /// assert_eq!(random_id.as_str().len(), 8);
/// assert_eq!(random_id.as_str().len(), 8); ///
/// /// let static_id = device_id!("01234567");
/// let static_id = device_id!("01234567"); /// assert_eq!(static_id.as_str(), "01234567");
/// assert_eq!(static_id.as_str(), "01234567"); ///
/// /// let ref_id: &DeviceId = "abcdefghi".into();
/// let ref_id: &DeviceId = "abcdefghi".into(); /// assert_eq!(ref_id.as_str(), "abcdefghi");
/// assert_eq!(ref_id.as_str(), "abcdefghi"); ///
/// /// let owned_id: Box<DeviceId> = "ijklmnop".into();
/// let owned_id: Box<DeviceId> = "ijklmnop".into(); /// assert_eq!(owned_id.as_str(), "ijklmnop");
/// assert_eq!(owned_id.as_str(), "ijklmnop"); /// ```
/// ``` #[repr(transparent)]
pub type DeviceId; pub struct DeviceId(str);
}
opaque_identifier!(DeviceId);
impl DeviceId { impl DeviceId {
/// Generates a random `DeviceId`, suitable for assignment to a new device. /// Generates a random `DeviceId`, suitable for assignment to a new device.

View File

@ -1,13 +1,12 @@
//! Identifiers for device keys for end-to-end encryption. //! Identifiers for device keys for end-to-end encryption.
use ruma_identifiers_validation::device_key_id::validate;
use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId}; use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId};
opaque_identifier_validated! { /// A key algorithm and a device id, combined with a ':'.
/// A key algorithm and a device id, combined with a ':'. #[repr(transparent)]
pub type DeviceKeyId [ validate ]; pub struct DeviceKeyId(str);
}
opaque_identifier_validated!(DeviceKeyId, ruma_identifiers_validation::device_key_id::validate);
impl DeviceKeyId { impl DeviceKeyId {
/// Create a `DeviceKeyId` from a `DeviceKeyAlgorithm` and a `DeviceId`. /// Create a `DeviceKeyId` from a `DeviceKeyAlgorithm` and a `DeviceId`.

View File

@ -2,46 +2,45 @@
use std::convert::TryInto; use std::convert::TryInto;
use ruma_identifiers_validation::event_id::validate;
use crate::ServerName; use crate::ServerName;
opaque_identifier_validated! { /// A Matrix event ID.
/// A Matrix event ID. ///
/// /// An `EventId` is generated randomly or converted from a string slice, and can be converted back
/// An `EventId` is generated randomly or converted from a string slice, and can be converted /// into a string as needed.
/// back into a string as needed. ///
/// /// # Room versions
/// # Room versions ///
/// /// Matrix specifies multiple [room versions](https://matrix.org/docs/spec/#room-versions) and the
/// Matrix specifies multiple [room versions](https://matrix.org/docs/spec/#room-versions) and /// format of event identifiers differ between them. The original format used by room versions 1 and
/// the format of event identifiers differ between them. The original format used by room /// 2 uses a short pseudorandom "localpart" followed by the hostname and port of the originating
/// versions 1 and 2 uses a short pseudorandom "localpart" followed by the hostname and port of /// homeserver. Later room versions change event identifiers to be a hash of the event encoded with
/// the originating homeserver. Later room versions change event identifiers to be a hash of the /// Base64. Some of the methods provided by `EventId` are only relevant to the original event
/// event encoded with Base64. Some of the methods provided by `EventId` are only relevant to /// format.
/// the original event format. ///
/// /// ```
/// ``` /// # use std::convert::TryFrom;
/// # use std::convert::TryFrom; /// # use ruma_identifiers::EventId;
/// # use ruma_identifiers::EventId; /// // Original format
/// // Original format /// assert_eq!(
/// assert_eq!( /// <&EventId>::try_from("$h29iv0s8:example.com").unwrap(),
/// <&EventId>::try_from("$h29iv0s8:example.com").unwrap(), /// "$h29iv0s8:example.com"
/// "$h29iv0s8:example.com" /// );
/// ); /// // Room version 3 format
/// // Room version 3 format /// assert_eq!(
/// assert_eq!( /// <&EventId>::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap(),
/// <&EventId>::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap(), /// "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk"
/// "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk" /// );
/// ); /// // Room version 4 format
/// // Room version 4 format /// assert_eq!(
/// assert_eq!( /// <&EventId>::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap(),
/// <&EventId>::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap(), /// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg"
/// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg" /// );
/// ); /// ```
/// ``` #[repr(transparent)]
pub type EventId [ validate ]; pub struct EventId(str);
}
opaque_identifier_validated!(EventId, ruma_identifiers_validation::event_id::validate);
impl EventId { impl EventId {
/// Attempts to generate an `EventId` for the given origin server with a localpart consisting /// Attempts to generate an `EventId` for the given origin server with a localpart consisting

View File

@ -1,7 +1,8 @@
opaque_identifier! { /// A Matrix key identifier.
/// A Matrix key identifier. ///
/// /// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is
/// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is /// provided simply for its semantic value.
/// provided simply for its semantic value. #[repr(transparent)]
pub type KeyName; pub struct KeyName(str);
}
opaque_identifier!(KeyName);

View File

@ -266,14 +266,7 @@ macro_rules! opaque_identifier_common_impls {
} }
macro_rules! opaque_identifier { macro_rules! opaque_identifier {
( ($id:ident) => {
$( #[doc = $docs:literal] )*
$vis:vis type $id:ident;
) => {
$( #[doc = $docs] )*
#[repr(transparent)]
pub struct $id(str);
opaque_identifier_common_impls!($id); opaque_identifier_common_impls!($id);
impl<'a> From<&'a str> for &'a $id { impl<'a> From<&'a str> for &'a $id {
@ -313,14 +306,7 @@ macro_rules! opaque_identifier {
} }
macro_rules! opaque_identifier_validated { macro_rules! opaque_identifier_validated {
( ($id:ident, $validate_id:expr) => {
$( #[doc = $docs:literal] )*
$vis:vis type $id:ident [ $validate_id:ident ];
) => {
$( #[doc = $docs] )*
#[repr(transparent)]
pub struct $id(str);
opaque_identifier_common_impls!($id); opaque_identifier_common_impls!($id);
impl From<Box<$id>> for String { impl From<Box<$id>> for String {
@ -386,5 +372,5 @@ macro_rules! opaque_identifier_validated {
try_from(s) try_from(s)
} }
} }
} };
} }

View File

@ -1,10 +1,9 @@
//! Matrix room name. //! 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! { opaque_identifier_validated!(RoomName, ruma_identifiers_validation::room_name::validate);
/// The name of a room.
///
/// It can't exceed 255 bytes or be empty.
pub type RoomName [ validate ];
}

View File

@ -1,11 +1,10 @@
//! Matrix-spec compliant server names. //! 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! { opaque_identifier_validated!(ServerName, ruma_identifiers_validation::server_name::validate);
/// A Matrix-spec compliant server name.
pub type ServerName [ validate ];
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -1,11 +1,10 @@
//! Matrix session ID. //! 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! { opaque_identifier_validated!(SessionId, 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.
pub type SessionId [ validate ];
}