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

View File

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

View File

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

View File

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

View File

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

View File

@ -58,10 +58,9 @@ impl<T: AsRef<str>> RoomIdOrAliasId<T> {
#[cfg_attr(docsrs, doc(cfg(feature = "either")))]
pub fn into_either(self) -> either::Either<RoomId<T>, RoomAliasId<T>> {
match self.variant() {
Variant::RoomId => either::Either::Left(RoomId {
full_id: self.full_id,
colon_idx: self.colon_idx,
}),
Variant::RoomId => {
either::Either::Left(RoomId { full_id: self.full_id, colon_idx: self.colon_idx })
}
Variant::RoomAliasId => either::Either::Right(RoomAliasId {
full_id: self.full_id,
colon_idx: self.colon_idx,
@ -94,17 +93,10 @@ where
S: AsRef<str> + Into<T>,
{
let colon_idx = parse_id(room_id_or_alias_id.as_ref(), &['#', '!'])?;
Ok(RoomIdOrAliasId {
full_id: room_id_or_alias_id.into(),
colon_idx,
})
Ok(RoomIdOrAliasId { full_id: room_id_or_alias_id.into(), colon_idx })
}
common_impls!(
RoomIdOrAliasId,
try_from,
"a Matrix room ID or room alias ID"
);
common_impls!(RoomIdOrAliasId, try_from, "a Matrix room ID or room alias ID");
impl<T> From<RoomId<T>> for RoomIdOrAliasId<T> {
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>> {
match id.variant() {
Variant::RoomId => Ok(RoomId {
full_id: id.full_id,
colon_idx: id.colon_idx,
}),
Variant::RoomAliasId => Err(RoomAliasId {
full_id: id.full_id,
colon_idx: id.colon_idx,
}),
Variant::RoomId => Ok(RoomId { 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>> {
match id.variant() {
Variant::RoomAliasId => Ok(RoomAliasId {
full_id: id.full_id,
colon_idx: id.colon_idx,
}),
Variant::RoomId => Err(RoomId {
full_id: id.full_id,
colon_idx: id.colon_idx,
}),
Variant::RoomAliasId => {
Ok(RoomAliasId { 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]
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::MissingSigil);
}
#[cfg(feature = "serde")]

View File

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

View File

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

View File

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