Skip to content

Commit

Permalink
identifiers: Don't require room IDs to contain a server name
Browse files Browse the repository at this point in the history
Room IDs being splittable into localpart and servername does not have
much inherent value and there are proposals like MSC4051¹ that propose
changing the format. Relaxing the rules makes Ruma forwards-compatible
with those proposals. The server_name accessor is kept because it is
used by at least one downstream, but is updated to return an `Option`.

¹ matrix-org/matrix-spec-proposals#4051
  • Loading branch information
jplatte committed Sep 28, 2023
1 parent 95f920b commit d2cbf6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
21 changes: 8 additions & 13 deletions crates/ruma-common/src/identifiers/room_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ impl RoomId {
Self::from_borrowed(&format!("!{}:{server_name}", super::generate_localpart(18))).to_owned()
}

/// Returns the rooms's unique ID.
pub fn localpart(&self) -> &str {
&self.as_str()[1..self.colon_idx()]
}

/// Returns the server name of the room ID.
pub fn server_name(&self) -> &ServerName {
ServerName::from_borrowed(&self.as_str()[self.colon_idx() + 1..])
pub fn server_name(&self) -> Option<&ServerName> {
let colon_idx = self.as_str().find(':')?;
Some(ServerName::from_borrowed(&self.as_str()[colon_idx + 1..]))
}

/// Create a `matrix.to` URI for this room ID.
Expand Down Expand Up @@ -202,10 +198,6 @@ impl RoomId {
None,
)
}

fn colon_idx(&self) -> usize {
self.as_str().find(':').unwrap()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -287,8 +279,11 @@ mod tests {
}

#[test]
fn missing_room_id_delimiter() {
assert_eq!(<&RoomId>::try_from("!29fhd83h92h0").unwrap_err(), IdParseError::MissingColon);
fn missing_server_name() {
assert_eq!(
<&RoomId>::try_from("!29fhd83h92h0").expect("Failed to create RoomId."),
"!29fhd83h92h0"
);
}

#[test]
Expand Down
9 changes: 7 additions & 2 deletions crates/ruma-identifiers-validation/src/room_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::{validate_delimited_id, Error};
use crate::{server_name, validate_id, Error};

pub fn validate(s: &str) -> Result<(), Error> {
validate_delimited_id(s, &['!'])
validate_id(s, &['!'])?;
if let Some(colon_idx) = s.find(':') {
server_name::validate(&s[colon_idx + 1..])?;
}

Ok(())
}

0 comments on commit d2cbf6a

Please sign in to comment.