ruma_identifiers/
lib.rs

1#![doc(html_favicon_url = "https://www.ruma.io/favicon.ico")]
2#![doc(html_logo_url = "https://www.ruma.io/images/logo.png")]
3//! ⚠ **Deprecated**: this crate has been merged into [ruma-common]. ⚠
4//!
5//! [ruma-common]: https://crates.io/crates/ruma-common
6
7#![warn(missing_docs)]
8// FIXME: Remove once lint doesn't trigger on std::convert::TryFrom in macros.rs anymore
9#![allow(unused_qualifications)]
10#![cfg_attr(docsrs, feature(doc_auto_cfg))]
11
12// Renamed in `Cargo.toml` so we can features with the same name as the package.
13// Rename them back here because the `Cargo.toml` names are ugly.
14#[cfg(feature = "serde")]
15extern crate serde1 as serde;
16
17#[cfg(feature = "rand")]
18extern crate rand_crate as rand;
19
20#[cfg(feature = "serde")]
21use std::convert::TryFrom;
22use std::fmt;
23
24#[cfg(feature = "serde")]
25use serde::de::{self, Deserializer, Unexpected};
26
27#[doc(inline)]
28pub use crate::{
29    client_secret::ClientSecret,
30    crypto_algorithms::{DeviceKeyAlgorithm, EventEncryptionAlgorithm, SigningKeyAlgorithm},
31    device_id::DeviceId,
32    device_key_id::DeviceKeyId,
33    event_id::EventId,
34    key_id::{DeviceSigningKeyId, KeyId, ServerSigningKeyId, SigningKeyId},
35    key_name::KeyName,
36    matrix_uri::{MatrixToUri, MatrixUri},
37    mxc_uri::MxcUri,
38    room_alias_id::RoomAliasId,
39    room_id::RoomId,
40    room_name::RoomName,
41    room_or_room_alias_id::RoomOrAliasId,
42    room_version_id::RoomVersionId,
43    server_name::ServerName,
44    session_id::SessionId,
45    signatures::{DeviceSignatures, EntitySignatures, ServerSignatures, Signatures},
46    transaction_id::TransactionId,
47    user_id::UserId,
48};
49#[doc(inline)]
50pub use ruma_identifiers_validation::error::Error;
51
52#[macro_use]
53mod macros;
54
55pub mod matrix_uri;
56pub mod user_id;
57
58mod client_secret;
59mod crypto_algorithms;
60mod device_id;
61mod device_key_id;
62mod event_id;
63mod key_id;
64mod key_name;
65mod mxc_uri;
66mod room_alias_id;
67mod room_id;
68mod room_name;
69mod room_or_room_alias_id;
70mod room_version_id;
71mod server_name;
72mod session_id;
73mod signatures;
74mod transaction_id;
75
76/// Generates a random identifier localpart.
77#[cfg(feature = "rand")]
78fn generate_localpart(length: usize) -> Box<str> {
79    use rand::Rng as _;
80    rand::thread_rng()
81        .sample_iter(&rand::distributions::Alphanumeric)
82        .map(char::from)
83        .take(length)
84        .collect::<String>()
85        .into_boxed_str()
86}
87
88/// Deserializes any type of id using the provided TryFrom implementation.
89///
90/// This is a helper function to reduce the boilerplate of the Deserialize implementations.
91#[cfg(feature = "serde")]
92fn deserialize_id<'de, D, T>(deserializer: D, expected_str: &str) -> Result<T, D::Error>
93where
94    D: Deserializer<'de>,
95    T: for<'a> TryFrom<&'a str>,
96{
97    ruma_serde::deserialize_cow_str(deserializer).and_then(|v| {
98        T::try_from(&v).map_err(|_| de::Error::invalid_value(Unexpected::Str(&v), &expected_str))
99    })
100}
101
102/// Shorthand for `<&DeviceId>::from`.
103#[macro_export]
104macro_rules! device_id {
105    ($s:expr) => {
106        <&$crate::DeviceId as ::std::convert::From<_>>::from($s)
107    };
108}
109
110// A plain re-export shows up in rustdoc despite doc(hidden). Use a module instead.
111// Bug report: https://github.com/rust-lang/rust/issues/83939
112#[doc(hidden)]
113pub mod _macros {
114    pub use ruma_identifiers_macros::*;
115}
116
117/// Compile-time checked `DeviceKeyId` construction.
118#[macro_export]
119macro_rules! device_key_id {
120    ($s:literal) => {
121        $crate::_macros::device_key_id!($crate, $s)
122    };
123}
124
125/// Compile-time checked `EventId` construction.
126#[macro_export]
127macro_rules! event_id {
128    ($s:literal) => {
129        $crate::_macros::event_id!($crate, $s)
130    };
131}
132
133/// Compile-time checked `RoomAliasId` construction.
134#[macro_export]
135macro_rules! room_alias_id {
136    ($s:literal) => {
137        $crate::_macros::room_alias_id!($crate, $s)
138    };
139}
140
141/// Compile-time checked `RoomId` construction.
142#[macro_export]
143macro_rules! room_id {
144    ($s:literal) => {
145        $crate::_macros::room_id!($crate, $s)
146    };
147}
148
149/// Compile-time checked `RoomVersionId` construction.
150#[macro_export]
151macro_rules! room_version_id {
152    ($s:literal) => {
153        $crate::_macros::room_version_id!($crate, $s)
154    };
155}
156
157/// Compile-time checked `ServerSigningKeyId` construction.
158#[macro_export]
159macro_rules! server_signing_key_id {
160    ($s:literal) => {
161        $crate::_macros::server_signing_key_id!($crate, $s)
162    };
163}
164
165/// Compile-time checked `ServerName` construction.
166#[macro_export]
167macro_rules! server_name {
168    ($s:literal) => {
169        $crate::_macros::server_name!($crate, $s)
170    };
171}
172
173/// Compile-time checked `MxcUri` construction.
174#[macro_export]
175macro_rules! mxc_uri {
176    ($s:literal) => {
177        $crate::_macros::mxc_uri!($crate, $s)
178    };
179}
180
181/// Compile-time checked `UserId` construction.
182#[macro_export]
183macro_rules! user_id {
184    ($s:literal) => {
185        $crate::_macros::user_id!($crate, $s)
186    };
187}
188
189// Wrapper around `Box<str>` that cannot be used in a meaningful way outside of
190// this crate. Used for string enums because their `_Custom` variant can't be
191// truly private (only `#[doc(hidden)]`).
192#[doc(hidden)]
193#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
194pub struct PrivOwnedStr(Box<str>);
195
196impl fmt::Debug for PrivOwnedStr {
197    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198        self.0.fmt(f)
199    }
200}