Add EventKind trait.

This commit is contained in:
Jimmy Cuadra 2016-06-23 04:13:43 -07:00
parent 3a90a96708
commit b84fc9b23e
4 changed files with 27 additions and 11 deletions

View File

@ -20,6 +20,20 @@ pub mod stripped;
pub mod tag;
pub mod typing;
/// A basic event.
#[derive(Debug, Deserialize, Serialize)]
pub struct Event<T> where T: Deserialize + Serialize {
pub content: T,
#[serde(rename="type")]
pub event_type: EventType,
}
/// A type that represents a kind of Matrix event.
///
/// The event kinds are basic events, room events, and state events.
/// This trait can be useful to constrain a generic parameter that must be a Matrix event.
pub trait EventKind: Deserialize + Serialize {}
/// The type of an event.
#[derive(Debug, Deserialize, Serialize)]
pub enum EventType {
@ -47,14 +61,6 @@ pub enum EventType {
Typing,
}
/// A basic event.
#[derive(Debug, Deserialize, Serialize)]
pub struct Event<T> where T: Deserialize + Serialize {
pub content: T,
#[serde(rename="type")]
pub event_type: EventType,
}
/// An event within the context of a room.
#[derive(Debug, Deserialize, Serialize)]
pub struct RoomEvent<T> where T: Deserialize + Serialize {
@ -83,6 +89,10 @@ pub struct StateEvent<T> where T: Deserialize + Serialize {
pub user_id: String,
}
impl<T> EventKind for Event<T> where T: Deserialize + Serialize {}
impl<T> EventKind for RoomEvent<T> where T: Deserialize + Serialize {}
impl<T> EventKind for StateEvent<T> where T: Deserialize + Serialize {}
impl Display for EventType {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
let event_type_str = match *self {

View File

@ -1,6 +1,6 @@
//! Types for the *m.presence* event.
use EventType;
use {EventKind, EventType};
/// Informs the client of a user's presence state change.
#[derive(Debug, Deserialize, Serialize)]
@ -38,3 +38,5 @@ pub enum PresenceState {
/// Connected to the service but not available for chat.
Unavailable,
}
impl EventKind for PresenceEvent {}

View File

@ -2,7 +2,7 @@
use std::collections::HashMap;
use EventType;
use {EventKind, EventType};
/// Informs the client of new receipts.
#[derive(Debug, Deserialize, Serialize)]
@ -37,3 +37,5 @@ pub struct Receipt {
/// The timestamp the receipt was sent at.
pub ts: u64,
}
impl EventKind for ReceiptEvent {}

View File

@ -1,6 +1,6 @@
//! Types for the *m.typing* event.
use EventType;
use {EventKind, EventType};
/// Informs the client of the list of users currently typing.
#[derive(Debug, Deserialize, Serialize)]
@ -19,3 +19,5 @@ pub struct TypingEventContent {
/// The list of user IDs typing in this room, if any.
pub user_ids: Vec<String>,
}
impl EventKind for TypingEvent {}