Struct JsonPayload

Source
pub struct JsonPayload<T> { /* private fields */ }
Expand description

A wrapper that enables JSON-encoded event payloads in crate::event::AsyncEvent and crate::event::PluginEvent

To store an arbitrary type as JSON inside the payload, make sure the data implements Serialize and Deserialize from serde and use JsonPayload<T> as the payload type:

use falco_event::events::{AnyEventPayload, RawEvent};
use falco_plugin::event::{EventSource, JsonPayload, PluginEvent};
use falco_plugin::event::events::Event;

#[derive(serde::Serialize, serde::Deserialize)]
struct MyEvent {
    param1: u32,
    param2: u32,
}

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

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

Note: this SDK provides JSON support 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.

Implementations§

Source§

impl<T> JsonPayload<T>

Source

pub fn new(inner: T) -> Self

Create a JsonPayload object from any data

Source

pub fn get_ref(&self) -> &T

Get a reference to the data inside

Source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the data inside

Source

pub fn into_inner(self) -> T

Return the wrapped data, dropping the wrapper

Trait Implementations§

Source§

impl<T> Debug for JsonPayload<T>
where T: Debug,

Source§

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

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

impl<T> EventSource for JsonPayload<T>
where T: EventSource,

Source§

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

Source name Read more
Source§

impl<'a, T> FromBytes<'a> for JsonPayload<T>

Source§

fn from_bytes(buf: &mut &'a [u8]) -> Result<Self, FromBytesError>

Read the binary representation of a field and return the parsed representation Read more
§

fn from_maybe_bytes(buf: Option<&mut &'a [u8]>) -> Result<Self, FromBytesError>

Read the binary representation of a field from a buffer that may or may not exist Read more
Source§

impl<T> ToBytes for JsonPayload<T>
where T: Serialize,

Source§

fn binary_size(&self) -> usize

Return the number of bytes needed to store the field
Source§

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

Write the binary representation to writer
Source§

fn default_repr() -> impl ToBytes

Return the default representation for the field type Read more

Auto Trait Implementations§

§

impl<T> !Freeze for JsonPayload<T>

§

impl<T> !RefUnwindSafe for JsonPayload<T>

§

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

§

impl<T> !Sync for JsonPayload<T>

§

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

§

impl<T> !UnwindSafe for JsonPayload<T>

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
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.