1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![warn(rust_2018_idioms)]
#![deny(
missing_copy_implementations,
missing_debug_implementations,
missing_docs
)]
#![allow(clippy::use_self)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "diesel")]
#[cfg_attr(feature = "diesel", macro_use)]
extern crate diesel;
use std::num::NonZeroU8;
#[cfg(feature = "serde")]
use serde::de::{self, Deserialize as _, Deserializer, Unexpected};
#[doc(inline)]
pub use crate::{
device_id::DeviceId, error::Error, event_id::EventId, room_alias_id::RoomAliasId,
room_id::RoomId, room_id_or_room_alias_id::RoomIdOrAliasId, room_version_id::RoomVersionId,
server_name::is_valid_server_name, user_id::UserId,
};
#[macro_use]
mod macros;
pub mod device_id;
#[cfg(feature = "diesel")]
#[cfg_attr(docsrs, doc(cfg(feature = "diesel")))]
mod diesel_integration;
mod error;
mod event_id;
mod room_alias_id;
mod room_id;
mod room_id_or_room_alias_id;
mod room_version_id;
mod server_name;
pub mod user_id;
const MAX_BYTES: usize = 255;
const MIN_CHARS: usize = 4;
#[cfg(feature = "rand")]
fn generate_localpart(length: usize) -> String {
use rand::Rng as _;
rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(length)
.collect()
}
fn validate_id(id: &str, valid_sigils: &[char]) -> Result<(), Error> {
if id.len() > MAX_BYTES {
return Err(Error::MaximumLengthExceeded);
}
if id.len() < MIN_CHARS {
return Err(Error::MinimumLengthNotSatisfied);
}
if !valid_sigils.contains(&id.chars().next().unwrap()) {
return Err(Error::MissingSigil);
}
Ok(())
}
fn parse_id(id: &str, valid_sigils: &[char]) -> Result<NonZeroU8, Error> {
validate_id(id, valid_sigils)?;
let colon_idx = id.find(':').ok_or(Error::MissingDelimiter)?;
if colon_idx < 2 {
return Err(Error::InvalidLocalPart);
}
if !is_valid_server_name(&id[colon_idx + 1..]) {
return Err(Error::InvalidServerName);
}
Ok(NonZeroU8::new(colon_idx as u8).unwrap())
}
#[cfg(feature = "serde")]
fn deserialize_id<'de, D, T>(deserializer: D, expected_str: &str) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: for<'a> std::convert::TryFrom<&'a str>,
{
std::borrow::Cow::<'_, str>::deserialize(deserializer).and_then(|v| {
T::try_from(&v).map_err(|_| de::Error::invalid_value(Unexpected::Str(&v), &expected_str))
})
}