ruma_common/identifiers/
room_or_alias_id.rs

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