ruma_identifiers_validation/
lib.rs

1#![doc(html_favicon_url = "https://ruma.dev/favicon.ico")]
2#![doc(html_logo_url = "https://ruma.dev/images/logo.png")]
3
4pub mod base64_public_key;
5pub mod client_secret;
6pub mod error;
7pub mod event_id;
8pub mod key_id;
9pub mod mxc_uri;
10pub mod room_alias_id;
11pub mod room_id;
12pub mod room_id_or_alias_id;
13pub mod room_version_id;
14pub mod server_name;
15pub mod server_signing_key_version;
16pub mod user_id;
17pub mod voip_version_id;
18
19pub use error::Error;
20
21/// All identifiers must be 255 bytes or less.
22#[cfg(not(feature = "compat-arbitrary-length-ids"))]
23const MAX_BYTES: usize = 255;
24
25/// Checks if an identifier is valid.
26fn validate_id(id: &str, first_byte: u8) -> Result<(), Error> {
27    #[cfg(not(feature = "compat-arbitrary-length-ids"))]
28    if id.len() > MAX_BYTES {
29        return Err(Error::MaximumLengthExceeded);
30    }
31
32    if id.as_bytes().first() != Some(&first_byte) {
33        return Err(Error::MissingLeadingSigil);
34    }
35
36    Ok(())
37}
38
39/// Checks an identifier that contains a localpart and hostname for validity.
40fn parse_id(id: &str, first_byte: u8) -> Result<usize, Error> {
41    validate_id(id, first_byte)?;
42    let colon_idx = id.find(':').ok_or(Error::MissingColon)?;
43    server_name::validate(&id[colon_idx + 1..])?;
44    Ok(colon_idx)
45}
46
47/// Checks an identifier that contains a localpart and hostname for validity.
48fn validate_delimited_id(id: &str, first_byte: u8) -> Result<(), Error> {
49    parse_id(id, first_byte)?;
50    Ok(())
51}
52
53/// Helper trait to validate the name of a key.
54pub trait KeyName: AsRef<str> {
55    /// Validate the given string for this name.
56    fn validate(s: &str) -> Result<(), Error>;
57}