falco_plugin/plugin/event/
event_input.rs1use 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#[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 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 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 pub fn event_number(&self) -> usize {
48 self.0.evtnum as usize
49 }
50}