Rerun cargo fmt

This commit is contained in:
Jonas Platte 2020-06-07 17:22:20 +02:00
parent 83de77f002
commit abf8eafe0e
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
9 changed files with 50 additions and 169 deletions

View File

@ -1,8 +1,7 @@
//! Identifiers for device keys for end-to-end encryption. //! Identifiers for device keys for end-to-end encryption.
use crate::{device_id::DeviceId, error::Error, key_algorithms::DeviceKeyAlgorithm}; use crate::{device_id::DeviceId, error::Error, key_algorithms::DeviceKeyAlgorithm};
use std::num::NonZeroU8; use std::{num::NonZeroU8, str::FromStr};
use std::str::FromStr;
/// A key algorithm and a device id, combined with a ':' /// A key algorithm and a device id, combined with a ':'
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -42,17 +41,10 @@ where
DeviceKeyAlgorithm::from_str(&key_str[0..colon_idx.get() as usize]) DeviceKeyAlgorithm::from_str(&key_str[0..colon_idx.get() as usize])
.map_err(|_| Error::UnknownKeyAlgorithm)?; .map_err(|_| Error::UnknownKeyAlgorithm)?;
Ok(DeviceKeyId { Ok(DeviceKeyId { full_id: key_id.into(), colon_idx })
full_id: key_id.into(),
colon_idx,
})
} }
common_impls!( common_impls!(DeviceKeyId, try_from, "Device key ID with algorithm and device ID");
DeviceKeyId,
try_from,
"Device key ID with algorithm and device ID"
);
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View File

@ -66,10 +66,7 @@ impl<T> EventId<T> {
} }
let full_id = format!("${}:{}", generate_localpart(18), server_name).into(); let full_id = format!("${}:{}", generate_localpart(18), server_name).into();
Ok(Self { Ok(Self { full_id, colon_idx: NonZeroU8::new(19) })
full_id,
colon_idx: NonZeroU8::new(19),
})
} }
/// Returns the event's unique ID. For the original event format as used by Matrix room /// Returns the event's unique ID. For the original event format as used by Matrix room
@ -94,8 +91,7 @@ impl<T> EventId<T> {
where where
T: AsRef<str>, T: AsRef<str>,
{ {
self.colon_idx self.colon_idx.map(|idx| &self.full_id.as_ref()[idx.get() as usize + 1..])
.map(|idx| &self.full_id.as_ref()[idx.get() as usize + 1..])
} }
} }
@ -110,17 +106,11 @@ where
if event_id.as_ref().contains(':') { if event_id.as_ref().contains(':') {
let colon_idx = parse_id(event_id.as_ref(), &['$'])?; let colon_idx = parse_id(event_id.as_ref(), &['$'])?;
Ok(EventId { Ok(EventId { full_id: event_id.into(), colon_idx: Some(colon_idx) })
full_id: event_id.into(),
colon_idx: Some(colon_idx),
})
} else { } else {
validate_id(event_id.as_ref(), &['$'])?; validate_id(event_id.as_ref(), &['$'])?;
Ok(EventId { Ok(EventId { full_id: event_id.into(), colon_idx: None })
full_id: event_id.into(),
colon_idx: None,
})
} }
} }
@ -275,10 +265,7 @@ mod tests {
#[test] #[test]
fn missing_original_event_id_sigil() { fn missing_original_event_id_sigil() {
assert_eq!( assert_eq!(EventId::try_from("39hvsi03hlne:example.com").unwrap_err(), Error::MissingSigil);
EventId::try_from("39hvsi03hlne:example.com").unwrap_err(),
Error::MissingSigil
);
} }
#[test] #[test]
@ -299,10 +286,7 @@ mod tests {
#[test] #[test]
fn invalid_event_id_host() { fn invalid_event_id_host() {
assert_eq!( assert_eq!(EventId::try_from("$39hvsi03hlne:/").unwrap_err(), Error::InvalidServerName);
EventId::try_from("$39hvsi03hlne:/").unwrap_err(),
Error::InvalidServerName
);
} }
#[test] #[test]

View File

@ -116,10 +116,7 @@ const MIN_CHARS: usize = 4;
#[cfg(feature = "rand")] #[cfg(feature = "rand")]
fn generate_localpart(length: usize) -> String { fn generate_localpart(length: usize) -> String {
use rand::Rng as _; use rand::Rng as _;
rand::thread_rng() rand::thread_rng().sample_iter(&rand::distributions::Alphanumeric).take(length).collect()
.sample_iter(&rand::distributions::Alphanumeric)
.take(length)
.collect()
} }
/// Checks if an identifier is valid. /// Checks if an identifier is valid.

View File

@ -47,10 +47,7 @@ where
{ {
let colon_idx = parse_id(room_id.as_ref(), &['#'])?; let colon_idx = parse_id(room_id.as_ref(), &['#'])?;
Ok(RoomAliasId { Ok(RoomAliasId { full_id: room_id.into(), colon_idx })
full_id: room_id.into(),
colon_idx,
})
} }
common_impls!(RoomAliasId, try_from, "a Matrix room alias ID"); common_impls!(RoomAliasId, try_from, "a Matrix room alias ID");
@ -138,26 +135,17 @@ mod tests {
#[test] #[test]
fn missing_localpart() { fn missing_localpart() {
assert_eq!( assert_eq!(RoomAliasId::try_from("#:example.com").unwrap_err(), Error::InvalidLocalPart);
RoomAliasId::try_from("#:example.com").unwrap_err(),
Error::InvalidLocalPart
);
} }
#[test] #[test]
fn missing_room_alias_id_delimiter() { fn missing_room_alias_id_delimiter() {
assert_eq!( assert_eq!(RoomAliasId::try_from("#ruma").unwrap_err(), Error::MissingDelimiter);
RoomAliasId::try_from("#ruma").unwrap_err(),
Error::MissingDelimiter
);
} }
#[test] #[test]
fn invalid_room_alias_id_host() { fn invalid_room_alias_id_host() {
assert_eq!( assert_eq!(RoomAliasId::try_from("#ruma:/").unwrap_err(), Error::InvalidServerName);
RoomAliasId::try_from("#ruma:/").unwrap_err(),
Error::InvalidServerName
);
} }
#[test] #[test]

View File

@ -44,10 +44,7 @@ impl<T> RoomId<T> {
} }
let full_id = format!("!{}:{}", generate_localpart(18), server_name).into(); let full_id = format!("!{}:{}", generate_localpart(18), server_name).into();
Ok(Self { Ok(Self { full_id, colon_idx: NonZeroU8::new(19).unwrap() })
full_id,
colon_idx: NonZeroU8::new(19).unwrap(),
})
} }
/// Returns the rooms's unique ID. /// Returns the rooms's unique ID.
@ -76,10 +73,7 @@ where
{ {
let colon_idx = parse_id(room_id.as_ref(), &['!'])?; let colon_idx = parse_id(room_id.as_ref(), &['!'])?;
Ok(RoomId { Ok(RoomId { full_id: room_id.into(), colon_idx })
full_id: room_id.into(),
colon_idx,
})
} }
common_impls!(RoomId, try_from, "a Matrix room ID"); common_impls!(RoomId, try_from, "a Matrix room ID");
@ -165,26 +159,17 @@ mod tests {
#[test] #[test]
fn missing_room_id_sigil() { fn missing_room_id_sigil() {
assert_eq!( assert_eq!(RoomId::try_from("carl:example.com").unwrap_err(), Error::MissingSigil);
RoomId::try_from("carl:example.com").unwrap_err(),
Error::MissingSigil
);
} }
#[test] #[test]
fn missing_room_id_delimiter() { fn missing_room_id_delimiter() {
assert_eq!( assert_eq!(RoomId::try_from("!29fhd83h92h0").unwrap_err(), Error::MissingDelimiter);
RoomId::try_from("!29fhd83h92h0").unwrap_err(),
Error::MissingDelimiter
);
} }
#[test] #[test]
fn invalid_room_id_host() { fn invalid_room_id_host() {
assert_eq!( assert_eq!(RoomId::try_from("!29fhd83h92h0:/").unwrap_err(), Error::InvalidServerName);
RoomId::try_from("!29fhd83h92h0:/").unwrap_err(),
Error::InvalidServerName
);
} }
#[test] #[test]

View File

@ -58,10 +58,9 @@ impl<T: AsRef<str>> RoomIdOrAliasId<T> {
#[cfg_attr(docsrs, doc(cfg(feature = "either")))] #[cfg_attr(docsrs, doc(cfg(feature = "either")))]
pub fn into_either(self) -> either::Either<RoomId<T>, RoomAliasId<T>> { pub fn into_either(self) -> either::Either<RoomId<T>, RoomAliasId<T>> {
match self.variant() { match self.variant() {
Variant::RoomId => either::Either::Left(RoomId { Variant::RoomId => {
full_id: self.full_id, either::Either::Left(RoomId { full_id: self.full_id, colon_idx: self.colon_idx })
colon_idx: self.colon_idx, }
}),
Variant::RoomAliasId => either::Either::Right(RoomAliasId { Variant::RoomAliasId => either::Either::Right(RoomAliasId {
full_id: self.full_id, full_id: self.full_id,
colon_idx: self.colon_idx, colon_idx: self.colon_idx,
@ -94,17 +93,10 @@ where
S: AsRef<str> + Into<T>, S: AsRef<str> + Into<T>,
{ {
let colon_idx = parse_id(room_id_or_alias_id.as_ref(), &['#', '!'])?; let colon_idx = parse_id(room_id_or_alias_id.as_ref(), &['#', '!'])?;
Ok(RoomIdOrAliasId { Ok(RoomIdOrAliasId { full_id: room_id_or_alias_id.into(), colon_idx })
full_id: room_id_or_alias_id.into(),
colon_idx,
})
} }
common_impls!( common_impls!(RoomIdOrAliasId, try_from, "a Matrix room ID or room alias ID");
RoomIdOrAliasId,
try_from,
"a Matrix room ID or room alias ID"
);
impl<T> From<RoomId<T>> for RoomIdOrAliasId<T> { impl<T> From<RoomId<T>> for RoomIdOrAliasId<T> {
fn from(RoomId { full_id, colon_idx }: RoomId<T>) -> Self { fn from(RoomId { full_id, colon_idx }: RoomId<T>) -> Self {
@ -123,14 +115,10 @@ impl<T: AsRef<str>> TryFrom<RoomIdOrAliasId<T>> for RoomId<T> {
fn try_from(id: RoomIdOrAliasId<T>) -> Result<RoomId<T>, RoomAliasId<T>> { fn try_from(id: RoomIdOrAliasId<T>) -> Result<RoomId<T>, RoomAliasId<T>> {
match id.variant() { match id.variant() {
Variant::RoomId => Ok(RoomId { Variant::RoomId => Ok(RoomId { full_id: id.full_id, colon_idx: id.colon_idx }),
full_id: id.full_id, Variant::RoomAliasId => {
colon_idx: id.colon_idx, Err(RoomAliasId { full_id: id.full_id, colon_idx: id.colon_idx })
}), }
Variant::RoomAliasId => Err(RoomAliasId {
full_id: id.full_id,
colon_idx: id.colon_idx,
}),
} }
} }
} }
@ -140,14 +128,10 @@ impl<T: AsRef<str>> TryFrom<RoomIdOrAliasId<T>> for RoomAliasId<T> {
fn try_from(id: RoomIdOrAliasId<T>) -> Result<RoomAliasId<T>, RoomId<T>> { fn try_from(id: RoomIdOrAliasId<T>) -> Result<RoomAliasId<T>, RoomId<T>> {
match id.variant() { match id.variant() {
Variant::RoomAliasId => Ok(RoomAliasId { Variant::RoomAliasId => {
full_id: id.full_id, Ok(RoomAliasId { full_id: id.full_id, colon_idx: id.colon_idx })
colon_idx: id.colon_idx, }
}), Variant::RoomId => Err(RoomId { full_id: id.full_id, colon_idx: id.colon_idx }),
Variant::RoomId => Err(RoomId {
full_id: id.full_id,
colon_idx: id.colon_idx,
}),
} }
} }
} }
@ -185,10 +169,7 @@ mod tests {
#[test] #[test]
fn missing_sigil_for_room_id_or_alias_id() { fn missing_sigil_for_room_id_or_alias_id() {
assert_eq!( assert_eq!(RoomIdOrAliasId::try_from("ruma:example.com").unwrap_err(), Error::MissingSigil);
RoomIdOrAliasId::try_from("ruma:example.com").unwrap_err(),
Error::MissingSigil
);
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]

View File

@ -292,9 +292,7 @@ mod tests {
#[test] #[test]
fn valid_version_1_room_version_id() { fn valid_version_1_room_version_id() {
assert_eq!( assert_eq!(
RoomVersionId::try_from("1") RoomVersionId::try_from("1").expect("Failed to create RoomVersionId.").as_ref(),
.expect("Failed to create RoomVersionId.")
.as_ref(),
"1" "1"
); );
} }
@ -302,9 +300,7 @@ mod tests {
#[test] #[test]
fn valid_version_2_room_version_id() { fn valid_version_2_room_version_id() {
assert_eq!( assert_eq!(
RoomVersionId::try_from("2") RoomVersionId::try_from("2").expect("Failed to create RoomVersionId.").as_ref(),
.expect("Failed to create RoomVersionId.")
.as_ref(),
"2" "2"
); );
} }
@ -312,9 +308,7 @@ mod tests {
#[test] #[test]
fn valid_version_3_room_version_id() { fn valid_version_3_room_version_id() {
assert_eq!( assert_eq!(
RoomVersionId::try_from("3") RoomVersionId::try_from("3").expect("Failed to create RoomVersionId.").as_ref(),
.expect("Failed to create RoomVersionId.")
.as_ref(),
"3" "3"
); );
} }
@ -322,9 +316,7 @@ mod tests {
#[test] #[test]
fn valid_version_4_room_version_id() { fn valid_version_4_room_version_id() {
assert_eq!( assert_eq!(
RoomVersionId::try_from("4") RoomVersionId::try_from("4").expect("Failed to create RoomVersionId.").as_ref(),
.expect("Failed to create RoomVersionId.")
.as_ref(),
"4" "4"
); );
} }
@ -332,9 +324,7 @@ mod tests {
#[test] #[test]
fn valid_version_5_room_version_id() { fn valid_version_5_room_version_id() {
assert_eq!( assert_eq!(
RoomVersionId::try_from("5") RoomVersionId::try_from("5").expect("Failed to create RoomVersionId.").as_ref(),
.expect("Failed to create RoomVersionId.")
.as_ref(),
"5" "5"
); );
} }
@ -342,19 +332,14 @@ mod tests {
#[test] #[test]
fn valid_custom_room_version_id() { fn valid_custom_room_version_id() {
assert_eq!( assert_eq!(
RoomVersionId::try_from("io.ruma.1") RoomVersionId::try_from("io.ruma.1").expect("Failed to create RoomVersionId.").as_ref(),
.expect("Failed to create RoomVersionId.")
.as_ref(),
"io.ruma.1" "io.ruma.1"
); );
} }
#[test] #[test]
fn empty_room_version_id() { fn empty_room_version_id() {
assert_eq!( assert_eq!(RoomVersionId::try_from(""), Err(Error::MinimumLengthNotSatisfied));
RoomVersionId::try_from(""),
Err(Error::MinimumLengthNotSatisfied)
);
} }
#[test] #[test]

View File

@ -43,10 +43,7 @@ where
validate_version(&key_str[colon_idx.get() as usize + 1..])?; validate_version(&key_str[colon_idx.get() as usize + 1..])?;
Ok(ServerKeyId { Ok(ServerKeyId { full_id: key_id.into(), colon_idx })
full_id: key_id.into(),
colon_idx,
})
} }
common_impls!(ServerKeyId, try_from, "Key ID with algorithm and version"); common_impls!(ServerKeyId, try_from, "Key ID with algorithm and version");
@ -93,10 +90,7 @@ mod tests {
#[test] #[test]
fn serialize_id() { fn serialize_id() {
let server_key_id: ServerKeyId<&str> = ServerKeyId::try_from("ed25519:abc123").unwrap(); let server_key_id: ServerKeyId<&str> = ServerKeyId::try_from("ed25519:abc123").unwrap();
assert_eq!( assert_eq!(to_json_value(&server_key_id).unwrap(), json!("ed25519:abc123"));
to_json_value(&server_key_id).unwrap(),
json!("ed25519:abc123")
);
} }
#[test] #[test]

View File

@ -50,11 +50,7 @@ impl<T> UserId<T> {
} }
let full_id = format!("@{}:{}", generate_localpart(12).to_lowercase(), server_name).into(); let full_id = format!("@{}:{}", generate_localpart(12).to_lowercase(), server_name).into();
Ok(Self { Ok(Self { full_id, colon_idx: NonZeroU8::new(13).unwrap(), is_historical: false })
full_id,
colon_idx: NonZeroU8::new(13).unwrap(),
is_historical: false,
})
} }
/// Attempts to complete a user ID, by adding the colon + server name and `@` prefix, if not /// Attempts to complete a user ID, by adding the colon + server name and `@` prefix, if not
@ -125,11 +121,7 @@ where
let is_historical = localpart_is_fully_comforming(localpart)?; let is_historical = localpart_is_fully_comforming(localpart)?;
Ok(UserId { Ok(UserId { full_id: user_id.into(), colon_idx, is_historical: !is_historical })
full_id: user_id.into(),
colon_idx,
is_historical: !is_historical,
})
} }
common_impls!(UserId, try_from, "a Matrix user ID"); common_impls!(UserId, try_from, "a Matrix user ID");
@ -276,9 +268,7 @@ mod tests {
#[test] #[test]
fn valid_user_id_with_explicit_standard_port() { fn valid_user_id_with_explicit_standard_port() {
assert_eq!( assert_eq!(
UserId::try_from("@carl:example.com:443") UserId::try_from("@carl:example.com:443").expect("Failed to create UserId.").as_ref(),
.expect("Failed to create UserId.")
.as_ref(),
"@carl:example.com:443" "@carl:example.com:443"
); );
} }
@ -292,42 +282,27 @@ mod tests {
#[test] #[test]
fn invalid_characters_in_user_id_localpart() { fn invalid_characters_in_user_id_localpart() {
assert_eq!( assert_eq!(UserId::try_from("@te\nst:example.com").unwrap_err(), Error::InvalidCharacters);
UserId::try_from("@te\nst:example.com").unwrap_err(),
Error::InvalidCharacters
);
} }
#[test] #[test]
fn missing_user_id_sigil() { fn missing_user_id_sigil() {
assert_eq!( assert_eq!(UserId::try_from("carl:example.com").unwrap_err(), Error::MissingSigil);
UserId::try_from("carl:example.com").unwrap_err(),
Error::MissingSigil
);
} }
#[test] #[test]
fn missing_localpart() { fn missing_localpart() {
assert_eq!( assert_eq!(UserId::try_from("@:example.com").unwrap_err(), Error::InvalidLocalPart);
UserId::try_from("@:example.com").unwrap_err(),
Error::InvalidLocalPart
);
} }
#[test] #[test]
fn missing_user_id_delimiter() { fn missing_user_id_delimiter() {
assert_eq!( assert_eq!(UserId::try_from("@carl").unwrap_err(), Error::MissingDelimiter);
UserId::try_from("@carl").unwrap_err(),
Error::MissingDelimiter
);
} }
#[test] #[test]
fn invalid_user_id_host() { fn invalid_user_id_host() {
assert_eq!( assert_eq!(UserId::try_from("@carl:/").unwrap_err(), Error::InvalidServerName);
UserId::try_from("@carl:/").unwrap_err(),
Error::InvalidServerName
);
} }
#[test] #[test]