ruma_common/
identifiers.rs

1//! Types for [Matrix](https://matrix.org/) identifiers for devices, events, keys, rooms, servers,
2//! users and URIs.
3
4// FIXME: Remove once lint doesn't trigger on std::convert::TryFrom in identifiers/macros.rs anymore
5#![allow(unused_qualifications)]
6
7#[doc(inline)]
8pub use ruma_identifiers_validation::{
9    error::{
10        Error as IdParseError, MatrixIdError, MatrixToError, MatrixUriError, MxcUriError,
11        VoipVersionIdError,
12    },
13    KeyName,
14};
15use serde::de::{self, Deserializer, Unexpected};
16
17#[doc(inline)]
18pub use self::{
19    base64_public_key::{Base64PublicKey, OwnedBase64PublicKey},
20    base64_public_key_or_device_id::{Base64PublicKeyOrDeviceId, OwnedBase64PublicKeyOrDeviceId},
21    client_secret::{ClientSecret, OwnedClientSecret},
22    crypto_algorithms::{
23        DeviceKeyAlgorithm, EventEncryptionAlgorithm, KeyDerivationAlgorithm, OneTimeKeyAlgorithm,
24        SigningKeyAlgorithm,
25    },
26    device_id::{DeviceId, OwnedDeviceId},
27    event_id::{EventId, OwnedEventId},
28    key_id::{
29        AnyKeyName, CrossSigningKeyId, CrossSigningOrDeviceSigningKeyId, DeviceKeyId,
30        DeviceSigningKeyId, KeyAlgorithm, KeyId, OneTimeKeyId, OwnedCrossSigningKeyId,
31        OwnedCrossSigningOrDeviceSigningKeyId, OwnedDeviceKeyId, OwnedDeviceSigningKeyId,
32        OwnedKeyId, OwnedOneTimeKeyId, OwnedServerSigningKeyId, OwnedSigningKeyId,
33        ServerSigningKeyId, SigningKeyId,
34    },
35    matrix_uri::{MatrixToUri, MatrixUri},
36    mxc_uri::{MxcUri, OwnedMxcUri},
37    one_time_key_name::{OneTimeKeyName, OwnedOneTimeKeyName},
38    room_alias_id::{OwnedRoomAliasId, RoomAliasId},
39    room_id::{OwnedRoomId, RoomId},
40    room_or_alias_id::{OwnedRoomOrAliasId, RoomOrAliasId},
41    room_version_id::RoomVersionId,
42    server_name::{OwnedServerName, ServerName},
43    server_signing_key_version::{OwnedServerSigningKeyVersion, ServerSigningKeyVersion},
44    session_id::{OwnedSessionId, SessionId},
45    signatures::{
46        CrossSigningOrDeviceSignatures, DeviceSignatures, EntitySignatures, ServerSignatures,
47        Signatures,
48    },
49    transaction_id::{OwnedTransactionId, TransactionId},
50    user_id::{OwnedUserId, UserId},
51    voip_id::{OwnedVoipId, VoipId},
52    voip_version_id::VoipVersionId,
53};
54
55pub mod matrix_uri;
56pub mod user_id;
57
58mod base64_public_key;
59mod base64_public_key_or_device_id;
60mod client_secret;
61mod crypto_algorithms;
62mod device_id;
63mod event_id;
64mod key_id;
65mod mxc_uri;
66mod one_time_key_name;
67mod room_alias_id;
68mod room_id;
69mod room_or_alias_id;
70mod room_version_id;
71mod server_name;
72mod server_signing_key_version;
73mod session_id;
74mod signatures;
75mod transaction_id;
76mod voip_id;
77mod voip_version_id;
78
79/// Generates a random identifier localpart.
80#[cfg(feature = "rand")]
81fn generate_localpart(length: usize) -> Box<str> {
82    use rand::Rng as _;
83    rand::thread_rng()
84        .sample_iter(&rand::distributions::Alphanumeric)
85        .map(char::from)
86        .take(length)
87        .collect::<String>()
88        .into_boxed_str()
89}
90
91/// Deserializes any type of id using the provided `TryFrom` implementation.
92///
93/// This is a helper function to reduce the boilerplate of the `Deserialize` implementations.
94fn deserialize_id<'de, D, T>(deserializer: D, expected_str: &str) -> Result<T, D::Error>
95where
96    D: Deserializer<'de>,
97    T: for<'a> TryFrom<&'a str>,
98{
99    crate::serde::deserialize_cow_str(deserializer).and_then(|v| {
100        T::try_from(&v).map_err(|_| de::Error::invalid_value(Unexpected::Str(&v), &expected_str))
101    })
102}
103
104/// Shorthand for `<&DeviceId>::from`.
105#[macro_export]
106macro_rules! device_id {
107    ($s:expr) => {
108        <&$crate::DeviceId as ::std::convert::From<_>>::from($s)
109    };
110}
111
112/// Shorthand for `OwnedDeviceId::from`.
113#[macro_export]
114macro_rules! owned_device_id {
115    ($s:expr) => {
116        <$crate::OwnedDeviceId as ::std::convert::From<_>>::from($s)
117    };
118}
119
120#[doc(hidden)]
121pub mod __private_macros {
122    pub use ruma_macros::{
123        base64_public_key, event_id, mxc_uri, room_alias_id, room_id, room_version_id, server_name,
124        server_signing_key_version, user_id,
125    };
126}
127
128/// Compile-time checked [`EventId`] construction.
129#[macro_export]
130macro_rules! event_id {
131    ($s:literal) => {
132        $crate::__private_macros::event_id!($crate, $s)
133    };
134}
135
136/// Compile-time checked [`OwnedEventId`] construction.
137#[macro_export]
138macro_rules! owned_event_id {
139    ($s:literal) => {
140        $crate::event_id!($s).to_owned()
141    };
142}
143
144/// Compile-time checked [`RoomAliasId`] construction.
145#[macro_export]
146macro_rules! room_alias_id {
147    ($s:literal) => {
148        $crate::__private_macros::room_alias_id!($crate, $s)
149    };
150}
151
152/// Compile-time checked [`OwnedRoomAliasId`] construction.
153#[macro_export]
154macro_rules! owned_room_alias_id {
155    ($s:literal) => {
156        $crate::room_alias_id!($s).to_owned()
157    };
158}
159
160/// Compile-time checked [`RoomId`] construction.
161#[macro_export]
162macro_rules! room_id {
163    ($s:literal) => {
164        $crate::__private_macros::room_id!($crate, $s)
165    };
166}
167
168/// Compile-time checked [`OwnedRoomId`] construction.
169#[macro_export]
170macro_rules! owned_room_id {
171    ($s:literal) => {
172        $crate::room_id!($s).to_owned()
173    };
174}
175
176/// Compile-time checked [`RoomVersionId`] construction.
177#[macro_export]
178macro_rules! room_version_id {
179    ($s:literal) => {
180        $crate::__private_macros::room_version_id!($crate, $s)
181    };
182}
183
184/// Compile-time checked [`ServerSigningKeyVersion`] construction.
185#[macro_export]
186macro_rules! server_signing_key_version {
187    ($s:literal) => {
188        $crate::__private_macros::server_signing_key_version!($crate, $s)
189    };
190}
191
192/// Compile-time checked [`OwnedServerSigningKeyVersion`] construction.
193#[macro_export]
194macro_rules! owned_server_signing_key_version {
195    ($s:literal) => {
196        $crate::server_signing_key_version!($s).to_owned()
197    };
198}
199
200/// Compile-time checked [`ServerName`] construction.
201#[macro_export]
202macro_rules! server_name {
203    ($s:literal) => {
204        $crate::__private_macros::server_name!($crate, $s)
205    };
206}
207
208/// Compile-time checked [`OwnedServerName`] construction.
209#[macro_export]
210macro_rules! owned_server_name {
211    ($s:literal) => {
212        $crate::server_name!($s).to_owned()
213    };
214}
215
216/// Compile-time checked [`SessionId`] construction.
217#[macro_export]
218macro_rules! session_id {
219    ($s:literal) => {{
220        const SESSION_ID: &$crate::SessionId = match $crate::SessionId::_priv_const_new($s) {
221            Ok(id) => id,
222            Err(e) => panic!("{}", e),
223        };
224
225        SESSION_ID
226    }};
227}
228
229/// Compile-time checked [`OwnedSessionId`] construction.
230#[macro_export]
231macro_rules! owned_session_id {
232    ($s:literal) => {
233        $crate::session_id!($s).to_owned()
234    };
235}
236
237/// Compile-time checked [`MxcUri`] construction.
238#[macro_export]
239macro_rules! mxc_uri {
240    ($s:literal) => {
241        $crate::__private_macros::mxc_uri!($crate, $s)
242    };
243}
244
245/// Compile-time checked [`OwnedMxcUri`] construction.
246#[macro_export]
247macro_rules! owned_mxc_uri {
248    ($s:literal) => {
249        $crate::mxc_uri!($s).to_owned()
250    };
251}
252
253/// Compile-time checked [`UserId`] construction.
254#[macro_export]
255macro_rules! user_id {
256    ($s:literal) => {
257        $crate::__private_macros::user_id!($crate, $s)
258    };
259}
260
261/// Compile-time checked [`OwnedUserId`] construction.
262#[macro_export]
263macro_rules! owned_user_id {
264    ($s:literal) => {
265        $crate::user_id!($s).to_owned()
266    };
267}
268
269/// Compile-time checked [`Base64PublicKey`] construction.
270#[macro_export]
271macro_rules! base64_public_key {
272    ($s:literal) => {
273        $crate::__private_macros::base64_public_key!($crate, $s)
274    };
275}
276
277/// Compile-time checked [`OwnedBase64PublicKey`] construction.
278#[macro_export]
279macro_rules! owned_base64_public_key {
280    ($s:literal) => {
281        $crate::base64_public_key!($s).to_owned()
282    };
283}