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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::{
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")]
use diesel::sql_types::Text;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use url::Host;
use crate::{deserialize_id, display, error::Error, parse_id};
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct RoomAliasId {
alias: String,
hostname: Host,
port: u16,
}
impl RoomAliasId {
pub fn hostname(&self) -> &Host {
&self.hostname
}
pub fn alias(&self) -> &str {
&self.alias
}
pub fn port(&self) -> u16 {
self.port
}
}
impl Display for RoomAliasId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
display(f, '#', &self.alias, &self.hostname, self.port)
}
}
impl Serialize for RoomAliasId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for RoomAliasId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_id(deserializer, "a Matrix room alias ID as a string")
}
}
impl<'a> TryFrom<&'a str> for RoomAliasId {
type Error = Error;
fn try_from(room_id: &'a str) -> Result<Self, Error> {
let (alias, host, port) = parse_id('#', room_id)?;
Ok(Self {
alias: alias.to_owned(),
hostname: host,
port,
})
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use serde_json::{from_str, to_string};
use super::RoomAliasId;
use crate::error::Error;
#[test]
fn valid_room_alias_id() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#ruma:example.com"
);
}
#[test]
fn serialize_valid_room_alias_id() {
assert_eq!(
to_string(
&RoomAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.")
)
.expect("Failed to convert RoomAliasId to JSON."),
r##""#ruma:example.com""##
);
}
#[test]
fn deserialize_valid_room_alias_id() {
assert_eq!(
from_str::<RoomAliasId>(r##""#ruma:example.com""##)
.expect("Failed to convert JSON to RoomAliasId"),
RoomAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.")
);
}
#[test]
fn valid_room_alias_id_with_explicit_standard_port() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com:443")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#ruma:example.com"
);
}
#[test]
fn valid_room_alias_id_with_non_standard_port() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com:5000")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#ruma:example.com:5000"
);
}
#[test]
fn valid_room_alias_id_unicode() {
assert_eq!(
RoomAliasId::try_from("#老虎£я:example.com")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#老虎£я:example.com"
);
}
#[test]
fn missing_room_alias_id_sigil() {
assert_eq!(
RoomAliasId::try_from("39hvsi03hlne:example.com").unwrap_err(),
Error::MissingSigil
);
}
#[test]
fn missing_room_alias_id_delimiter() {
assert_eq!(
RoomAliasId::try_from("#ruma").unwrap_err(),
Error::MissingDelimiter
);
}
#[test]
fn invalid_room_alias_id_host() {
assert_eq!(
RoomAliasId::try_from("#ruma:/").unwrap_err(),
Error::InvalidHost
);
}
#[test]
fn invalid_room_alias_id_port() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com:notaport").unwrap_err(),
Error::InvalidHost
);
}
}