identifiers: Refactor Error enum
This commit is contained in:
parent
374603f51c
commit
a5c08c8fde
@ -3,7 +3,7 @@ use std::{num::NonZeroU8, str::FromStr};
|
|||||||
use crate::{crypto_algorithms::DeviceKeyAlgorithm, Error};
|
use crate::{crypto_algorithms::DeviceKeyAlgorithm, Error};
|
||||||
|
|
||||||
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
||||||
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDeviceKeyDelimiter)? as u8)
|
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
|
||||||
.ok_or(Error::UnknownKeyAlgorithm)?;
|
.ok_or(Error::UnknownKeyAlgorithm)?;
|
||||||
|
|
||||||
DeviceKeyAlgorithm::from_str(&s[0..colon_idx.get() as usize])
|
DeviceKeyAlgorithm::from_str(&s[0..colon_idx.get() as usize])
|
||||||
|
@ -22,17 +22,12 @@ pub enum Error {
|
|||||||
/// The ID exceeds 255 bytes (or 32 codepoints for a room version ID).
|
/// The ID exceeds 255 bytes (or 32 codepoints for a room version ID).
|
||||||
MaximumLengthExceeded,
|
MaximumLengthExceeded,
|
||||||
|
|
||||||
/// The ID is missing the colon delimiter between localpart and server name.
|
/// The ID is missing the colon delimiter between localpart and server name, or between key
|
||||||
|
/// algorithm and key name / version.
|
||||||
MissingDelimiter,
|
MissingDelimiter,
|
||||||
|
|
||||||
/// The ID is missing the colon delimiter between key algorithm and device ID.
|
|
||||||
MissingDeviceKeyDelimiter,
|
|
||||||
|
|
||||||
/// The ID is missing the colon delimiter between key algorithm and version.
|
|
||||||
MissingSigningKeyDelimiter,
|
|
||||||
|
|
||||||
/// The ID is missing the correct leading sigil.
|
/// The ID is missing the correct leading sigil.
|
||||||
MissingSigil,
|
MissingLeadingSigil,
|
||||||
|
|
||||||
/// The key algorithm is not recognized.
|
/// The key algorithm is not recognized.
|
||||||
UnknownKeyAlgorithm,
|
UnknownKeyAlgorithm,
|
||||||
@ -46,10 +41,8 @@ impl Display for Error {
|
|||||||
Error::InvalidKeyVersion => "key ID version contains invalid characters",
|
Error::InvalidKeyVersion => "key ID version contains invalid characters",
|
||||||
Error::InvalidServerName => "server name is not a valid IP address or domain name",
|
Error::InvalidServerName => "server name is not a valid IP address or domain name",
|
||||||
Error::MaximumLengthExceeded => "ID exceeds 255 bytes",
|
Error::MaximumLengthExceeded => "ID exceeds 255 bytes",
|
||||||
Error::MissingDelimiter => "colon is required between localpart and server name",
|
Error::MissingDelimiter => "required colon is missing",
|
||||||
Error::MissingDeviceKeyDelimiter => "colon is required between algorithm and device ID",
|
Error::MissingLeadingSigil => "leading sigil is incorrect or missing",
|
||||||
Error::MissingSigningKeyDelimiter => "colon is required between algorithm and version",
|
|
||||||
Error::MissingSigil => "leading sigil is incorrect or missing",
|
|
||||||
Error::UnknownKeyAlgorithm => "unknown key algorithm specified",
|
Error::UnknownKeyAlgorithm => "unknown key algorithm specified",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ pub fn validate(s: &str) -> Result<Option<NonZeroU8>, Error> {
|
|||||||
true => Some(parse_id(s, &['$'])?),
|
true => Some(parse_id(s, &['$'])?),
|
||||||
false => {
|
false => {
|
||||||
if !s.starts_with('$') {
|
if !s.starts_with('$') {
|
||||||
return Err(Error::MissingSigil);
|
return Err(Error::MissingLeadingSigil);
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
@ -24,7 +24,7 @@ fn validate_id(id: &str, valid_sigils: &[char]) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !id.starts_with(valid_sigils) {
|
if !id.starts_with(valid_sigils) {
|
||||||
return Err(Error::MissingSigil);
|
return Err(Error::MissingLeadingSigil);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -3,7 +3,7 @@ use std::{num::NonZeroU8, str::FromStr};
|
|||||||
use crate::{crypto_algorithms::SigningKeyAlgorithm, Error};
|
use crate::{crypto_algorithms::SigningKeyAlgorithm, Error};
|
||||||
|
|
||||||
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
||||||
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingSigningKeyDelimiter)? as u8)
|
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
|
||||||
.ok_or(Error::UnknownKeyAlgorithm)?;
|
.ok_or(Error::UnknownKeyAlgorithm)?;
|
||||||
|
|
||||||
validate_signing_key_algorithm(&s[..colon_idx.get() as usize])?;
|
validate_signing_key_algorithm(&s[..colon_idx.get() as usize])?;
|
||||||
|
@ -7,6 +7,8 @@ Breaking changes:
|
|||||||
* Remove deprecated `is_` methods
|
* Remove deprecated `is_` methods
|
||||||
* Rename `ServerKeyAlgorithm` to `SigningKeyAlgorithm`
|
* Rename `ServerKeyAlgorithm` to `SigningKeyAlgorithm`
|
||||||
* Rename `ServerKeyId` to `ServerSigningKeyId`
|
* Rename `ServerKeyId` to `ServerSigningKeyId`
|
||||||
|
* Rename `Error::MissingSigil` to `Error::MissingLeadingSigil`
|
||||||
|
* Merge all missing delimiter error variants
|
||||||
|
|
||||||
Improvements:
|
Improvements:
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ mod test {
|
|||||||
fn missing_delimiter() {
|
fn missing_delimiter() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
DeviceKeyId::try_from("ed25519|JLAFKJWSCS").unwrap_err(),
|
DeviceKeyId::try_from("ed25519|JLAFKJWSCS").unwrap_err(),
|
||||||
Error::MissingDeviceKeyDelimiter,
|
Error::MissingDelimiter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,14 +243,17 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_original_event_id_sigil() {
|
fn missing_original_event_id_sigil() {
|
||||||
assert_eq!(EventId::try_from("39hvsi03hlne:example.com").unwrap_err(), Error::MissingSigil);
|
assert_eq!(
|
||||||
|
EventId::try_from("39hvsi03hlne:example.com").unwrap_err(),
|
||||||
|
Error::MissingLeadingSigil
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_base64_event_id_sigil() {
|
fn missing_base64_event_id_sigil() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
EventId::try_from("acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap_err(),
|
EventId::try_from("acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap_err(),
|
||||||
Error::MissingSigil
|
Error::MissingLeadingSigil
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,7 +261,7 @@ mod tests {
|
|||||||
fn missing_url_safe_base64_event_id_sigil() {
|
fn missing_url_safe_base64_event_id_sigil() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
EventId::try_from("Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap_err(),
|
EventId::try_from("Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap_err(),
|
||||||
Error::MissingSigil
|
Error::MissingLeadingSigil
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ mod tests {
|
|||||||
fn missing_room_alias_id_sigil() {
|
fn missing_room_alias_id_sigil() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
RoomAliasId::try_from("39hvsi03hlne:example.com").unwrap_err(),
|
RoomAliasId::try_from("39hvsi03hlne:example.com").unwrap_err(),
|
||||||
Error::MissingSigil
|
Error::MissingLeadingSigil
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +145,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn invalid_leading_sigil() {
|
fn invalid_leading_sigil() {
|
||||||
assert_eq!(RoomAliasId::try_from("!room_id:foo.bar").unwrap_err(), Error::MissingSigil);
|
assert_eq!(
|
||||||
|
RoomAliasId::try_from("!room_id:foo.bar").unwrap_err(),
|
||||||
|
Error::MissingLeadingSigil
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -150,7 +150,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_room_id_sigil() {
|
fn missing_room_id_sigil() {
|
||||||
assert_eq!(RoomId::try_from("carl:example.com").unwrap_err(), Error::MissingSigil);
|
assert_eq!(RoomId::try_from("carl:example.com").unwrap_err(), Error::MissingLeadingSigil);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -166,7 +166,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_sigil_for_room_id_or_alias_id() {
|
fn missing_sigil_for_room_id_or_alias_id() {
|
||||||
assert_eq!(RoomIdOrAliasId::try_from("ruma:example.com").unwrap_err(), Error::MissingSigil);
|
assert_eq!(
|
||||||
|
RoomIdOrAliasId::try_from("ruma:example.com").unwrap_err(),
|
||||||
|
Error::MissingLeadingSigil
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
|
@ -97,7 +97,7 @@ mod tests {
|
|||||||
fn missing_delimiter() {
|
fn missing_delimiter() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ServerSigningKeyId::try_from("ed25519|Abc_1").unwrap_err(),
|
ServerSigningKeyId::try_from("ed25519|Abc_1").unwrap_err(),
|
||||||
Error::MissingSigningKeyDelimiter,
|
Error::MissingDelimiter,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -239,7 +239,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_user_id_sigil() {
|
fn missing_user_id_sigil() {
|
||||||
assert_eq!(UserId::try_from("carl:example.com").unwrap_err(), Error::MissingSigil);
|
assert_eq!(UserId::try_from("carl:example.com").unwrap_err(), Error::MissingLeadingSigil);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user