falco_plugin/plugin/exported_tables/field/
public.rs

1use crate::plugin::exported_tables::field_value::dynamic::DynamicFieldValue;
2use crate::plugin::exported_tables::field_value::traits::FieldValue;
3use crate::plugin::exported_tables::field_value::traits::{seal, StaticField};
4use crate::plugin::exported_tables::metadata::HasMetadata;
5use crate::plugin::tables::data::FieldTypeId;
6use anyhow::Error;
7use falco_plugin_api::ss_plugin_state_data;
8use std::ffi::CStr;
9use std::ops::{Deref, DerefMut};
10
11/// Export the field via Falco tables API
12///
13/// This is a wrapper that tells the Rust SDK to export a field to other plugins
14/// with write access.
15///
16/// This type implements [`Deref`] and [`DerefMut`], so you do not need any extra
17/// code when accessing the actual data.
18#[derive(Debug)]
19pub struct Public<T>(T);
20
21impl<T: FieldValue + Default> HasMetadata for Public<T> {
22    type Metadata = ();
23
24    fn new_with_metadata(_tag: &'static CStr, _meta: &Self::Metadata) -> Result<Self, Error> {
25        Ok(Self(T::default()))
26    }
27}
28
29impl<T> Deref for Public<T> {
30    type Target = T;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl<T> DerefMut for Public<T> {
38    fn deref_mut(&mut self) -> &mut Self::Target {
39        &mut self.0
40    }
41}
42
43impl<T: FieldValue> seal::Sealed for Public<T> {}
44
45impl<T: FieldValue> FieldValue for Public<T> {
46    fn to_data(&self, out: &mut ss_plugin_state_data, type_id: FieldTypeId) -> Result<(), Error> {
47        self.0.to_data(out, type_id)
48    }
49}
50
51impl<T: StaticField> StaticField for Public<T> {
52    const TYPE_ID: FieldTypeId = T::TYPE_ID;
53    const READONLY: bool = T::READONLY;
54}
55
56impl<T: TryFrom<DynamicFieldValue>> TryFrom<DynamicFieldValue> for Public<T> {
57    type Error = T::Error;
58
59    fn try_from(value: DynamicFieldValue) -> Result<Self, Self::Error> {
60        Ok(Self(T::try_from(value)?))
61    }
62}