Skip to content

Commit

Permalink
twiq: Add impl {Display,FromStr} for EventType
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Sep 23, 2022
1 parent 4ab11a4 commit b7900a8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion twiq/crates/domain/src/event.rs
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{fmt::Display, str::FromStr};

use event_store_core::{Event as RawEvent, EventType as RawEventType};

Expand Down Expand Up @@ -40,6 +40,22 @@ impl EventType {
}
}

impl Display for EventType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", RawEventType::from(*self))
}
}

impl FromStr for EventType {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let raw_event_type =
RawEventType::from_str(s).map_err(|e| Error::Unknown(e.to_string()))?;
EventType::try_from(raw_event_type)
}
}

impl From<EventType> for RawEventType {
fn from(event_type: EventType) -> Self {
use EventType::*;
Expand Down Expand Up @@ -127,6 +143,11 @@ mod tests {
let deserialized: EventType =
serde_json::from_str(r#"{"type":"user_created","ignore":"unused"}"#)?;
assert_eq!(deserialized, UserCreated);

assert_eq!(
EventType::from_str("user_created")?.to_string(),
"user_created"
);
Ok(())
}
}

0 comments on commit b7900a8

Please sign in to comment.