falco_plugin/plugin/event/
event_input.rs

1use anyhow::Context;
2use falco_event::events::RawEvent;
3use falco_plugin_api::ss_plugin_event_input;
4use std::ffi::CStr;
5use std::marker::PhantomData;
6
7/// # An event from which additional data may be extracted
8#[derive(Debug)]
9pub struct EventInput<'a, T>(
10    pub(crate) ss_plugin_event_input,
11    pub(crate) PhantomData<fn(&'a T)>,
12);
13
14impl<'a, T> EventInput<'a, T>
15where
16    for<'b> T: TryFrom<&'b RawEvent<'a>>,
17    for<'b> <T as TryFrom<&'b RawEvent<'a>>>::Error: std::error::Error + Send + Sync + 'static,
18{
19    /// # Get the event
20    ///
21    /// This method parses the raw event data into another type, e.g. a [`RawEvent`] instance,
22    /// or a specific event type.
23    pub fn event(&self) -> anyhow::Result<T> {
24        let raw = unsafe { RawEvent::from_ptr(self.0.evt as *const _) }?;
25        let event = Ok(<&RawEvent<'_> as TryInto<T>>::try_into(&raw)
26            .with_context(|| format!("parsing event {raw:?}"))?);
27        #[allow(clippy::let_and_return)]
28        event
29    }
30
31    /// # Get the event source
32    ///
33    /// Return the event source (if any)
34    pub fn source(&self) -> Option<&CStr> {
35        unsafe {
36            if self.0.evtsrc.is_null() {
37                None
38            } else {
39                Some(CStr::from_ptr(self.0.evtsrc))
40            }
41        }
42    }
43
44    /// # Get the event number
45    ///
46    /// Return the event number as determined by the plugin framework
47    pub fn event_number(&self) -> usize {
48        self.0.evtnum as usize
49    }
50}