events: Hide EventType::Custom

This commit is contained in:
Jonas Platte 2020-08-29 23:39:08 +02:00
parent 961d45a581
commit 532e7a7233
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 20 additions and 10 deletions

View File

@ -21,6 +21,8 @@ Breaking changes:
* Use `ruma_identifiers::{ServerName, ServerKeyId}` in `signatures` fields of * Use `ruma_identifiers::{ServerName, ServerKeyId}` in `signatures` fields of
`pdu::RoomV1Pdu, RoomV1PduStub, RoomV3Pdu, RoomV3PduStub}` and `pdu::RoomV1Pdu, RoomV1PduStub, RoomV3Pdu, RoomV3PduStub}` and
`room::member::SignedContent`. `room::member::SignedContent`.
* Remove the `EventType::Custom` variant. You can still check for custom event types by going
through `.as_str()`. This ensures that new event types doesn't break existing code.
Improvements: Improvements:

View File

@ -3,6 +3,9 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// The type of an event. /// The type of an event.
///
/// This type can hold an arbitrary string. To check for events that are not available as a
/// documented variant here, use its string representation, obtained through `.as_str()`.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive] #[non_exhaustive]
#[serde(from = "String", into = "String")] #[serde(from = "String", into = "String")]
@ -136,13 +139,14 @@ pub enum EventType {
/// m.typing /// m.typing
Typing, Typing,
/// Any event that is not part of the specification. #[doc(hidden)]
Custom(String), _Custom(String),
} }
impl Display for EventType { impl EventType {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { /// Creates a string slice from this `EventType`.
let event_type_str = match *self { pub fn as_str(&self) -> &str {
match *self {
EventType::CallAnswer => "m.call.answer", EventType::CallAnswer => "m.call.answer",
EventType::CallCandidates => "m.call.candidates", EventType::CallCandidates => "m.call.candidates",
EventType::CallHangup => "m.call.hangup", EventType::CallHangup => "m.call.hangup",
@ -186,10 +190,14 @@ impl Display for EventType {
EventType::Sticker => "m.sticker", EventType::Sticker => "m.sticker",
EventType::Tag => "m.tag", EventType::Tag => "m.tag",
EventType::Typing => "m.typing", EventType::Typing => "m.typing",
EventType::Custom(ref event_type) => event_type, EventType::_Custom(ref event_type) => event_type,
}; }
}
}
write!(f, "{}", event_type_str) impl Display for EventType {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(self.as_str())
} }
} }
@ -242,7 +250,7 @@ where
"m.sticker" => EventType::Sticker, "m.sticker" => EventType::Sticker,
"m.tag" => EventType::Tag, "m.tag" => EventType::Tag,
"m.typing" => EventType::Typing, "m.typing" => EventType::Typing,
_ => EventType::Custom(s.into()), _ => EventType::_Custom(s.into()),
} }
} }
} }
@ -306,6 +314,6 @@ mod tests {
serde_json_eq(EventType::Sticker, json!("m.sticker")); serde_json_eq(EventType::Sticker, json!("m.sticker"));
serde_json_eq(EventType::Tag, json!("m.tag")); serde_json_eq(EventType::Tag, json!("m.tag"));
serde_json_eq(EventType::Typing, json!("m.typing")); serde_json_eq(EventType::Typing, json!("m.typing"));
serde_json_eq(EventType::Custom("io.ruma.test".into()), json!("io.ruma.test")); serde_json_eq(EventType::_Custom("io.ruma.test".into()), json!("io.ruma.test"));
} }
} }