diff --git a/backend/src/event.rs b/backend/src/event.rs index ac492ea..e1fbb0d 100644 --- a/backend/src/event.rs +++ b/backend/src/event.rs @@ -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> { @@ -93,7 +62,7 @@ impl<'a, T: EventTrait<'a>> From for Event<'a, T> { Event(value, PhantomData) } } - +#[derive(Debug)] pub(crate) struct EventBuilder<'a> { id: Option<&'a str>, event: Option<&'a str>, @@ -101,7 +70,7 @@ pub(crate) struct EventBuilder<'a> { } #[allow(dead_code)] -impl <'a>EventBuilder<'a> { +impl<'a> EventBuilder<'a> { pub fn new() -> Self { EventBuilder { id: None, @@ -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 } @@ -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() + } +} diff --git a/backend/src/log.rs b/backend/src/log.rs index b282200..b6bb7f4 100644 --- a/backend/src/log.rs +++ b/backend/src/log.rs @@ -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(); diff --git a/backend/src/ws.rs b/backend/src/ws.rs index 08241bf..5d2ac6f 100644 --- a/backend/src/ws.rs +++ b/backend/src/ws.rs @@ -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. /// @@ -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 }