diff --git a/ruma-api/tests/conversions.rs b/ruma-api/tests/conversions.rs index c8c598b0..01bd1201 100644 --- a/ruma-api/tests/conversions.rs +++ b/ruma-api/tests/conversions.rs @@ -38,13 +38,15 @@ ruma_api! { fn request_serde() -> Result<(), Box> { use std::convert::TryFrom; + use ruma_identifiers::user_id; + let req = Request { hello: "hi".to_owned(), world: "test".to_owned(), q1: "query_param_special_chars %/&@!".to_owned(), q2: 55, bar: "barVal".to_owned(), - baz: UserId::try_from("@bazme:ruma.io")?, + baz: user_id!("@bazme:ruma.io"), }; let http_req = req.clone().try_into_http_request("https://homeserver.tld", None)?; diff --git a/ruma-client-api/src/r0/membership/invite_user.rs b/ruma-client-api/src/r0/membership/invite_user.rs index ab22e7cc..2380c459 100644 --- a/ruma-client-api/src/r0/membership/invite_user.rs +++ b/ruma-client-api/src/r0/membership/invite_user.rs @@ -53,9 +53,7 @@ pub enum InvitationRecipient { #[cfg(test)] mod tests { - use std::convert::TryFrom; - - use ruma_identifiers::UserId; + use ruma_identifiers::user_id; use serde_json::{from_value as from_json_value, json}; use super::InvitationRecipient; @@ -65,7 +63,7 @@ mod tests { let incoming = from_json_value::(json!({ "user_id": "@carl:example.org" })) .unwrap(); - let user_id = UserId::try_from("@carl:example.org").unwrap(); + let user_id = user_id!("@carl:example.org"); let recipient = InvitationRecipient::UserId { user_id }; assert_eq!(incoming, recipient); } diff --git a/ruma-client-api/src/r0/message/get_message_events.rs b/ruma-client-api/src/r0/message/get_message_events.rs index bcae9b72..239e0302 100644 --- a/ruma-client-api/src/r0/message/get_message_events.rs +++ b/ruma-client-api/src/r0/message/get_message_events.rs @@ -117,17 +117,15 @@ pub enum Direction { mod tests { use super::{Direction, Request}; - use std::convert::TryFrom; - use js_int::uint; use ruma_api::Endpoint; - use ruma_identifiers::RoomId; + use ruma_identifiers::room_id; use crate::r0::filter::{LazyLoadOptions, RoomEventFilter}; #[test] fn test_serialize_some_room_event_filter() { - let room_id = RoomId::try_from("!roomid:example.org").unwrap(); + let room_id = room_id!("!roomid:example.org"); let filter = RoomEventFilter { lazy_load_options: LazyLoadOptions::Enabled { include_redundant_members: true }, rooms: Some(vec![room_id.clone()]), @@ -154,7 +152,7 @@ mod tests { #[test] fn test_serialize_none_room_event_filter() { - let room_id = RoomId::try_from("!roomid:example.org").unwrap(); + let room_id = room_id!("!roomid:example.org"); let req = Request { room_id, from: "token".into(), @@ -171,7 +169,7 @@ mod tests { #[test] fn test_serialize_default_room_event_filter() { - let room_id = RoomId::try_from("!roomid:example.org").unwrap(); + let room_id = room_id!("!roomid:example.org"); let req = Request { room_id, from: "token".into(), diff --git a/ruma-events/src/ignored_user_list.rs b/ruma-events/src/ignored_user_list.rs index 1437b433..243b6eb2 100644 --- a/ruma-events/src/ignored_user_list.rs +++ b/ruma-events/src/ignored_user_list.rs @@ -20,11 +20,9 @@ pub struct IgnoredUserListEventContent { #[cfg(test)] mod tests { - use std::convert::TryFrom; - use matches::assert_matches; use ruma_common::Raw; - use ruma_identifiers::UserId; + use ruma_identifiers::user_id; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::IgnoredUserListEventContent; @@ -34,7 +32,7 @@ mod tests { fn serialization() { let ignored_user_list_event = BasicEvent { content: IgnoredUserListEventContent { - ignored_users: vec![UserId::try_from("@carl:example.com").unwrap()], + ignored_users: vec![user_id!("@carl:example.com")], }, }; @@ -68,7 +66,7 @@ mod tests { .unwrap(), BasicEvent { content: AnyBasicEventContent::IgnoredUserList(IgnoredUserListEventContent { ignored_users, }), - } if ignored_users == vec![UserId::try_from("@carl:example.com").unwrap()] + } if ignored_users == vec![user_id!("@carl:example.com")] ); } } diff --git a/ruma-events/src/presence.rs b/ruma-events/src/presence.rs index 9b71ca1b..0f89792e 100644 --- a/ruma-events/src/presence.rs +++ b/ruma-events/src/presence.rs @@ -51,12 +51,10 @@ pub struct PresenceEventContent { #[cfg(test)] mod tests { - use std::convert::TryFrom; - use js_int::uint; use matches::assert_matches; use ruma_common::presence::PresenceState; - use ruma_identifiers::UserId; + use ruma_identifiers::user_id; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{PresenceEvent, PresenceEventContent}; @@ -72,7 +70,7 @@ mod tests { presence: PresenceState::Online, status_msg: Some("Making cupcakes".into()), }, - sender: UserId::try_from("@example:localhost").unwrap(), + sender: user_id!("@example:localhost"), }; let json = json!({ diff --git a/ruma-events/src/room/canonical_alias.rs b/ruma-events/src/room/canonical_alias.rs index 1e9f514f..0d0e1648 100644 --- a/ruma-events/src/room/canonical_alias.rs +++ b/ruma-events/src/room/canonical_alias.rs @@ -39,13 +39,10 @@ impl CanonicalAliasEventContent { #[cfg(test)] mod tests { - use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, - }; + use std::time::{Duration, UNIX_EPOCH}; use ruma_common::Raw; - use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; + use ruma_identifiers::{event_id, room_alias_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::CanonicalAliasEventContent; @@ -55,14 +52,14 @@ mod tests { fn serialization_with_optional_fields_as_none() { let canonical_alias_event = StateEvent { content: CanonicalAliasEventContent { - alias: Some(RoomAliasId::try_from("#somewhere:localhost").unwrap()), + alias: Some(room_alias_id!("#somewhere:localhost")), alt_aliases: Vec::new(), }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: None, - room_id: RoomId::try_from("!dummy:example.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!dummy:example.com"), + sender: user_id!("@carl:example.com"), state_key: "".into(), unsigned: Unsigned::default(), }; @@ -156,7 +153,7 @@ mod tests { #[test] fn nonempty_field_as_some() { - let alias = Some(RoomAliasId::try_from("#somewhere:localhost").unwrap()); + let alias = Some(room_alias_id!("#somewhere:localhost")); let json_data = json!({ "content": { "alias": "#somewhere:localhost" diff --git a/ruma-events/src/room/create.rs b/ruma-events/src/room/create.rs index 2cab3846..958b25e8 100644 --- a/ruma-events/src/room/create.rs +++ b/ruma-events/src/room/create.rs @@ -68,11 +68,9 @@ fn default_room_version_id() -> RoomVersionId { #[cfg(test)] mod tests { - use std::convert::TryFrom; - use matches::assert_matches; use ruma_common::Raw; - use ruma_identifiers::{RoomVersionId, UserId}; + use ruma_identifiers::{user_id, RoomVersionId}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::CreateEventContent; @@ -80,7 +78,7 @@ mod tests { #[test] fn serialization() { let content = CreateEventContent { - creator: UserId::try_from("@carl:example.com").unwrap(), + creator: user_id!("@carl:example.com"), federate: false, room_version: RoomVersionId::Version4, predecessor: None, diff --git a/ruma-events/src/room/message.rs b/ruma-events/src/room/message.rs index f5a6f992..0cb6364b 100644 --- a/ruma-events/src/room/message.rs +++ b/ruma-events/src/room/message.rs @@ -406,14 +406,11 @@ impl TextMessageEventContent { #[cfg(test)] mod tests { - use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, - }; + use std::time::{Duration, UNIX_EPOCH}; use matches::assert_matches; use ruma_common::Raw; - use ruma_identifiers::{EventId, RoomId, UserId}; + use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{AudioMessageEventContent, FormattedBody, MessageEventContent, MessageFormat}; @@ -431,10 +428,10 @@ mod tests { url: Some("http://example.com/audio.mp3".into()), file: None, }), - event_id: EventId::try_from("$143273582443PhrSn:example.org").unwrap(), + event_id: event_id!("$143273582443PhrSn:example.org"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(10_000), - room_id: RoomId::try_from("!testroomid:example.org").unwrap(), - sender: UserId::try_from("@user:example.org").unwrap(), + room_id: room_id!("!testroomid:example.org"), + sender: user_id!("@user:example.org"), unsigned: Unsigned::default(), }; @@ -518,7 +515,7 @@ mod tests { formatted: None, relates_to: Some(RelatesTo { in_reply_to: Some(InReplyTo { - event_id: EventId::try_from("$15827405538098VGFWH:example.com").unwrap(), + event_id: event_id!("$15827405538098VGFWH:example.com"), }), }), }); diff --git a/ruma-events/src/room/name.rs b/ruma-events/src/room/name.rs index aea90ff7..9ea9a81c 100644 --- a/ruma-events/src/room/name.rs +++ b/ruma-events/src/room/name.rs @@ -57,7 +57,6 @@ where #[cfg(test)] mod tests { use std::{ - convert::TryFrom, iter::FromIterator, time::{Duration, UNIX_EPOCH}, }; @@ -65,7 +64,7 @@ mod tests { use js_int::Int; use matches::assert_matches; use ruma_common::Raw; - use ruma_identifiers::{EventId, RoomId, UserId}; + use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use crate::{StateEvent, Unsigned}; @@ -76,11 +75,11 @@ mod tests { fn serialization_with_optional_fields_as_none() { let name_event = StateEvent { content: NameEventContent { name: Some("The room name".into()) }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: None, - room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!n8f893n9:example.com"), + sender: user_id!("@carl:example.com"), state_key: "".into(), unsigned: Unsigned::default(), }; @@ -105,11 +104,11 @@ mod tests { fn serialization_with_all_fields() { let name_event = StateEvent { content: NameEventContent { name: Some("The room name".into()) }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: Some(NameEventContent { name: Some("The old name".into()) }), - room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!n8f893n9:example.com"), + sender: user_id!("@carl:example.com"), state_key: "".into(), unsigned: Unsigned { age: Some(Int::from(100)), ..Unsigned::default() }, }; diff --git a/ruma-events/src/room/power_levels.rs b/ruma-events/src/room/power_levels.rs index 3446afcf..a98bd0f9 100644 --- a/ruma-events/src/room/power_levels.rs +++ b/ruma-events/src/room/power_levels.rs @@ -119,13 +119,12 @@ fn is_default_power_level(l: &Int) -> bool { mod tests { use std::{ collections::BTreeMap, - convert::TryFrom, time::{Duration, UNIX_EPOCH}, }; use js_int::Int; use maplit::btreemap; - use ruma_identifiers::{EventId, RoomId, UserId}; + use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{json, to_value as to_json_value}; use super::{default_power_level, NotificationPowerLevels, PowerLevelsEventContent}; @@ -148,12 +147,12 @@ mod tests { users_default: Int::from(0), notifications: NotificationPowerLevels::default(), }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: None, - room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), + room_id: room_id!("!n8f893n9:example.com"), unsigned: Unsigned::default(), - sender: UserId::try_from("@carl:example.com").unwrap(), + sender: user_id!("@carl:example.com"), state_key: "".into(), }; @@ -173,7 +172,7 @@ mod tests { #[test] fn serialization_with_all_fields() { - let user = UserId::try_from("@carl:example.com").unwrap(); + let user = user_id!("@carl:example.com"); let power_levels_event = StateEvent { content: PowerLevelsEventContent { ban: Int::from(23), @@ -191,7 +190,7 @@ mod tests { users_default: Int::from(23), notifications: NotificationPowerLevels { room: Int::from(23) }, }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: Some(PowerLevelsEventContent { // Make just one field different so we at least know they're two different objects. @@ -210,7 +209,7 @@ mod tests { users_default: Int::from(42), notifications: NotificationPowerLevels { room: Int::from(42) }, }), - room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), + room_id: room_id!("!n8f893n9:example.com"), unsigned: Unsigned { age: Some(Int::from(100)), ..Unsigned::default() }, sender: user, state_key: "".into(), diff --git a/ruma-events/src/room_key.rs b/ruma-events/src/room_key.rs index dc7f7c77..786959ec 100644 --- a/ruma-events/src/room_key.rs +++ b/ruma-events/src/room_key.rs @@ -33,9 +33,7 @@ pub struct RoomKeyEventContent { #[cfg(test)] mod tests { - use std::convert::TryFrom; - - use ruma_identifiers::RoomId; + use ruma_identifiers::room_id; use serde_json::{json, to_value as to_json_value}; use super::RoomKeyEventContent; @@ -46,7 +44,7 @@ mod tests { let ev = BasicEvent { content: RoomKeyEventContent { algorithm: Algorithm::MegolmV1AesSha2, - room_id: RoomId::try_from("!testroomid:example.org").unwrap(), + room_id: room_id!("!testroomid:example.org"), session_id: "SessId".into(), session_key: "SessKey".into(), }, diff --git a/ruma-events/tests/custom.rs b/ruma-events/tests/custom.rs index b3ee154c..4a5db18b 100644 --- a/ruma-events/tests/custom.rs +++ b/ruma-events/tests/custom.rs @@ -1,7 +1,4 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use matches::assert_matches; use ruma_common::Raw; @@ -10,7 +7,7 @@ use ruma_events::{ AnySyncMessageEvent, AnySyncRoomEvent, MessageEvent, StateEvent, SyncMessageEvent, SyncStateEvent, Unsigned, }; -use ruma_identifiers::{EventId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{ from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, }; @@ -54,10 +51,10 @@ fn serialize_custom_message_event() { }), event_type: "m.room.message".into(), }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(10), - room_id: RoomId::try_from("!room:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!room:room.com"), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), }); @@ -94,11 +91,11 @@ fn serialize_custom_state_event() { }), event_type: "m.made.up".into(), }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(10), prev_content: None, - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), state_key: "".into(), unsigned: Unsigned::default(), }); @@ -148,10 +145,10 @@ fn deserialize_custom_state_event() { state_key, unsigned, }) if json == expected_content && event_type == "m.reaction" - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) - && sender == UserId::try_from("@carl:example.com").unwrap() - && room_id == RoomId::try_from("!room:room.com").unwrap() + && sender == user_id!("@carl:example.com") + && room_id == room_id!("!room:room.com") && state_key == "" && !unsigned.is_empty() ); @@ -183,9 +180,9 @@ fn deserialize_custom_state_sync_event() { state_key, unsigned, } if json == expected_content && event_type == "m.reaction" - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) - && sender == UserId::try_from("@carl:example.com").unwrap() + && sender == user_id!("@carl:example.com") && state_key == "" && !unsigned.is_empty() ); @@ -231,9 +228,9 @@ fn deserialize_custom_message_sync_event() { sender, unsigned, })) if json == expected_content && event_type == "m.reaction" - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) - && sender == UserId::try_from("@carl:example.com").unwrap() + && sender == user_id!("@carl:example.com") && !unsigned.is_empty() ); } diff --git a/ruma-events/tests/enums.rs b/ruma-events/tests/enums.rs index 401780d8..0e9492bb 100644 --- a/ruma-events/tests/enums.rs +++ b/ruma-events/tests/enums.rs @@ -1,7 +1,5 @@ -use std::convert::TryFrom; - use matches::assert_matches; -use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_alias_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, Value as JsonValue}; use ruma_events::{ @@ -166,7 +164,7 @@ fn aliases_event_sync_deserialization() { .. }) )) - if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ] + if aliases == vec![ room_alias_id!("#somewhere:localhost") ] ); } @@ -205,7 +203,7 @@ fn alias_room_event_deserialization() { .. }) )) - if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ] + if aliases == vec![ room_alias_id!("#somewhere:localhost") ] ); } @@ -244,7 +242,7 @@ fn alias_event_deserialization() { .. }) )) - if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ] + if aliases == vec![ room_alias_id!("#somewhere:localhost") ] ); } @@ -256,15 +254,15 @@ fn alias_event_field_access() { from_json_value::(json_data.clone()), Ok(AnyEvent::State(state_event)) if state_event.state_key() == "" - && state_event.room_id() == &RoomId::try_from("!room:room.com").unwrap() - && state_event.event_id() == &EventId::try_from("$152037280074GZeOm:localhost").unwrap() - && state_event.sender() == &UserId::try_from("@example:localhost").unwrap() + && state_event.room_id() == &room_id!("!room:room.com") + && state_event.event_id() == &event_id!("$152037280074GZeOm:localhost") + && state_event.sender() == &user_id!("@example:localhost") ); let deser = from_json_value::(json_data).unwrap(); if let AnyStateEventContent::RoomAliases(AliasesEventContent { aliases, .. }) = deser.content() { - assert_eq!(aliases, vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()]) + assert_eq!(aliases, vec![room_alias_id!("#somewhere:localhost")]) } else { panic!("the `Any*Event` enum's accessor methods may have been altered") } diff --git a/ruma-events/tests/ephemeral_event.rs b/ruma-events/tests/ephemeral_event.rs index 9ecbc53b..a7ba6e5f 100644 --- a/ruma-events/tests/ephemeral_event.rs +++ b/ruma-events/tests/ephemeral_event.rs @@ -1,12 +1,9 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use maplit::btreemap; use matches::assert_matches; use ruma_common::Raw; -use ruma_identifiers::{EventId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use ruma_events::{ @@ -19,9 +16,9 @@ use ruma_events::{ fn ephemeral_serialize_typing() { let aliases_event = EphemeralRoomEvent { content: AnyEphemeralRoomEventContent::Typing(TypingEventContent { - user_ids: vec![UserId::try_from("@carl:example.com").unwrap()], + user_ids: vec![user_id!("@carl:example.com")], }), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), + room_id: room_id!("!roomid:room.com"), }; let actual = to_json_value(&aliases_event).unwrap(); @@ -56,15 +53,15 @@ fn deserialize_ephemeral_typing() { user_ids, }), room_id, - } if user_ids[0] == UserId::try_from("@carl:example.com").unwrap() - && room_id == RoomId::try_from("!roomid:room.com").unwrap() + } if user_ids[0] == user_id!("@carl:example.com") + && room_id == room_id!("!roomid:room.com") ); } #[test] fn ephemeral_serialize_receipt() { - let event_id = EventId::try_from("$h29iv0s8:example.com").unwrap(); - let user_id = UserId::try_from("@carl:example.com").unwrap(); + let event_id = event_id!("$h29iv0s8:example.com"); + let user_id = user_id!("@carl:example.com"); let aliases_event = EphemeralRoomEvent { content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(btreemap! { @@ -74,7 +71,7 @@ fn ephemeral_serialize_receipt() { }), }, })), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), + room_id: room_id!("!roomid:room.com"), }; let actual = to_json_value(&aliases_event).unwrap(); @@ -95,8 +92,8 @@ fn ephemeral_serialize_receipt() { #[test] fn deserialize_ephemeral_receipt() { - let event_id = EventId::try_from("$h29iv0s8:example.com").unwrap(); - let user_id = UserId::try_from("@carl:example.com").unwrap(); + let event_id = event_id!("$h29iv0s8:example.com"); + let user_id = user_id!("@carl:example.com"); let json_data = json!({ "content": { @@ -119,7 +116,7 @@ fn deserialize_ephemeral_receipt() { content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(receipts)), room_id, } if !receipts.is_empty() && receipts.contains_key(&event_id) - && room_id == RoomId::try_from("!roomid:room.com").unwrap() + && room_id == room_id!("!roomid:room.com") && receipts .get(&event_id) .map(|r| r.read.as_ref().unwrap().get(&user_id).unwrap().clone()) diff --git a/ruma-events/tests/event_enums.rs b/ruma-events/tests/event_enums.rs index a88e62f0..af04781b 100644 --- a/ruma-events/tests/event_enums.rs +++ b/ruma-events/tests/event_enums.rs @@ -1,11 +1,8 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use js_int::UInt; use matches::assert_matches; -use ruma_identifiers::{EventId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use ruma_events::{ @@ -59,10 +56,10 @@ fn deserialize_message_event() { sender, unsigned, }) if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap() - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@carl:example.com").unwrap() + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@carl:example.com") && unsigned.is_empty() ); } @@ -88,10 +85,10 @@ fn serialize_message_event() { }, url: "http://www.matrix.org".into(), }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), }); diff --git a/ruma-events/tests/message_event.rs b/ruma-events/tests/message_event.rs index c1113514..7b31cbb5 100644 --- a/ruma-events/tests/message_event.rs +++ b/ruma-events/tests/message_event.rs @@ -1,7 +1,4 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use js_int::{uint, UInt}; use matches::assert_matches; @@ -12,7 +9,7 @@ use ruma_events::{ sticker::StickerEventContent, AnyMessageEventContent, AnySyncMessageEvent, MessageEvent, RawExt, Unsigned, }; -use ruma_identifiers::{EventId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; #[test] @@ -36,10 +33,10 @@ fn message_serialize_sticker() { }, url: "http://www.matrix.org".into(), }), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), }; @@ -137,10 +134,10 @@ fn deserialize_message_call_answer() { sender, unsigned, } if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap() - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@carl:example.com").unwrap() + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@carl:example.com") && unsigned.is_empty() ); } @@ -196,11 +193,11 @@ fn deserialize_message_sticker() { room_id, sender, unsigned - } if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + } if event_id == event_id!("$h29iv0s8:example.com") && body == "Hello" && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@carl:example.com").unwrap() + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@carl:example.com") && height == UInt::new(423) && width == UInt::new(1011) && mimetype == "image/png" @@ -225,7 +222,7 @@ fn deserialize_message_sticker() { #[test] fn deserialize_message_then_convert_to_full() { - let rid = RoomId::try_from("!roomid:room.com").unwrap(); + let rid = room_id!("!roomid:room.com"); let json_data = json!({ "content": { "answer": { diff --git a/ruma-events/tests/pdu.rs b/ruma-events/tests/pdu.rs index cf08f353..d4e7f25c 100644 --- a/ruma-events/tests/pdu.rs +++ b/ruma-events/tests/pdu.rs @@ -1,6 +1,6 @@ use std::{ collections::BTreeMap, - convert::{TryFrom, TryInto}, + convert::TryInto, time::{Duration, SystemTime}, }; @@ -9,7 +9,7 @@ use ruma_events::{ pdu::{EventHash, Pdu, PduStub, RoomV1Pdu, RoomV1PduStub, RoomV3Pdu, RoomV3PduStub}, EventType, }; -use ruma_identifiers::{EventId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; #[test] @@ -24,22 +24,22 @@ fn serialize_stub_as_v1() { unsigned.insert("somekey".into(), json!({"a": 456})); let v1_stub = RoomV1PduStub { - sender: UserId::try_from("@sender:example.com").unwrap(), + sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), prev_events: vec![( - EventId::try_from("$previousevent:matrix.org").unwrap(), + event_id!("$previousevent:matrix.org"), EventHash { sha256: "123567".into() }, )], depth: 2_u32.into(), auth_events: vec![( - EventId::try_from("$someauthevent:matrix.org").unwrap(), + event_id!("$someauthevent:matrix.org"), EventHash { sha256: "21389CFEDABC".into() }, )], - redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), + redacts: Some(event_id!("$9654:matrix.org")), unsigned, hashes: EventHash { sha256: "1233543bABACDEF".into() }, signatures, @@ -85,16 +85,16 @@ fn serialize_stub_as_v3() { unsigned.insert("somekey".into(), json!({"a": 456})); let v3_stub = RoomV3PduStub { - sender: UserId::try_from("@sender:example.com").unwrap(), + sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), - prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()], + prev_events: vec![event_id!("$previousevent:matrix.org")], depth: 2_u32.into(), - auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()], - redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), + auth_events: vec![event_id!("$someauthevent:matrix.org")], + redacts: Some(event_id!("$9654:matrix.org")), unsigned, hashes: EventHash { sha256: "1233543bABACDEF".into() }, signatures, @@ -171,10 +171,7 @@ fn deserialize_stub_as_v1() { match parsed { PduStub::RoomV1PduStub(v1_stub) => { - assert_eq!( - v1_stub.auth_events.first().unwrap().0, - EventId::try_from("$abc123:matrix.org").unwrap() - ); + assert_eq!(v1_stub.auth_events.first().unwrap().0, event_id!("$abc123:matrix.org")); assert_eq!( v1_stub.auth_events.first().unwrap().1.sha256, "Base64EncodedSha256HashesShouldBe43BytesLong" @@ -222,10 +219,7 @@ fn deserialize_stub_as_v3() { match parsed { PduStub::RoomV1PduStub(_) => panic!("Matched V1 stub"), PduStub::RoomV3PduStub(v3_stub) => { - assert_eq!( - v3_stub.auth_events.first().unwrap(), - &EventId::try_from("$abc123:matrix.org").unwrap() - ); + assert_eq!(v3_stub.auth_events.first().unwrap(), &event_id!("$abc123:matrix.org")); } } } @@ -242,24 +236,24 @@ fn serialize_pdu_as_v1() { unsigned.insert("somekey".into(), json!({"a": 456})); let v1_pdu = RoomV1Pdu { - room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), - event_id: EventId::try_from("$somejoinevent:matrix.org").unwrap(), - sender: UserId::try_from("@sender:example.com").unwrap(), + room_id: room_id!("!n8f893n9:example.com"), + event_id: event_id!("$somejoinevent:matrix.org"), + sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), prev_events: vec![( - EventId::try_from("$previousevent:matrix.org").unwrap(), + event_id!("$previousevent:matrix.org"), EventHash { sha256: "123567".into() }, )], depth: 2_u32.into(), auth_events: vec![( - EventId::try_from("$someauthevent:matrix.org").unwrap(), + event_id!("$someauthevent:matrix.org"), EventHash { sha256: "21389CFEDABC".into() }, )], - redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), + redacts: Some(event_id!("$9654:matrix.org")), unsigned, hashes: EventHash { sha256: "1233543bABACDEF".into() }, signatures, @@ -307,17 +301,17 @@ fn serialize_pdu_as_v3() { unsigned.insert("somekey".into(), json!({"a": 456})); let v3_pdu = RoomV3Pdu { - room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(), - sender: UserId::try_from("@sender:example.com").unwrap(), + room_id: room_id!("!n8f893n9:example.com"), + sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), - prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()], + prev_events: vec![event_id!("$previousevent:matrix.org")], depth: 2_u32.into(), - auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()], - redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), + auth_events: vec![event_id!("$someauthevent:matrix.org")], + redacts: Some(event_id!("$9654:matrix.org")), unsigned, hashes: EventHash { sha256: "1233543bABACDEF".into() }, signatures, @@ -397,10 +391,7 @@ fn test_deserialize_pdu_as_v1() { match parsed { Pdu::RoomV1Pdu(v1_pdu) => { - assert_eq!( - v1_pdu.auth_events.first().unwrap().0, - EventId::try_from("$abc123:matrix.org").unwrap() - ); + assert_eq!(v1_pdu.auth_events.first().unwrap().0, event_id!("$abc123:matrix.org")); assert_eq!( v1_pdu.auth_events.first().unwrap().1.sha256, "Base64EncodedSha256HashesShouldBe43BytesLong" @@ -449,10 +440,7 @@ fn deserialize_pdu_as_v3() { match parsed { Pdu::RoomV1Pdu(_) => panic!("Matched V1 PDU"), Pdu::RoomV3Pdu(v3_pdu) => { - assert_eq!( - v3_pdu.auth_events.first().unwrap(), - &EventId::try_from("$abc123:matrix.org").unwrap() - ); + assert_eq!(v3_pdu.auth_events.first().unwrap(), &event_id!("$abc123:matrix.org")); } } } @@ -469,22 +457,22 @@ fn convert_v1_stub_to_pdu() { unsigned.insert("somekey".into(), json!({"a": 456})); let v1_stub = RoomV1PduStub { - sender: UserId::try_from("@sender:example.com").unwrap(), + sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), prev_events: vec![( - EventId::try_from("$previousevent:matrix.org").unwrap(), + event_id!("$previousevent:matrix.org"), EventHash { sha256: "123567".into() }, )], depth: 2_u32.into(), auth_events: vec![( - EventId::try_from("$someauthevent:matrix.org").unwrap(), + event_id!("$someauthevent:matrix.org"), EventHash { sha256: "21389CFEDABC".into() }, )], - redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), + redacts: Some(event_id!("$9654:matrix.org")), unsigned: (&unsigned).clone(), hashes: EventHash { sha256: "1233543bABACDEF".into() }, signatures: (&signatures).clone(), @@ -492,8 +480,8 @@ fn convert_v1_stub_to_pdu() { assert_matches!( v1_stub.into_v1_pdu( - RoomId::try_from("!n8f893n9:example.com").unwrap(), - EventId::try_from("$somejoinevent:matrix.org").unwrap() + room_id!("!n8f893n9:example.com"), + event_id!("$somejoinevent:matrix.org") ), RoomV1Pdu { room_id, @@ -511,20 +499,20 @@ fn convert_v1_stub_to_pdu() { unsigned, hashes: EventHash { sha256 }, signatures, - } if room_id == RoomId::try_from("!n8f893n9:example.com").unwrap() - && event_id == EventId::try_from("$somejoinevent:matrix.org").unwrap() - && sender == UserId::try_from("@sender:example.com").unwrap() + } if room_id == room_id!("!n8f893n9:example.com") + && event_id == event_id!("$somejoinevent:matrix.org") + && sender == user_id!("@sender:example.com") && origin == "matrix.org" && origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658) && kind == EventType::RoomPowerLevels && content == json!({"testing": 123}) && state_key == Some("state".into()) - && prev_events[0].0 == EventId::try_from("$previousevent:matrix.org").unwrap() + && prev_events[0].0 == event_id!("$previousevent:matrix.org") && prev_events[0].1.sha256 == "123567" && depth == 2_u32.into() - && auth_events.first().unwrap().0 == EventId::try_from("$someauthevent:matrix.org").unwrap() + && auth_events.first().unwrap().0 == event_id!("$someauthevent:matrix.org") && auth_events.first().unwrap().1.sha256 == "21389CFEDABC" - && redacts == Some(EventId::try_from("$9654:matrix.org").unwrap()) + && redacts == Some(event_id!("$9654:matrix.org")) && unsigned == (&unsigned).clone() && sha256 == "1233543bABACDEF" && signatures == (&signatures).clone() @@ -544,23 +532,23 @@ fn convert_v3_stub_to_pdu() { unsigned.insert("somekey".into(), json!({"a": 456})); let v3_stub = RoomV3PduStub { - sender: UserId::try_from("@sender:example.com").unwrap(), + sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), - prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()], + prev_events: vec![event_id!("$previousevent:matrix.org")], depth: 2_u32.into(), - auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()], - redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()), + auth_events: vec![event_id!("$someauthevent:matrix.org")], + redacts: Some(event_id!("$9654:matrix.org")), unsigned: (&unsigned).clone(), hashes: EventHash { sha256: "1233543bABACDEF".into() }, signatures: (&signatures).clone(), }; assert_matches!( - v3_stub.into_v3_pdu(RoomId::try_from("!n8f893n9:example.com").unwrap()), + v3_stub.into_v3_pdu(room_id!("!n8f893n9:example.com")), RoomV3Pdu { room_id, sender, @@ -576,17 +564,17 @@ fn convert_v3_stub_to_pdu() { unsigned, hashes: EventHash { sha256 }, signatures, - } if room_id == RoomId::try_from("!n8f893n9:example.com").unwrap() - && sender == UserId::try_from("@sender:example.com").unwrap() + } if room_id == room_id!("!n8f893n9:example.com") + && sender == user_id!("@sender:example.com") && origin == "matrix.org" && origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658) && kind == EventType::RoomPowerLevels && content == json!({"testing": 123}) && state_key == Some("state".into()) - && prev_events == vec![EventId::try_from("$previousevent:matrix.org").unwrap()] + && prev_events == vec![event_id!("$previousevent:matrix.org")] && depth == 2_u32.into() - && auth_events == vec![EventId::try_from("$someauthevent:matrix.org").unwrap()] - && redacts == Some(EventId::try_from("$9654:matrix.org").unwrap()) + && auth_events == vec![event_id!("$someauthevent:matrix.org")] + && redacts == Some(event_id!("$9654:matrix.org")) && unsigned == (&unsigned).clone() && sha256 == "1233543bABACDEF" && signatures == (&signatures).clone() diff --git a/ruma-events/tests/redacted.rs b/ruma-events/tests/redacted.rs index 34c02ad6..43da7bd4 100644 --- a/ruma-events/tests/redacted.rs +++ b/ruma-events/tests/redacted.rs @@ -1,7 +1,4 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use matches::assert_matches; use ruma_common::Raw; @@ -18,7 +15,7 @@ use ruma_events::{ RedactedSyncMessageEvent, RedactedSyncStateEvent, RedactedSyncUnsigned, RedactedUnsigned, Unsigned, }; -use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id, RoomVersionId}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; fn sync_unsigned() -> RedactedSyncUnsigned { @@ -28,10 +25,10 @@ fn sync_unsigned() -> RedactedSyncUnsigned { // to the event type string. unsigned.redacted_because = Some(Box::new(SyncRedactionEvent { content: RedactionEventContent { reason: Some("redacted because".into()) }, - redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + redacts: event_id!("$h29iv0s8:example.com"), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - sender: UserId::try_from("@carl:example.com").unwrap(), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), })); @@ -42,11 +39,11 @@ fn full_unsigned() -> RedactedUnsigned { let mut unsigned = RedactedUnsigned::default(); unsigned.redacted_because = Some(Box::new(RedactionEvent { content: RedactionEventContent { reason: Some("redacted because".into()) }, - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + redacts: event_id!("$h29iv0s8:example.com"), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - sender: UserId::try_from("@carl:example.com").unwrap(), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), })); @@ -57,9 +54,9 @@ fn full_unsigned() -> RedactedUnsigned { fn redacted_message_event_serialize() { let redacted = RedactedSyncMessageEvent { content: RedactedMessageEventContent, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - sender: UserId::try_from("@carl:example.com").unwrap(), + sender: user_id!("@carl:example.com"), unsigned: RedactedSyncUnsigned::default(), }; @@ -78,10 +75,10 @@ fn redacted_message_event_serialize() { fn redacted_aliases_event_serialize_no_content() { let redacted = RedactedSyncStateEvent { content: RedactedAliasesEventContent { aliases: None }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), state_key: "".into(), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - sender: UserId::try_from("@carl:example.com").unwrap(), + sender: user_id!("@carl:example.com"), unsigned: RedactedSyncUnsigned::default(), }; @@ -101,10 +98,10 @@ fn redacted_aliases_event_serialize_no_content() { fn redacted_aliases_event_serialize_with_content() { let redacted = RedactedSyncStateEvent { content: RedactedAliasesEventContent { aliases: Some(vec![]) }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), state_key: "".to_string(), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - sender: UserId::try_from("@carl:example.com").unwrap(), + sender: user_id!("@carl:example.com"), unsigned: RedactedSyncUnsigned::default(), }; @@ -146,7 +143,7 @@ fn redacted_aliases_deserialize() { AnySyncRoomEvent::RedactedState(AnyRedactedSyncStateEvent::RoomAliases(RedactedSyncStateEvent { content: RedactedAliasesEventContent { aliases }, event_id, .. - })) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + })) if event_id == event_id!("$h29iv0s8:example.com") && aliases.is_none() ) } @@ -174,8 +171,8 @@ fn redacted_deserialize_any_room() { AnyRoomEvent::RedactedMessage(AnyRedactedMessageEvent::RoomMessage(RedactedMessageEvent { content: RedactedMessageEventContent, event_id, room_id, .. - })) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() - && room_id == RoomId::try_from("!roomid:room.com").unwrap() + })) if event_id == event_id!("$h29iv0s8:example.com") + && room_id == room_id!("!roomid:room.com") ) } @@ -187,11 +184,11 @@ fn redacted_deserialize_any_room_sync() { // to the event type string. unsigned.redacted_because = Some(Box::new(RedactionEvent { content: RedactionEventContent { reason: Some("redacted because".into()) }, - redacts: EventId::try_from("$h29iv0s8:example.com").unwrap(), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + redacts: event_id!("$h29iv0s8:example.com"), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), })); @@ -213,7 +210,7 @@ fn redacted_deserialize_any_room_sync() { AnySyncRoomEvent::RedactedMessage(AnyRedactedSyncMessageEvent::RoomMessage(RedactedSyncMessageEvent { content: RedactedMessageEventContent, event_id, .. - })) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + })) if event_id == event_id!("$h29iv0s8:example.com") ) } @@ -243,10 +240,10 @@ fn redacted_state_event_deserialize() { creator, }, event_id, state_key, unsigned, .. - })) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + })) if event_id == event_id!("$h29iv0s8:example.com") && unsigned.redacted_because.is_some() && state_key == "hello there" - && creator == UserId::try_from("@carl:example.com").unwrap() + && creator == user_id!("@carl:example.com") ) } @@ -273,7 +270,7 @@ fn redacted_custom_event_serialize() { event_type, }, event_id, state_key, unsigned, .. - })) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + })) if event_id == event_id!("$h29iv0s8:example.com") && unsigned.redacted_because.is_some() && state_key == "hello there" && event_type == "m.made.up" @@ -281,7 +278,7 @@ fn redacted_custom_event_serialize() { let x = from_json_value::>(redacted).unwrap().deserialize().unwrap(); - assert_eq!(x.event_id(), &EventId::try_from("$h29iv0s8:example.com").unwrap()) + assert_eq!(x.event_id(), &event_id!("$h29iv0s8:example.com")) } #[test] @@ -290,8 +287,8 @@ fn redacted_custom_event_deserialize() { let redacted = RedactedSyncStateEvent { content: RedactedCustomEventContent { event_type: "m.made.up".into() }, - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + event_id: event_id!("$h29iv0s8:example.com"), + sender: user_id!("@carl:example.com"), state_key: "hello there".into(), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), unsigned: unsigned.clone(), @@ -327,11 +324,11 @@ fn redact_method_properly_redacts() { let redaction = RedactionEvent { content: RedactionEventContent { reason: Some("redacted because".into()) }, - redacts: EventId::try_from("$143273582443PhrSn:example.com").unwrap(), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + redacts: event_id!("$143273582443PhrSn:example.com"), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), }; @@ -346,10 +343,10 @@ fn redact_method_properly_redacts() { sender, origin_server_ts, unsigned, - }) if event_id == EventId::try_from("$143273582443PhrSn:example.com").unwrap() + }) if event_id == event_id!("$143273582443PhrSn:example.com") && unsigned.redacted_because.is_some() - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@user:example.com").unwrap() + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@user:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) ); } diff --git a/ruma-events/tests/redaction.rs b/ruma-events/tests/redaction.rs index 735af8a4..53260cf1 100644 --- a/ruma-events/tests/redaction.rs +++ b/ruma-events/tests/redaction.rs @@ -1,7 +1,4 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use matches::assert_matches; use ruma_common::Raw; @@ -9,7 +6,7 @@ use ruma_events::{ room::redaction::{RedactionEvent, RedactionEventContent}, AnyMessageEvent, Unsigned, }; -use ruma_identifiers::{EventId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{ from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, }; @@ -32,11 +29,11 @@ fn redaction() -> JsonValue { fn serialize_redaction() { let aliases_event = RedactionEvent { content: RedactionEventContent { reason: Some("being a turd".into()) }, - redacts: EventId::try_from("$nomore:example.com").unwrap(), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + redacts: event_id!("$nomore:example.com"), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), }; @@ -64,11 +61,11 @@ fn deserialize_redaction() { sender, unsigned, }) if reas == "being a turd" - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() - && redacts == EventId::try_from("$nomore:example.com").unwrap() + && event_id == event_id!("$h29iv0s8:example.com") + && redacts == event_id!("$nomore:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@carl:example.com").unwrap() + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@carl:example.com") && unsigned.is_empty() ); } diff --git a/ruma-events/tests/state_event.rs b/ruma-events/tests/state_event.rs index 7fce4db7..144ca247 100644 --- a/ruma-events/tests/state_event.rs +++ b/ruma-events/tests/state_event.rs @@ -1,7 +1,4 @@ -use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, -}; +use std::time::{Duration, UNIX_EPOCH}; use js_int::UInt; use matches::assert_matches; @@ -11,7 +8,7 @@ use ruma_events::{ AnyRoomEvent, AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, RawExt, StateEvent, SyncStateEvent, Unsigned, }; -use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; +use ruma_identifiers::{event_id, room_alias_id, room_id, user_id}; use serde_json::{ from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, }; @@ -36,16 +33,16 @@ fn aliases_event_with_prev_content() -> JsonValue { #[test] fn serialize_aliases_with_prev_content() { let aliases_event = StateEvent { - content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ - RoomAliasId::try_from("#somewhere:localhost").unwrap(), - ])), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![room_alias_id!( + "#somewhere:localhost" + )])), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: Some(AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ - RoomAliasId::try_from("#inner:localhost").unwrap(), + room_alias_id!("#inner:localhost"), ]))), - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), state_key: "".into(), unsigned: Unsigned::default(), }; @@ -59,14 +56,14 @@ fn serialize_aliases_with_prev_content() { #[test] fn serialize_aliases_without_prev_content() { let aliases_event = StateEvent { - content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ - RoomAliasId::try_from("#somewhere:localhost").unwrap(), - ])), - event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(), + content: AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![room_alias_id!( + "#somewhere:localhost" + )])), + event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), prev_content: None, - room_id: RoomId::try_from("!roomid:room.com").unwrap(), - sender: UserId::try_from("@carl:example.com").unwrap(), + room_id: room_id!("!roomid:room.com"), + sender: user_id!("@carl:example.com"), state_key: "".into(), unsigned: Unsigned::default(), }; @@ -99,7 +96,7 @@ fn deserialize_aliases_content() { .deserialize_content("m.room.aliases") .unwrap(), AnyStateEventContent::RoomAliases(content) - if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] + if content.aliases == vec![room_alias_id!("#somewhere:localhost")] ); } @@ -121,12 +118,12 @@ fn deserialize_aliases_with_prev_content() { sender, state_key, unsigned, - } if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + } if content.aliases == vec![room_alias_id!("#somewhere:localhost")] + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()] - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@carl:example.com").unwrap() + && prev_content.aliases == vec![room_alias_id!("#inner:localhost")] + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@carl:example.com") && state_key == "" && unsigned.is_empty() ); @@ -148,11 +145,11 @@ fn deserialize_aliases_sync_with_room_id() { sender, state_key, unsigned, - } if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] - && event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + } if content.aliases == vec![room_alias_id!("#somewhere:localhost")] + && event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()] - && sender == UserId::try_from("@carl:example.com").unwrap() + && prev_content.aliases == vec![room_alias_id!("#inner:localhost")] + && sender == user_id!("@carl:example.com") && state_key == "" && unsigned.is_empty() ); @@ -203,10 +200,10 @@ fn deserialize_avatar_without_prev_content() { sender, state_key, unsigned - } if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + } if event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && room_id == RoomId::try_from("!roomid:room.com").unwrap() - && sender == UserId::try_from("@carl:example.com").unwrap() + && room_id == room_id!("!roomid:room.com") + && sender == user_id!("@carl:example.com") && state_key == "" && matches!( info.as_ref(), @@ -279,9 +276,9 @@ fn deserialize_member_event_with_top_level_membership_field() { sender, .. } - )) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap() + )) if event_id == event_id!("$h29iv0s8:example.com") && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && sender == UserId::try_from("@example:localhost").unwrap() + && sender == user_id!("@example:localhost") && content.displayname == Some("example".into()) ); } @@ -309,10 +306,10 @@ fn deserialize_full_event_convert_to_sync() { sender, state_key, unsigned, - }) if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()] + }) if content.aliases == vec![room_alias_id!("#somewhere:localhost")] && event_id == "$h29iv0s8:example.com" && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) - && prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()] + && prev_content.aliases == vec![room_alias_id!("#inner:localhost")] && sender == "@carl:example.com" && state_key == "" && unsigned.is_empty() diff --git a/ruma-events/tests/stripped.rs b/ruma-events/tests/stripped.rs index 567e1d95..2f1a9b26 100644 --- a/ruma-events/tests/stripped.rs +++ b/ruma-events/tests/stripped.rs @@ -1,11 +1,9 @@ -use std::convert::TryFrom; - use js_int::uint; use ruma_events::{ room::{join_rules::JoinRule, topic::TopicEventContent}, AnyStateEventContent, AnyStrippedStateEvent, StrippedStateEvent, }; -use ruma_identifiers::UserId; +use ruma_identifiers::user_id; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; #[test] @@ -15,7 +13,7 @@ fn serialize_stripped_state_event_any_content() { topic: "Testing room".into(), }), state_key: "".into(), - sender: UserId::try_from("@example:localhost").unwrap(), + sender: user_id!("@example:localhost"), }; let json_data = json!({ @@ -35,7 +33,7 @@ fn serialize_stripped_state_event_any_event() { let event = AnyStrippedStateEvent::RoomTopic(StrippedStateEvent { content: TopicEventContent { topic: "Testing room".into() }, state_key: "".into(), - sender: UserId::try_from("@example:localhost").unwrap(), + sender: user_id!("@example:localhost"), }); let json_data = json!({ diff --git a/ruma-events/tests/to_device.rs b/ruma-events/tests/to_device.rs index 09d9a012..4a3ca6fa 100644 --- a/ruma-events/tests/to_device.rs +++ b/ruma-events/tests/to_device.rs @@ -1,18 +1,16 @@ -use std::convert::TryFrom; - use ruma_events::{ room_key::RoomKeyEventContent, Algorithm, AnyToDeviceEventContent, ToDeviceEvent, }; -use ruma_identifiers::{RoomId, UserId}; +use ruma_identifiers::{room_id, user_id}; use serde_json::{json, to_value as to_json_value}; #[test] fn serialization() { let ev = ToDeviceEvent { - sender: UserId::try_from("@example:example.org").unwrap(), + sender: user_id!("@example:example.org"), content: AnyToDeviceEventContent::RoomKey(RoomKeyEventContent { algorithm: Algorithm::MegolmV1AesSha2, - room_id: RoomId::try_from("!testroomid:example.org").unwrap(), + room_id: room_id!("!testroomid:example.org"), session_id: "SessId".into(), session_key: "SessKey".into(), }), diff --git a/ruma-federation-api/src/serde/pdu_process_response.rs b/ruma-federation-api/src/serde/pdu_process_response.rs index 57c0a807..862508ed 100644 --- a/ruma-federation-api/src/serde/pdu_process_response.rs +++ b/ruma-federation-api/src/serde/pdu_process_response.rs @@ -70,9 +70,9 @@ impl<'de> Visitor<'de> for PduProcessResponseVisitor { #[cfg(test)] mod tests { - use std::{collections::BTreeMap, convert::TryFrom}; + use std::collections::BTreeMap; - use ruma_identifiers::EventId; + use ruma_identifiers::{event_id, EventId}; use serde_json::{json, value::Serializer as JsonSerializer}; use super::{deserialize, serialize}; @@ -80,10 +80,7 @@ mod tests { #[test] fn serialize_error() { let mut response: BTreeMap> = BTreeMap::new(); - response.insert( - EventId::try_from("$someevent:matrix.org").unwrap(), - Err("Some processing error.".into()), - ); + response.insert(event_id!("$someevent:matrix.org"), Err("Some processing error.".into())); let serialized = serialize(&response, JsonSerializer).unwrap(); let json = json!({ @@ -95,7 +92,7 @@ mod tests { #[test] fn serialize_ok() { let mut response: BTreeMap> = BTreeMap::new(); - response.insert(EventId::try_from("$someevent:matrix.org").unwrap(), Ok(())); + response.insert(event_id!("$someevent:matrix.org"), Ok(())); let serialized = serialize(&response, serde_json::value::Serializer).unwrap(); let json = json!({ @@ -111,7 +108,7 @@ mod tests { }); let response = deserialize(json).unwrap(); - let event_id = EventId::try_from("$someevent:matrix.org").unwrap(); + let event_id = event_id!("$someevent:matrix.org"); let event_response = response.get(&event_id).unwrap().clone().unwrap_err(); assert_eq!(event_response, "Some processing error."); @@ -124,7 +121,7 @@ mod tests { }); let response = deserialize(json).unwrap(); - let event_id = EventId::try_from("$someevent:matrix.org").unwrap(); + let event_id = event_id!("$someevent:matrix.org"); assert!(response.get(&event_id).unwrap().is_ok()); } @@ -136,7 +133,7 @@ mod tests { }); let response = deserialize(json).unwrap(); - let event_id = EventId::try_from("$someevent:matrix.org").unwrap(); + let event_id = event_id!("$someevent:matrix.org"); let event_response = response.get(&event_id).unwrap().clone().unwrap_err(); assert_eq!(event_response, ""); @@ -148,9 +145,6 @@ mod tests { "$someevent:matrix.org": {} }); let response = deserialize(json).unwrap(); - assert!(response - .get(&EventId::try_from("$someevent:matrix.org").unwrap()) - .unwrap() - .is_ok()); + assert!(response.get(&event_id!("$someevent:matrix.org")).unwrap().is_ok()); } }