Struct PluginEvent

Source
pub struct PluginEvent<T>
where T: EventSource,
{ pub plugin_id: u32, pub event_data: T, }
Expand description

Plugin event

This is the event type generated by source plugins. This differs from the autogenerated falco_event_schema::types::PPME_PLUGINEVENT_E type in the following ways:

  • none of the fields are Options
  • serde serialization/deserialization is not supported
  • does not depend on the whole falco_event_schema crate

It handles encoding and decoding the payload automatically, so you can use any type that can be converted from/to a byte buffer (including a raw &[u8]) as the payload.

To store an arbitrary type inside the payload, make sure the data implements FromBytes, ToBytes and EventSource, for example:

use std::io::Write;
use falco_event::events::{AnyEventPayload, RawEvent};
use falco_event::fields::{FromBytes, FromBytesError, ToBytes};
use falco_plugin::event::{EventSource, PluginEvent};
use falco_plugin::event::events::Event;

struct MyEvent {
    param1: u32,
    param2: u32,
}

impl EventSource for MyEvent {
    const SOURCE: Option<&'static str> = Some("my_plugin");
}

impl FromBytes<'_> for MyEvent {
    fn from_bytes(buf: &mut &[u8]) -> Result<Self, FromBytesError> {
        if buf.len() < 2 * size_of::<u32>() {
            return Err(FromBytesError::InvalidLength);
        }

        let param1 = u32::from_le_bytes(buf[..size_of::<u32>()].try_into().unwrap());
        let param2 = u32::from_le_bytes(buf[size_of::<u32>()..].try_into().unwrap());
        *buf = &buf[2 * size_of::<u32>()..];
        Ok(MyEvent { param1, param2 })
    }
}

impl ToBytes for MyEvent {
    fn binary_size(&self) -> usize {
        2 * size_of::<u32>()
    }

   fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
        writer.write_all(&self.param1.to_le_bytes())?;
        writer.write_all(&self.param2.to_le_bytes())?;
        Ok(())
    }

    fn default_repr() -> impl ToBytes {
        MyEvent { param1: 0, param2: 0 }
    }
}

// in a plugin trait implementation:
type Event<'a> = Event<PluginEvent<MyEvent>>;

Note: this SDK also provides support for JSON-encoded payloads since it’s already necessary to talk to the Falco Plugin API. JSON is not a good choice for high-volume events, as it takes a lot of space and is pretty slow, compared to binary formats. See the source of plugin::event:json for what is needed to support a different serialization format and consider using e.g. bincode instead.

Fields§

§plugin_id: u32

ID of the plugin that generated this event

§event_data: T

Payload, as a custom type

Trait Implementations§

Source§

impl<T> Debug for PluginEvent<T>
where T: EventSource + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> EventPayload for PluginEvent<T>
where T: EventSource,

Source§

const ID: u16 = 322u16

Source§

const SOURCE: Option<&'static str> = <T as EventSource>::SOURCE

Source§

impl<'raw_event, T> FromRawEvent<'raw_event> for PluginEvent<T>
where T: EventSource + FromBytes<'raw_event>,

Source§

fn parse(raw: &RawEvent<'raw_event>) -> Result<Self, PayloadFromBytesError>

Parse a raw event into the type implementing this trait
Source§

impl<T> PayloadToBytes for PluginEvent<T>
where T: EventSource + ToBytes,

Source§

fn binary_size(&self) -> usize

Get the binary size of the payload Read more
Source§

fn write<W: Write>(&self, metadata: &EventMetadata, writer: W) -> Result<()>

Write the payload to a writer implementing [std::io::Write].

Auto Trait Implementations§

§

impl<T> Freeze for PluginEvent<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for PluginEvent<T>
where T: RefUnwindSafe,

§

impl<T> Send for PluginEvent<T>
where T: Send,

§

impl<T> Sync for PluginEvent<T>
where T: Sync,

§

impl<T> Unpin for PluginEvent<T>
where T: Unpin,

§

impl<T> UnwindSafe for PluginEvent<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AnyEventPayload for T
where T: EventPayload,

§

const SOURCES: &'static [Option<&'static str>]

The sources of the events that this payload type can represent.
§

const EVENT_TYPES: &'static [u16]

The event types that this payload type can represent.
§

fn event_sources() -> Vec<&'static str>

Get all the event sources for this payload type Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.