ruma_identifiers/
room_or_room_alias_id.rs

1//! Matrix identifiers for places where a room ID or room alias ID are used interchangeably.
2
3use std::{convert::TryFrom, hint::unreachable_unchecked};
4
5use crate::{server_name::ServerName, RoomAliasId, RoomId};
6
7/// A Matrix [room ID] or a Matrix [room alias ID].
8///
9/// `RoomOrAliasId` is useful for APIs that accept either kind of room identifier. It is converted
10/// from a string slice, and can be converted back into a string as needed. When converted from a
11/// string slice, the variant is determined by the leading sigil character.
12///
13/// ```
14/// # use std::convert::TryFrom;
15/// # use ruma_identifiers::RoomOrAliasId;
16/// assert_eq!(<&RoomOrAliasId>::try_from("#ruma:example.com").unwrap(), "#ruma:example.com");
17///
18/// assert_eq!(
19///     <&RoomOrAliasId>::try_from("!n8f893n9:example.com").unwrap(),
20///     "!n8f893n9:example.com"
21/// );
22/// ```
23///
24/// [room ID]: https://spec.matrix.org/v1.2/appendices/#room-ids-and-event-ids
25/// [room alias ID]: https://spec.matrix.org/v1.2/appendices/#room-aliases
26#[repr(transparent)]
27#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
28pub struct RoomOrAliasId(str);
29
30opaque_identifier_validated!(
31    RoomOrAliasId,
32    ruma_identifiers_validation::room_id_or_alias_id::validate
33);
34
35impl RoomOrAliasId {
36    /// Returns the local part (everything after the `!` or `#` and before the first colon).
37    pub fn localpart(&self) -> &str {
38        &self.as_str()[1..self.colon_idx()]
39    }
40
41    /// Returns the server name of the room (alias) ID.
42    pub fn server_name(&self) -> &ServerName {
43        ServerName::from_borrowed(&self.as_str()[self.colon_idx() + 1..])
44    }
45
46    /// Whether this is a room id (starts with `'!'`)
47    pub fn is_room_id(&self) -> bool {
48        self.variant() == Variant::RoomId
49    }
50
51    /// Whether this is a room alias id (starts with `'#'`)
52    pub fn is_room_alias_id(&self) -> bool {
53        self.variant() == Variant::RoomAliasId
54    }
55
56    /// Turn this `RoomOrAliasId` into `Either<RoomId, RoomAliasId>`
57    #[cfg(feature = "either")]
58    pub fn into_either(self: Box<Self>) -> either::Either<Box<RoomId>, Box<RoomAliasId>> {
59        let variant = self.variant();
60        let boxed_str = self.into_owned();
61
62        match variant {
63            Variant::RoomId => either::Either::Left(RoomId::from_owned(boxed_str)),
64            Variant::RoomAliasId => either::Either::Right(RoomAliasId::from_owned(boxed_str)),
65        }
66    }
67
68    fn colon_idx(&self) -> usize {
69        self.as_str().find(':').unwrap()
70    }
71
72    fn variant(&self) -> Variant {
73        match self.as_str().bytes().next() {
74            Some(b'!') => Variant::RoomId,
75            Some(b'#') => Variant::RoomAliasId,
76            _ => unsafe { unreachable_unchecked() },
77        }
78    }
79}
80
81#[derive(PartialEq)]
82enum Variant {
83    RoomId,
84    RoomAliasId,
85}
86
87impl<'a> From<&'a RoomId> for &'a RoomOrAliasId {
88    fn from(room_id: &'a RoomId) -> Self {
89        RoomOrAliasId::from_borrowed(room_id.as_str())
90    }
91}
92
93impl<'a> From<&'a RoomAliasId> for &'a RoomOrAliasId {
94    fn from(room_alias_id: &'a RoomAliasId) -> Self {
95        RoomOrAliasId::from_borrowed(room_alias_id.as_str())
96    }
97}
98
99impl From<Box<RoomId>> for Box<RoomOrAliasId> {
100    fn from(room_id: Box<RoomId>) -> Self {
101        RoomOrAliasId::from_owned(room_id.into_owned())
102    }
103}
104
105impl From<Box<RoomAliasId>> for Box<RoomOrAliasId> {
106    fn from(room_alias_id: Box<RoomAliasId>) -> Self {
107        RoomOrAliasId::from_owned(room_alias_id.into_owned())
108    }
109}
110
111impl<'a> TryFrom<&'a RoomOrAliasId> for &'a RoomId {
112    type Error = &'a RoomAliasId;
113
114    fn try_from(id: &'a RoomOrAliasId) -> Result<&'a RoomId, &'a RoomAliasId> {
115        match id.variant() {
116            Variant::RoomId => Ok(RoomId::from_borrowed(id.as_str())),
117            Variant::RoomAliasId => Err(RoomAliasId::from_borrowed(id.as_str())),
118        }
119    }
120}
121
122impl<'a> TryFrom<&'a RoomOrAliasId> for &'a RoomAliasId {
123    type Error = &'a RoomId;
124
125    fn try_from(id: &'a RoomOrAliasId) -> Result<&'a RoomAliasId, &'a RoomId> {
126        match id.variant() {
127            Variant::RoomAliasId => Ok(RoomAliasId::from_borrowed(id.as_str())),
128            Variant::RoomId => Err(RoomId::from_borrowed(id.as_str())),
129        }
130    }
131}
132
133impl TryFrom<Box<RoomOrAliasId>> for Box<RoomId> {
134    type Error = Box<RoomAliasId>;
135
136    fn try_from(id: Box<RoomOrAliasId>) -> Result<Box<RoomId>, Box<RoomAliasId>> {
137        match id.variant() {
138            Variant::RoomId => Ok(RoomId::from_owned(id.into_owned())),
139            Variant::RoomAliasId => Err(RoomAliasId::from_owned(id.into_owned())),
140        }
141    }
142}
143
144impl TryFrom<Box<RoomOrAliasId>> for Box<RoomAliasId> {
145    type Error = Box<RoomId>;
146
147    fn try_from(id: Box<RoomOrAliasId>) -> Result<Box<RoomAliasId>, Box<RoomId>> {
148        match id.variant() {
149            Variant::RoomAliasId => Ok(RoomAliasId::from_owned(id.into_owned())),
150            Variant::RoomId => Err(RoomId::from_owned(id.into_owned())),
151        }
152    }
153}
154
155#[cfg(test)]
156mod tests {
157    use std::convert::TryFrom;
158
159    use super::RoomOrAliasId;
160    use crate::Error;
161
162    #[test]
163    fn valid_room_id_or_alias_id_with_a_room_alias_id() {
164        assert_eq!(
165            <&RoomOrAliasId>::try_from("#ruma:example.com")
166                .expect("Failed to create RoomAliasId.")
167                .as_ref(),
168            "#ruma:example.com"
169        );
170    }
171
172    #[test]
173    fn valid_room_id_or_alias_id_with_a_room_id() {
174        assert_eq!(
175            <&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
176                .expect("Failed to create RoomId.")
177                .as_ref(),
178            "!29fhd83h92h0:example.com"
179        );
180    }
181
182    #[test]
183    fn missing_sigil_for_room_id_or_alias_id() {
184        assert_eq!(
185            <&RoomOrAliasId>::try_from("ruma:example.com").unwrap_err(),
186            Error::MissingLeadingSigil
187        );
188    }
189
190    #[cfg(feature = "serde")]
191    #[test]
192    fn serialize_valid_room_id_or_alias_id_with_a_room_alias_id() {
193        assert_eq!(
194            serde_json::to_string(
195                <&RoomOrAliasId>::try_from("#ruma:example.com")
196                    .expect("Failed to create RoomAliasId.")
197            )
198            .expect("Failed to convert RoomAliasId to JSON."),
199            r##""#ruma:example.com""##
200        );
201    }
202
203    #[cfg(feature = "serde")]
204    #[test]
205    fn serialize_valid_room_id_or_alias_id_with_a_room_id() {
206        assert_eq!(
207            serde_json::to_string(
208                <&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
209                    .expect("Failed to create RoomId.")
210            )
211            .expect("Failed to convert RoomId to JSON."),
212            r#""!29fhd83h92h0:example.com""#
213        );
214    }
215
216    #[cfg(feature = "serde")]
217    #[test]
218    fn deserialize_valid_room_id_or_alias_id_with_a_room_alias_id() {
219        assert_eq!(
220            serde_json::from_str::<Box<RoomOrAliasId>>(r##""#ruma:example.com""##)
221                .expect("Failed to convert JSON to RoomAliasId"),
222            <&RoomOrAliasId>::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.")
223        );
224    }
225
226    #[cfg(feature = "serde")]
227    #[test]
228    fn deserialize_valid_room_id_or_alias_id_with_a_room_id() {
229        assert_eq!(
230            serde_json::from_str::<Box<RoomOrAliasId>>(r##""!29fhd83h92h0:example.com""##)
231                .expect("Failed to convert JSON to RoomId"),
232            <&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
233                .expect("Failed to create RoomAliasId.")
234        );
235    }
236}