Skip to content

Commit

Permalink
Updated events to allow for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
CEbbinghaus committed Mar 2, 2024
1 parent 0c25a65 commit e14ca59
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 29 deletions.
4 changes: 2 additions & 2 deletions backend/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub enum CardEvent {
Updated,
}

impl EventTrait for CardEvent {
fn get_event(&self) -> Option<&'static str> {
impl<'a> EventTrait<'a> for CardEvent {
fn get_event(&self) -> Option<&'a str> {
Some(match self {
CardEvent::Inserted => "insert",
CardEvent::Removed => "remove",
Expand Down
97 changes: 70 additions & 27 deletions backend/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
use std::marker::PhantomData;

use actix_web::web::Bytes;
use tracing::warn;

pub(crate) struct Event<T: EventTrait>(T);
pub(crate) struct Event<'a, T: EventTrait<'a>>(T, PhantomData<&'a T>);

pub(crate) trait EventTrait {
fn get_id(&self) -> Option<&'static str> {
pub(crate) trait EventTrait<'a> {
fn get_id(&self) -> Option<&'a str> {
None
}
fn get_event(&self) -> Option<&'static str> {
fn get_event(&self) -> Option<&'a str> {
None
}
fn get_data(&self) -> Option<&'static str> {
fn get_data(&self) -> Option<&'a str> {
None
}
}

impl<T: EventTrait> Event<T> {
pub fn new(val: T) -> Self {
Event(val)
impl<'a, T: EventTrait<'a>> Event<'a, T> {
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<T: EventTrait> ToString for Event<T> {
impl<'a, T: EventTrait<'a>> ToString for Event<'a, T> {
fn to_string(&self) -> String {
let mut output = "".to_string();

Expand All @@ -42,61 +76,70 @@ impl<T: EventTrait> ToString for Event<T> {
}
}

impl<T: EventTrait> From<Event<T>> for Bytes {
fn from(val: Event<T>) -> Self {
impl<'a, T: EventTrait<'a>> From<Event<'a, T>> for Bytes {
fn from(val: Event<'a, T>) -> Self {
Bytes::from(val.to_string())
}
}

impl<T: EventTrait> From<Event<T>> for String {
fn from(val: Event<T>) -> Self {
impl<'a, T: EventTrait<'a>> From<Event<'a, T>> for String {
fn from(val: Event<'a, T>) -> Self {
val.to_string()
}
}

impl<T: EventTrait> From<T> for Event<T> {
impl<'a, T: EventTrait<'a>> From<T> for Event<'a, T> {
fn from(value: T) -> Self {
Event(value)
Event(value, PhantomData)
}
}

pub(crate) struct EventBuilder {
id: Option<&'static str>,
event: Option<&'static str>,
data: Option<&'static str>,
pub(crate) struct EventBuilder<'a> {
id: Option<&'a str>,
event: Option<&'a str>,
data: Option<&'a str>,
}

#[allow(dead_code)]
impl EventBuilder {
impl <'a>EventBuilder<'a> {
pub fn new() -> Self {
EventBuilder {
id: None,
event: None,
data: None,
}
}
pub fn with_id(mut self, id: &'static str) -> Self {
pub fn with_id(mut self, id: &'a str) -> Self {
self.id = Some(id);
self
}
pub fn with_event(mut self, event: &'static str) -> Self {
pub fn set_id(&mut self, id: &'a str) {
self.id = Some(id);
}
pub fn with_event(mut self, event: &'a str) -> Self {
self.event = Some(event);
self
}
pub fn with_data(mut self, data: &'static str) -> Self {
pub fn set_event(&mut self, event: &'a str) {
self.event = Some(event);
}
pub fn with_data(mut self, data: &'a str) -> Self {
self.data = Some(data);
self
}
pub fn set_data(&mut self, data: &'a str) {
self.data = Some(data);
}
}

impl EventTrait for EventBuilder {
fn get_data(&self) -> Option<&'static str> {
impl <'a> EventTrait<'a> for EventBuilder<'a> {
fn get_data(&self) -> Option<&'a str> {
self.data
}
fn get_event(&self) -> Option<&'static str> {
fn get_event(&self) -> Option<&'a str> {
self.event
}
fn get_id(&self) -> Option<&'static str> {
fn get_id(&self) -> Option<&'a str> {
self.id
}
}

0 comments on commit e14ca59

Please sign in to comment.