diff --git a/ruma-identifiers/src/device_key_id.rs b/ruma-identifiers/src/device_key_id.rs index 419c9398..f13891cd 100644 --- a/ruma-identifiers/src/device_key_id.rs +++ b/ruma-identifiers/src/device_key_id.rs @@ -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 { diff --git a/ruma-identifiers/src/event_id.rs b/ruma-identifiers/src/event_id.rs index 77d31857..da8f0013 100644 --- a/ruma-identifiers/src/event_id.rs +++ b/ruma-identifiers/src/event_id.rs @@ -66,10 +66,7 @@ impl EventId { } 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 EventId { where T: AsRef, { - 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] diff --git a/ruma-identifiers/src/lib.rs b/ruma-identifiers/src/lib.rs index 09f378e6..14dfa9ae 100644 --- a/ruma-identifiers/src/lib.rs +++ b/ruma-identifiers/src/lib.rs @@ -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. diff --git a/ruma-identifiers/src/room_alias_id.rs b/ruma-identifiers/src/room_alias_id.rs index b18ccc00..9228f28b 100644 --- a/ruma-identifiers/src/room_alias_id.rs +++ b/ruma-identifiers/src/room_alias_id.rs @@ -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] diff --git a/ruma-identifiers/src/room_id.rs b/ruma-identifiers/src/room_id.rs index 07fdfee6..e7be0bb2 100644 --- a/ruma-identifiers/src/room_id.rs +++ b/ruma-identifiers/src/room_id.rs @@ -44,10 +44,7 @@ impl RoomId { } 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] diff --git a/ruma-identifiers/src/room_id_or_room_alias_id.rs b/ruma-identifiers/src/room_id_or_room_alias_id.rs index 5e1dcf0c..be88f386 100644 --- a/ruma-identifiers/src/room_id_or_room_alias_id.rs +++ b/ruma-identifiers/src/room_id_or_room_alias_id.rs @@ -58,10 +58,9 @@ impl> RoomIdOrAliasId { #[cfg_attr(docsrs, doc(cfg(feature = "either")))] pub fn into_either(self) -> either::Either, RoomAliasId> { 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 + Into, { 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 From> for RoomIdOrAliasId { fn from(RoomId { full_id, colon_idx }: RoomId) -> Self { @@ -123,14 +115,10 @@ impl> TryFrom> for RoomId { fn try_from(id: RoomIdOrAliasId) -> Result, RoomAliasId> { 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> TryFrom> for RoomAliasId { fn try_from(id: RoomIdOrAliasId) -> Result, RoomId> { 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")] diff --git a/ruma-identifiers/src/room_version_id.rs b/ruma-identifiers/src/room_version_id.rs index 742b2cc8..776a5028 100644 --- a/ruma-identifiers/src/room_version_id.rs +++ b/ruma-identifiers/src/room_version_id.rs @@ -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] diff --git a/ruma-identifiers/src/server_key_id.rs b/ruma-identifiers/src/server_key_id.rs index 336a052b..f19d514b 100644 --- a/ruma-identifiers/src/server_key_id.rs +++ b/ruma-identifiers/src/server_key_id.rs @@ -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] diff --git a/ruma-identifiers/src/user_id.rs b/ruma-identifiers/src/user_id.rs index 2e8cb77c..656ca5b2 100644 --- a/ruma-identifiers/src/user_id.rs +++ b/ruma-identifiers/src/user_id.rs @@ -50,11 +50,7 @@ impl UserId { } 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]