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;
opaque_identifier_validated! {
/// A client secret. /// A client secret.
/// ///
/// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must /// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
/// must not exceed 255 characters. /// must not exceed 255 characters.
pub type ClientSecret [ validate ]; #[repr(transparent)]
} pub struct ClientSecret(str);
opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -1,11 +1,10 @@
#[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 /// Device identifiers in Matrix are completely opaque character sequences. This type is provided
/// provided simply for its semantic value. /// simply for its semantic value.
/// ///
/// # Example /// # Example
/// ///
@ -24,8 +23,10 @@ opaque_identifier! {
/// 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");
/// ``` /// ```
pub type DeviceId; #[repr(transparent)]
} 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 ':'.
pub type DeviceKeyId [ validate ]; #[repr(transparent)]
} 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,24 +2,21 @@
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 /// An `EventId` is generated randomly or converted from a string slice, and can be converted back
/// back into a string as needed. /// into a string as needed.
/// ///
/// # Room versions /// # Room versions
/// ///
/// Matrix specifies multiple [room versions](https://matrix.org/docs/spec/#room-versions) and /// Matrix specifies multiple [room versions](https://matrix.org/docs/spec/#room-versions) and the
/// the format of event identifiers differ between them. The original format used by room /// format of event identifiers differ between them. The original format used by room versions 1 and
/// versions 1 and 2 uses a short pseudorandom "localpart" followed by the hostname and port of /// 2 uses a short pseudorandom "localpart" followed by the hostname and port of the originating
/// the originating homeserver. Later room versions change event identifiers to be a hash of the /// homeserver. Later room versions change event identifiers to be a hash of the event encoded with
/// event encoded with Base64. Some of the methods provided by `EventId` are only relevant to /// Base64. Some of the methods provided by `EventId` are only relevant to the original event
/// the original event format. /// format.
/// ///
/// ``` /// ```
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
@ -40,8 +37,10 @@ opaque_identifier_validated! {
/// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg" /// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg"
/// ); /// );
/// ``` /// ```
pub type EventId [ validate ]; #[repr(transparent)]
} 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.
pub type KeyName; #[repr(transparent)]
} 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;
opaque_identifier_validated! {
/// The name of a room. /// The name of a room.
/// ///
/// It can't exceed 255 bytes or be empty. /// It can't exceed 255 bytes or be empty.
pub type RoomName [ validate ]; #[repr(transparent)]
} pub struct RoomName(str);
opaque_identifier_validated!(RoomName, ruma_identifiers_validation::room_name::validate);

View File

@ -1,11 +1,10 @@
//! Matrix-spec compliant server names. //! Matrix-spec compliant server names.
use ruma_identifiers_validation::server_name::validate;
opaque_identifier_validated! {
/// A Matrix-spec compliant server name. /// A Matrix-spec compliant server name.
pub type ServerName [ validate ]; #[repr(transparent)]
} pub struct ServerName(str);
opaque_identifier_validated!(ServerName, ruma_identifiers_validation::server_name::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;
opaque_identifier_validated! {
/// A session ID. /// A session ID.
/// ///
/// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must /// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
/// must not exceed 255 characters. /// must not exceed 255 characters.
pub type SessionId [ validate ]; #[repr(transparent)]
} pub struct SessionId(str);
opaque_identifier_validated!(SessionId, ruma_identifiers_validation::session_id::validate);