falco_plugin/plugin/source/
open_params.rs

1use serde::ser::SerializeStruct;
2use serde::{Serialize, Serializer};
3use std::ffi::{CStr, CString};
4
5/// # Specification of open parameters for a source plugin instance
6///
7/// **Note**: this appears unused as of API version 3.4.0
8#[derive(Debug)]
9pub enum OpenParam<'a> {
10    /// # A single string valid as a sample open parameter
11    Item {
12        /// the value itself
13        value: &'a str,
14        /// the description
15        desc: &'a str,
16    },
17    /// # A sequence of strings, each valid as a sample open parameter
18    Seq {
19        /// the values itself
20        values: &'a [&'a str],
21        /// the description
22        desc: &'a str,
23        /// the separator used to join the values together
24        separator: char,
25    },
26}
27
28impl Serialize for OpenParam<'_> {
29    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30    where
31        S: Serializer,
32    {
33        match self {
34            OpenParam::Item { value, desc } => {
35                let mut ss = serializer.serialize_struct("param", 2)?;
36                ss.serialize_field("value", value)?;
37                ss.serialize_field("desc", desc)?;
38                ss.end()
39            }
40            OpenParam::Seq {
41                values,
42                desc,
43                separator,
44            } => {
45                let mut sep = [0u8; 4];
46                let sep = separator.encode_utf8(sep.as_mut_slice());
47                let value = values.join(sep);
48                let mut ss = serializer.serialize_struct("param", 3)?;
49                ss.serialize_field("value", value.as_str())?;
50                ss.serialize_field("desc", desc)?;
51                ss.serialize_field("separator", &separator)?;
52                ss.end()
53            }
54        }
55    }
56}
57
58/// # Serialize the open parameter specification
59///
60/// This function can be used in [`SourcePlugin::list_open_params`](`crate::source::SourcePlugin::list_open_params`)
61/// to describe the allowed values for the instance open parameters.
62///
63/// **Note**: this appears unused as of API version 3.4.0
64pub fn serialize_open_params<'a>(
65    params: &[OpenParam],
66    storage: &'a mut CString,
67) -> Result<&'a CStr, anyhow::Error> {
68    let buf = serde_json::to_string_pretty(params)?;
69    let mut buf = CString::new(buf)?;
70    std::mem::swap(&mut buf, storage);
71    Ok(storage.as_c_str())
72}