events: Generate accessor methods in a more readable manner

This commit is contained in:
Jonas Platte 2022-09-16 11:15:49 +02:00
parent e7a3c81d0d
commit df971d2c5c
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C

View File

@ -134,24 +134,22 @@ event_enum! {
} }
} }
/// Declares an item with a doc attribute computed by some macro expression. macro_rules! timeline_event_accessors {
/// This allows documentation to be dynamically generated based on input. (
/// Necessary to work around <https://github.com/rust-lang/rust/issues/52607>. $(
macro_rules! doc_concat { #[doc = $docs:literal]
( $( #[doc = $doc:expr] $( $thing:tt )* )* ) => ( $( #[doc = $doc] $( $thing )* )* ); pub fn $field:ident(&self) -> $ty:ty;
} )*
) => {
macro_rules! room_ev_accessor { $(
($field:ident: $ty:ty) => { #[doc = $docs]
doc_concat! {
#[doc = concat!("Returns this event's `", stringify!($field), "` field.")]
pub fn $field(&self) -> $ty { pub fn $field(&self) -> $ty {
match self { match self {
Self::MessageLike(ev) => ev.$field(), Self::MessageLike(ev) => ev.$field(),
Self::State(ev) => ev.$field(), Self::State(ev) => ev.$field(),
} }
} }
} )*
}; };
} }
@ -167,17 +165,21 @@ pub enum AnyTimelineEvent {
} }
impl AnyTimelineEvent { impl AnyTimelineEvent {
room_ev_accessor!(origin_server_ts: MilliSecondsSinceUnixEpoch); timeline_event_accessors! {
room_ev_accessor!(room_id: &RoomId); /// Returns this event's `origin_server_ts` field.
room_ev_accessor!(event_id: &EventId); pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
room_ev_accessor!(sender: &UserId);
/// Returns this event's `room_id` field.
pub fn room_id(&self) -> &RoomId;
/// Returns this event's `event_id` field.
pub fn event_id(&self) -> &EventId;
/// Returns this event's `sender` field.
pub fn sender(&self) -> &UserId;
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one. /// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&TransactionId> { pub fn transaction_id(&self) -> Option<&TransactionId>;
match self {
Self::MessageLike(ev) => ev.transaction_id(),
Self::State(ev) => ev.transaction_id(),
}
} }
} }
@ -195,16 +197,18 @@ pub enum AnySyncTimelineEvent {
} }
impl AnySyncTimelineEvent { impl AnySyncTimelineEvent {
room_ev_accessor!(origin_server_ts: MilliSecondsSinceUnixEpoch); timeline_event_accessors! {
room_ev_accessor!(event_id: &EventId); /// Returns this event's `origin_server_ts` field.
room_ev_accessor!(sender: &UserId); pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch;
/// Returns this event's `event_id` field.
pub fn event_id(&self) -> &EventId;
/// Returns this event's `sender` field.
pub fn sender(&self) -> &UserId;
/// Returns this event's `transaction_id` from inside `unsigned`, if there is one. /// Returns this event's `transaction_id` from inside `unsigned`, if there is one.
pub fn transaction_id(&self) -> Option<&TransactionId> { pub fn transaction_id(&self) -> Option<&TransactionId>;
match self {
Self::MessageLike(ev) => ev.transaction_id(),
Self::State(ev) => ev.transaction_id(),
}
} }
/// Converts `self` to an `AnyTimelineEvent` by adding the given a room ID. /// Converts `self` to an `AnyTimelineEvent` by adding the given a room ID.