-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
487 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use ceramic_event::unvalidated::{self, signed}; | ||
use ipld_core::ipld::Ipld; | ||
|
||
use super::UnvalidatedEvent; | ||
|
||
#[derive(Debug)] | ||
pub enum ValidationNeeded { | ||
SignedInit(SignedInit), | ||
SignedData(SignedData), | ||
Time(Time), | ||
None(Unsigned), | ||
} | ||
|
||
impl From<UnvalidatedEvent> for ValidationNeeded { | ||
fn from(value: UnvalidatedEvent) -> Self { | ||
match value.event.as_ref() { | ||
unvalidated::Event::Time(_) => Self::Time(Time(value)), | ||
unvalidated::Event::Signed(signed) => match signed.payload() { | ||
unvalidated::Payload::Data(_) => Self::SignedData(SignedData(value)), | ||
unvalidated::Payload::Init(_) => Self::SignedInit(SignedInit(value)), | ||
}, | ||
unvalidated::Event::Unsigned(_) => Self::None(Unsigned(value)), | ||
} | ||
} | ||
} | ||
|
||
/// This is a simple macro to create a tuple struct around UnvalidatedEvent and allow access | ||
/// to the inner value. It's purpose is to support implementing `From<UnvalidatedEvent> for ValidationNeeded` | ||
/// and get the correct structs that can be required in fn signatures, but still dereferenced as the | ||
/// original event type without having to match on all the branches when we require it to be a specific variant. | ||
macro_rules! unvalidated_wrapper { | ||
($name: ident) => { | ||
#[derive(Debug)] | ||
pub(crate) struct $name(UnvalidatedEvent); | ||
|
||
impl $name { | ||
#[allow(dead_code)] | ||
/// Get the inner tuple struct value | ||
pub fn into_inner(self) -> UnvalidatedEvent { | ||
self.0 | ||
} | ||
|
||
#[allow(dead_code)] | ||
/// Get the inner tuple struct value by reference | ||
pub fn as_inner(&self) -> &UnvalidatedEvent { | ||
&self.0 | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// Create an as `as_signed` function that panics for unsigned events. | ||
/// Will be derived on our signed structs to | ||
macro_rules! as_signed { | ||
($name: ident) => { | ||
impl $name { | ||
pub fn as_signed(&self) -> &signed::Event<Ipld> { | ||
match self.0.event.as_ref() { | ||
unvalidated::Event::Time(_) => unreachable!("time event is not signed"), | ||
unvalidated::Event::Signed(s) => s, | ||
unvalidated::Event::Unsigned(_) => unreachable!("unsigned event is not signed"), | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl Time { | ||
#[allow(dead_code)] | ||
pub fn as_time(&self) -> &unvalidated::TimeEvent { | ||
match self.0.event.as_ref() { | ||
unvalidated::Event::Time(t) => t, | ||
unvalidated::Event::Signed(_) => unreachable!("signed event event is not time"), | ||
unvalidated::Event::Unsigned(_) => unreachable!("unsigned event is not time"), | ||
} | ||
} | ||
} | ||
|
||
// Generate the tuple structs to use in the `ValidationNeeded` enum variants | ||
unvalidated_wrapper!(SignedData); | ||
unvalidated_wrapper!(SignedInit); | ||
unvalidated_wrapper!(Time); | ||
unvalidated_wrapper!(Unsigned); | ||
|
||
// Add `as_signed(&self) -> &signed::Event<Ipld>` functions to the signed types | ||
as_signed!(SignedData); | ||
as_signed!(SignedInit); | ||
|
||
#[derive(Debug)] | ||
pub struct GroupedEvents { | ||
pub time: Vec<Time>, | ||
pub unsigned: Vec<Unsigned>, | ||
pub signed: SignedEvents, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SignedEvents { | ||
pub data: Vec<SignedData>, | ||
pub init: Vec<SignedInit>, | ||
} | ||
|
||
impl From<Vec<UnvalidatedEvent>> for GroupedEvents { | ||
fn from(value: Vec<UnvalidatedEvent>) -> Self { | ||
let mut grouped = GroupedEvents::new(value.len() / 2); | ||
|
||
value | ||
.into_iter() | ||
.for_each(|v| grouped.add(ValidationNeeded::from(v))); | ||
grouped | ||
} | ||
} | ||
|
||
impl GroupedEvents { | ||
fn new(capacity: usize) -> Self { | ||
Self { | ||
time: Vec::with_capacity(capacity), | ||
unsigned: Vec::with_capacity(capacity), | ||
signed: SignedEvents { | ||
data: Vec::with_capacity(capacity), | ||
init: Vec::with_capacity(capacity), | ||
}, | ||
} | ||
} | ||
|
||
fn add(&mut self, new: ValidationNeeded) { | ||
match new { | ||
ValidationNeeded::SignedInit(v) => self.signed.init.push(v), | ||
ValidationNeeded::SignedData(v) => self.signed.data.push(v), | ||
ValidationNeeded::Time(v) => self.time.push(v), | ||
ValidationNeeded::None(v) => self.unsigned.push(v), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#[allow(dead_code)] | ||
mod event; | ||
mod grouped; | ||
mod signed; | ||
|
||
// ValidatedEvent is only used in tests currently | ||
#[allow(unused_imports)] | ||
pub use event::{EventValidator, UnvalidatedEvent, ValidatedEvent, ValidatedEvents}; | ||
pub use event::{SignedEventValidator, UnvalidatedEvent, ValidatedEvent, ValidatedEvents}; |
Oops, something went wrong.