Skip to content

Commit

Permalink
refactored websocket event to simplify it
Browse files Browse the repository at this point in the history
  • Loading branch information
CEbbinghaus committed Feb 25, 2024
1 parent b4e8788 commit b6cd053
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions backend/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use actix_web::web::Bytes;

pub(crate) struct Event<T : EventTrait> {
val: T
}
pub(crate) struct Event<T : EventTrait>(T);

pub(crate) trait EventTrait {
fn get_id(&self) -> Option<&'static str> {
Expand All @@ -18,29 +16,29 @@ pub(crate) trait EventTrait {

impl<T: EventTrait> Event<T> {
pub fn new(val: T) -> Self {
Event { val }
Event(val)
}
}

impl<T : EventTrait> ToString for Event<T> {
fn to_string(&self) -> String {
let mut output = "".to_string();

if let Some(value) = self.val.get_id() {
if let Some(value) = self.0.get_id() {
output += &format!("id: {}\n", value);
}
if let Some(value) = self.val.get_event() {
if let Some(value) = self.0.get_event() {
output += &format!("event: {}\n", value);
}
if let Some(value) = self.val.get_data() {
if let Some(value) = self.0.get_data() {
output += &format!("data: {}\n", value);
}

if output != "" {
output += "\n";
}

return output;
output
}
}

Expand All @@ -50,9 +48,15 @@ impl<T: EventTrait> Into<Bytes> for Event<T> {
}
}

impl<T: EventTrait> Into<String> for Event<T> {
fn into(self) -> String {
self.to_string()
}
}

impl<T: EventTrait> From<T> for Event<T> {
fn from(value: T) -> Self {
Event { val: value }
Event(value)
}
}

Expand Down Expand Up @@ -90,4 +94,4 @@ impl EventTrait for EventBuilder {
fn get_id(&self) -> Option<&'static str> {
self.id
}
}
}

0 comments on commit b6cd053

Please sign in to comment.