mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-11-30 04:40:47 +01:00
handler macro
This commit is contained in:
parent
5be2b0a473
commit
e846fcceae
5 changed files with 91 additions and 56 deletions
|
|
@ -46,3 +46,59 @@ impl<Head: EventHandler, Tail: EventHandler> EventHandler for HandlerStack<Head,
|
|||
(self.head.finish(subjects), self.tail.finish(subjects))
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! handler {
|
||||
($name:ident {$($child:ident: $ty:path),*}) => {
|
||||
handler!($name { $($child: $ty,)* } );
|
||||
};
|
||||
($name:ident {$($child:ident: $ty:path,)*}) => {
|
||||
paste::paste! {
|
||||
#[derive(Default)]
|
||||
pub struct $name {
|
||||
$($child: $ty),*
|
||||
}
|
||||
|
||||
pub struct [<$name Output>] {
|
||||
$($child: <$ty as $crate::EventHandler>::Output),*
|
||||
}
|
||||
|
||||
impl serde::Serialize for [<$name Output>] {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
use serde::ser::SerializeStruct;
|
||||
let mut state = serializer.serialize_struct(concat!("$name", "output"), 3)?;
|
||||
$(state.serialize_field("$child", &self.$child)?;)*
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl $crate::EventHandler for $name {
|
||||
type Output = [<$name Output>];
|
||||
|
||||
fn does_handle(&self, ty: $crate::RawEventType) -> bool {
|
||||
#[allow(unused_imports)]
|
||||
use $crate::EventHandler;
|
||||
$(self.$child.does_handle(ty))||*
|
||||
}
|
||||
|
||||
fn handle(&mut self, time: u32, subject: $crate::SubjectId, event: &$crate::GameEvent) {
|
||||
#[allow(unused_imports)]
|
||||
use $crate::EventHandler;
|
||||
$(self.$child.handle(time, subject, event);)*
|
||||
}
|
||||
|
||||
fn finish(self, subjects: &$crate::SubjectMap) -> Self::Output {
|
||||
#[allow(unused_imports)]
|
||||
use $crate::EventHandler;
|
||||
Self::Output {
|
||||
$($child: self.$child.finish(subjects),)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue