Improve validation error for empty mxid.

Previously an empty MXID was reported as MissingLeadingSigil but a more
concise variant is just Empty.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-03 04:41:13 +00:00
parent b57e03e342
commit c434098fb1

View File

@ -22,17 +22,17 @@ pub use error::Error;
const MAX_BYTES: usize = 255;
/// Checks if an identifier is valid.
fn validate_id(id: &str, first_byte: u8) -> Result<(), Error> {
fn validate_id(id: &str, sigil: u8) -> Result<(), Error> {
#[cfg(not(feature = "compat-arbitrary-length-ids"))]
if id.len() > MAX_BYTES {
return Err(Error::MaximumLengthExceeded);
}
if id.as_bytes().first() != Some(&first_byte) {
return Err(Error::MissingLeadingSigil);
match id.as_bytes().first() {
Some(&first_byte) if first_byte == sigil => Ok(()),
Some(_) => Err(Error::MissingLeadingSigil),
None => Err(Error::Empty),
}
Ok(())
}
/// Checks an identifier that contains a localpart and hostname for validity.