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.
use ruma_identifiers_validation::client_secret::validate;
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 ];
}
#[repr(transparent)]
pub struct ClientSecret(str);
opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate);
#[cfg(test)]
mod tests {

View File

@ -1,11 +1,10 @@
#[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.
/// Device identifiers in Matrix are completely opaque character sequences. This type is provided
/// simply for its semantic value.
///
/// # Example
///
@ -24,8 +23,10 @@ opaque_identifier! {
/// let owned_id: Box<DeviceId> = "ijklmnop".into();
/// assert_eq!(owned_id.as_str(), "ijklmnop");
/// ```
pub type DeviceId;
}
#[repr(transparent)]
pub struct DeviceId(str);
opaque_identifier!(DeviceId);
impl DeviceId {
/// 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.
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 ];
}
#[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`.

View File

@ -2,24 +2,21 @@
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.
/// 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.
/// 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;
@ -40,8 +37,10 @@ opaque_identifier_validated! {
/// "$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 {
/// 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.
///
/// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is
/// 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 {
(
$( #[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<Box<$id>> for String {
@ -386,5 +372,5 @@ macro_rules! opaque_identifier_validated {
try_from(s)
}
}
}
};
}

View File

@ -1,10 +1,9 @@
//! Matrix room name.
use ruma_identifiers_validation::room_name::validate;
opaque_identifier_validated! {
/// The name of a room.
///
/// 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.
use ruma_identifiers_validation::server_name::validate;
opaque_identifier_validated! {
/// 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)]
mod tests {

View File

@ -1,11 +1,10 @@
//! Matrix session ID.
use ruma_identifiers_validation::session_id::validate;
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 ];
}
#[repr(transparent)]
pub struct SessionId(str);
opaque_identifier_validated!(SessionId, ruma_identifiers_validation::session_id::validate);