falco_event/events/
event.rs

1use crate::events::to_bytes::EventToBytes;
2use crate::events::{AnyEventPayload, EventMetadata, PayloadToBytes};
3use crate::events::{FromRawEvent, PayloadFromBytesError, RawEvent};
4use std::fmt::{Debug, Formatter};
5use std::io::Write;
6
7/// A Falco event.
8///
9/// This struct represents a Falco event with metadata and parameters. The [`EventMetadata`] contains
10/// fields common to all events (the timestamp and thread ID), while the `params` field contains
11/// the event-specific data.
12#[derive(Clone)]
13pub struct Event<T> {
14    /// The metadata for the event, which includes common fields like timestamp and thread ID.
15    pub metadata: EventMetadata,
16
17    /// The parameters for the event, which are specific to the type of event.
18    pub params: T,
19}
20
21impl<T: Debug> Debug for Event<T> {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        write!(f, "{:?} {:?}", self.metadata, self.params)
24    }
25}
26
27impl<T: PayloadToBytes> EventToBytes for Event<T> {
28    #[inline]
29    fn binary_size(&self) -> usize {
30        26 + self.params.binary_size()
31    }
32
33    #[inline]
34    fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
35        self.params.write(&self.metadata, writer)
36    }
37}
38
39impl<'a, 'b, T: FromRawEvent<'a>> TryFrom<&'b RawEvent<'a>> for Event<T> {
40    type Error = PayloadFromBytesError;
41
42    #[inline]
43    fn try_from(raw: &'b RawEvent<'a>) -> Result<Self, Self::Error> {
44        raw.load::<T>()
45    }
46}
47
48impl<T: AnyEventPayload> AnyEventPayload for Event<T> {
49    const SOURCES: &'static [Option<&'static str>] = T::SOURCES;
50    const EVENT_TYPES: &'static [u16] = T::EVENT_TYPES;
51}