Skip to content

Commit

Permalink
Added Event Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
CEbbinghaus committed Mar 3, 2024
1 parent e14ca59 commit 0dae2dd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 38 deletions.
91 changes: 56 additions & 35 deletions backend/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,9 @@ pub(crate) trait EventTrait<'a> {
}

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

pub fn parse(str: &'a str) -> impl EventTrait + 'a {
let mut event = EventBuilder::new();

for line in str.split("\n") {
let (key, value) = match line.split_once(":") {
None => {
warn!(line = line, "Malformed Event detected");
continue;
}
Some(key) => key,
};

if value.is_empty() {
warn!(key = key, "Event had empty data");
continue;
}

match key {
"id" => event.set_id(value),
"event" => event.set_event(value),
"data" => event.set_data(value),
_ => {
warn!(key=key, "Invalid key provided");
continue
}
};
}

event
}
}

impl<'a, T: EventTrait<'a>> ToString for Event<'a, T> {
Expand Down Expand Up @@ -93,15 +62,15 @@ impl<'a, T: EventTrait<'a>> From<T> for Event<'a, T> {
Event(value, PhantomData)
}
}

#[derive(Debug)]
pub(crate) struct EventBuilder<'a> {
id: Option<&'a str>,
event: Option<&'a str>,
data: Option<&'a str>,
}

#[allow(dead_code)]
impl <'a>EventBuilder<'a> {
impl<'a> EventBuilder<'a> {
pub fn new() -> Self {
EventBuilder {
id: None,
Expand Down Expand Up @@ -132,7 +101,7 @@ impl <'a>EventBuilder<'a> {
}
}

impl <'a> EventTrait<'a> for EventBuilder<'a> {
impl<'a> EventTrait<'a> for EventBuilder<'a> {
fn get_data(&self) -> Option<&'a str> {
self.data
}
Expand All @@ -143,3 +112,55 @@ impl <'a> EventTrait<'a> for EventBuilder<'a> {
self.id
}
}

#[derive(Debug)]
pub struct ParsedEvent<'a>(EventBuilder<'a>);

impl<'a> ParsedEvent<'a> {
pub fn parse(str: &'a str) -> ParsedEvent<'a> {
let mut event = EventBuilder::new();

for line in str.split("\n") {
let (key, mut value) = match line.split_once(":") {
None => {
warn!(line = line, "Malformed Event detected");
continue;
}
Some(key) => key,
};

if value.is_empty() {
warn!(key = key, "Event had empty data");
continue;
}

value = value.trim();

match key {
"id" => event.set_id(value),
"event" => event.set_event(value),
"data" => event.set_data(value),
_ => {
warn!(key = key, "Invalid key provided");
continue;
}
};
}

ParsedEvent(event)
}
}

impl<'a> EventTrait<'a> for ParsedEvent<'a> {
fn get_id(&self) -> Option<&'a str> {
self.0.get_id()
}

fn get_event(&self) -> Option<&'a str> {
self.0.get_event()
}

fn get_data(&self) -> Option<&'a str> {
self.0.get_data()
}
}
4 changes: 3 additions & 1 deletion backend/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub fn create_subscriber() {

if cfg!(debug_assertions) {
subscriber
.with(tracing_subscriber::fmt::layer().pretty())
.with(tracing_subscriber::fmt::layer().pretty().with_filter(
tracing_subscriber::filter::LevelFilter::from_level(CONFIG.log_level),
))
.init();
} else {
subscriber.init();
Expand Down
6 changes: 4 additions & 2 deletions backend/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::{select, sync::broadcast::*, time::interval};
use tracing::{debug, error, info, warn};


use crate::{dto::CardEvent, event::Event};
use crate::{dto::CardEvent, event::{Event, ParsedEvent}};

/// How often heartbeat pings are sent.
///
Expand Down Expand Up @@ -97,7 +97,9 @@ pub async fn handle_ws(
debug!("msg: {msg:?}");

match msg {
Message::Text(_) => {
Message::Text(data) => {
let event = ParsedEvent::parse(&data);
info!(event = ?event, "Recieved Event");
let _ = channel.send(CardEvent::Updated);
// drop client's text messages
}
Expand Down

0 comments on commit 0dae2dd

Please sign in to comment.