Skip to content

Commit

Permalink
new(event): implement PayloadToBytes for AnyEvent
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek committed Dec 12, 2024
1 parent e1d5323 commit 3636925
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions falco_event_derive/src/event_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,20 @@ impl EventInfo {
}
)
}

fn variant_to_bytes(&self) -> proc_macro2::TokenStream {
let event_code = &self.event_code;
let event_type = Ident::new(
&event_code.to_string().replace("PPME_", ""),
event_code.span(),
);

quote!(
AnyEvent::#event_type(inner) => {
inner.write(metadata, writer)
}
)
}
}

struct Events {
Expand Down Expand Up @@ -459,12 +473,17 @@ impl Events {
fn variant_fmts(&self) -> impl Iterator<Item = proc_macro2::TokenStream> + '_ {
self.events.iter().map(|e| e.variant_fmt())
}

fn variants_to_bytes(&self) -> impl Iterator<Item = proc_macro2::TokenStream> + '_ {
self.events.iter().map(|e| e.variant_to_bytes())
}
}

fn event_info_variant(events: &Events, variant: CodegenVariant) -> proc_macro2::TokenStream {
let typedefs = events.typedefs(variant);
let variants = events.enum_variants(variant);
let variant_fmts = events.variant_fmts();
let variants_to_bytes = events.variants_to_bytes();
let lifetime = match variant {
CodegenVariant::Borrowed => Some(quote!(<'a>)),
CodegenVariant::Owned => None,
Expand Down Expand Up @@ -494,6 +513,14 @@ fn event_info_variant(events: &Events, variant: CodegenVariant) -> proc_macro2::
#(#variants,)*
}

impl #lifetime crate::event_derive::PayloadToBytes for AnyEvent #lifetime {
fn write<W: std::io::Write>(&self, metadata: &crate::event_derive::EventMetadata, writer: W) -> std::io::Result<()> {
match self {
#(#variants_to_bytes)*
}
}
}

impl #lifetime crate::event_derive::Format<crate::event_derive::format_type::PF_NA> for AnyEvent #lifetime {
fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Expand Down

0 comments on commit 3636925

Please sign in to comment.