-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modularize the event system and add helper macros for creating …
…messages
- Loading branch information
Showing
20 changed files
with
262 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[derive(Debug, Default)] | ||
pub struct ClearActivityEvent; | ||
|
||
impl ClearActivityEvent { | ||
pub fn on_clear_activity(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[derive(Debug, Default)] | ||
pub struct ConnectEvent; | ||
|
||
impl ConnectEvent { | ||
pub fn on_connect(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[derive(Debug, Default)] | ||
pub struct DisconnectEvent; | ||
|
||
impl DisconnectEvent { | ||
pub fn on_disconnect(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::types::Config; | ||
|
||
#[derive(Debug)] | ||
pub struct InitializeEvent { | ||
pub config: Config, | ||
} | ||
|
||
impl InitializeEvent { | ||
pub fn new(config: Config) -> Self { | ||
Self { config } | ||
} | ||
|
||
pub fn on_initialize(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::{ | ||
json::deserialize::{Deserializable, Json}, | ||
presence::activity::ActivityContext, | ||
types::Config, | ||
}; | ||
|
||
pub mod clear_activity; | ||
pub mod connect; | ||
pub mod disconnect; | ||
pub mod initialize; | ||
pub mod reset_timestamp; | ||
pub mod update_activity; | ||
pub mod update_workspace; | ||
|
||
pub use clear_activity::ClearActivityEvent; | ||
pub use connect::ConnectEvent; | ||
pub use disconnect::DisconnectEvent; | ||
pub use initialize::InitializeEvent; | ||
pub use reset_timestamp::ResetTimestampEvent; | ||
pub use update_activity::UpdateActivityEvent; | ||
pub use update_workspace::UpdateWorkspaceEvent; | ||
|
||
#[derive(Debug)] | ||
pub enum ClientEvent { | ||
Connect(ConnectEvent), | ||
Initialize(InitializeEvent), | ||
UpdateActivity(UpdateActivityEvent), | ||
ClearActivity(ClearActivityEvent), | ||
UpdateWorkspace(UpdateWorkspaceEvent), | ||
ResetTimestamp(ResetTimestampEvent), | ||
Disconnect(DisconnectEvent), | ||
} | ||
|
||
macro_rules! data { | ||
($map:expr) => { | ||
$map.get("data") | ||
.and_then(|v| v.as_map()) | ||
.ok_or("Missing or invalid 'data' field")? | ||
}; | ||
($map:expr, $expr:expr) => { | ||
$map.get("data") | ||
.and_then($expr) | ||
.ok_or("Missing or invalid 'data' field")? | ||
}; | ||
} | ||
|
||
impl ClientEvent { | ||
// { type: string, data: any } | ||
pub fn deserialize(json: &str) -> Result<Self, String> { | ||
let map = Json::deserialize(json)?; | ||
|
||
let ty = map | ||
.get("type") | ||
.and_then(|v| v.as_str()) | ||
.ok_or("Missing 'type' field")?; | ||
|
||
Ok(match ty { | ||
"connect" => Self::Connect(ConnectEvent), | ||
"initialize" => { | ||
Self::Initialize(InitializeEvent::new(Config::deserialize(data!(map))?)) | ||
} | ||
"update_activity" => Self::UpdateActivity(UpdateActivityEvent::new( | ||
ActivityContext::deserialize(data!(map))?, | ||
)), | ||
"clear_activity" => Self::ClearActivity(ClearActivityEvent), | ||
"update_workspace" => { | ||
Self::UpdateWorkspace(UpdateWorkspaceEvent::new(data!(map, |v| v.as_string()))) | ||
} | ||
"reset_timestamp" => Self::ResetTimestamp(ResetTimestampEvent), | ||
"disconnect" => Self::Disconnect(DisconnectEvent), | ||
_ => return Err(format!("Unknown message type: {}", ty)), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[derive(Debug, Default)] | ||
pub struct ResetTimestampEvent; | ||
|
||
impl ResetTimestampEvent { | ||
pub fn on_timestamp_reset(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::presence::activity::ActivityContext; | ||
|
||
#[derive(Debug)] | ||
pub struct UpdateActivityEvent { | ||
pub context: ActivityContext, | ||
} | ||
|
||
impl UpdateActivityEvent { | ||
pub fn new(context: ActivityContext) -> Self { | ||
Self { context } | ||
} | ||
|
||
pub fn on_update_activity(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[derive(Debug)] | ||
pub struct UpdateWorkspaceEvent { | ||
pub workspace: String, | ||
} | ||
|
||
impl UpdateWorkspaceEvent { | ||
pub fn new(workspace: String) -> Self { | ||
Self { workspace } | ||
} | ||
|
||
pub fn on_update_workspace(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use super::{client::ClientEvent, local::LocalEvent}; | ||
|
||
#[derive(Debug)] | ||
pub enum Event { | ||
Client(ClientEvent), | ||
Local(LocalEvent), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[derive(Debug, Default)] | ||
pub struct ClientDisconnectedEvent; | ||
|
||
impl ClientDisconnectedEvent { | ||
pub fn on_client_disconnected(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type Error = Box<dyn std::error::Error + Send + Sync>; | ||
|
||
#[derive(Debug)] | ||
pub struct ErrorEvent { | ||
pub error: Error, | ||
} | ||
|
||
impl ErrorEvent { | ||
pub fn new(error: Error) -> Self { | ||
Self { error } | ||
} | ||
|
||
pub fn on_error(self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub mod client_disconnected; | ||
pub mod error; | ||
|
||
pub use client_disconnected::ClientDisconnectedEvent; | ||
pub use error::ErrorEvent; | ||
|
||
#[derive(Debug)] | ||
pub enum LocalEvent { | ||
ClientDisconnected(ClientDisconnectedEvent), | ||
Error(ErrorEvent), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod client; | ||
pub mod event; | ||
pub mod local; |
Oops, something went wrong.