Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto: add extension trait for easier parsing of domain types #4886

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion crates/proto/src/event.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
<Self as DomainType>::Proto: ProtoEvent,
anyhow::Error: From<<Self as TryFrom<<Self as DomainType>::Proto>>::Error>,
{
fn try_from_event(event: &abci::Event) -> anyhow::Result<Self> {
Ok(<Self as DomainType>::Proto::from_event(event)?.try_into()?)
}
}

impl<T: DomainType> EventDomainType for T
where
<T as DomainType>::Proto: ProtoEvent,
anyhow::Error: From<<Self as TryFrom<<Self as DomainType>::Proto>>::Error>,
{
}
Loading