diff --git a/crates/proto/src/event.rs b/crates/proto/src/event.rs index 0c24a6ce48..e7e69fefa4 100644 --- a/crates/proto/src/event.rs +++ b/crates/proto/src/event.rs @@ -1,4 +1,4 @@ -use crate::{Message, Name}; +use crate::{DomainType, Message, Name}; use anyhow::{self, Context}; use serde::{de::DeserializeOwned, Serialize}; use std::collections::HashMap; @@ -121,3 +121,25 @@ mod tests { assert_eq!(proto_output, proto_output2); } } + +/// An extension trait allowing for easy conversion from events into domain types. +/// +/// This makes the task of writing code that processes events much more easy, +/// since you can just attempt to parse the event directly into the specific domain +/// type. +pub trait EventDomainType: DomainType +where + ::Proto: ProtoEvent, + anyhow::Error: From<::Proto>>::Error>, +{ + fn try_from_event(event: &abci::Event) -> anyhow::Result { + Ok(::Proto::from_event(event)?.try_into()?) + } +} + +impl EventDomainType for T +where + ::Proto: ProtoEvent, + anyhow::Error: From<::Proto>>::Error>, +{ +}