falco_event/events/
payload.rs

1use crate::events::types::EventType;
2use crate::events::EventMetadata;
3use crate::fields::FromBytesError;
4use std::io::Write;
5use thiserror::Error;
6
7#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
8pub enum EventDirection {
9    Entry,
10    Exit,
11}
12
13pub trait EventPayload {
14    const ID: EventType;
15    const NAME: &'static str;
16
17    type LengthType;
18
19    fn direction() -> EventDirection {
20        match Self::ID as u32 % 2 {
21            0 => EventDirection::Entry,
22            1 => EventDirection::Exit,
23            _ => unreachable!(),
24        }
25    }
26}
27
28#[derive(Debug, Error)]
29pub enum PayloadFromBytesError {
30    /// Failed to parse a particular field
31    #[error("failed to parse field {0}")]
32    NamedField(&'static str, #[source] FromBytesError),
33
34    /// Type mismatch
35    #[error("type mismatch")]
36    TypeMismatch,
37
38    /// Truncated event
39    #[error("truncated event (wanted {wanted}, got {got})")]
40    TruncatedEvent { wanted: usize, got: usize },
41
42    /// Unsupported event type
43    #[error("unsupported event type {0}")]
44    UnsupportedEventType(u32),
45}
46
47pub trait PayloadToBytes {
48    fn binary_size(&self) -> usize;
49
50    fn write<W: Write>(&self, metadata: &EventMetadata, writer: W) -> std::io::Result<()>;
51}