Module export

Source
Expand description

Exporting tables to other plugins

Exporting a table to other plugins is done using the crate::tables::export::Entry derive macro. It lets you use a struct type as a parameter to export::Table. You can then create a new table using TablesInput::add_table.

Every field in the entry struct must be wrapped in Public, Private or Readonly, except for nested tables. These just need to be a Box<Table<K, E>>, as it makes no sense to have a private nested table and the distinction between writable and readonly is meaningless for tables (they have no setter to replace the whole table and you can always add/remove entries from the nested table).

§Example

use std::ffi::{CStr, CString};
use falco_plugin::base::Plugin;
use falco_plugin::tables::TablesInput;
use falco_plugin::tables::export;

// define the struct representing each table entry
#[derive(export::Entry)]
struct ExportedTable {
    int_field: export::Readonly<u64>,      // do not allow writes via the plugin API
    string_field: export::Public<CString>, // allow writes via the plugin API
    secret: export::Private<Vec<u8>>,      // do not expose over the plugin API at all
}

// define the type holding the plugin state
struct MyPlugin {
    // you can use methods on this instance to access fields bypassing the Falco table API
    // (for performance within your own plugin)
    exported_table: Box<export::Table<u64, ExportedTable>>,
}

// implement the base::Plugin trait
impl Plugin for MyPlugin {
    // ...

    fn new(input: Option<&TablesInput>, config: Self::ConfigType)
        -> Result<Self, anyhow::Error> {

        let Some(input) = input else {
            anyhow::bail!("Did not get tables input");
        };

        // create a new table called "exported"
        //
        // The concrete type is inferred from the field type the result is stored in.
        let exported_table = input.add_table(export::Table::new(c"exported")?)?;

        Ok(MyPlugin { exported_table })
    }
}

Structs§

Private
Do not export the field via Falco tables API
Public
Export the field via Falco tables API
Readonly
Export the field via Falco tables API with read-only access
Table
A table exported to other plugins

Derive Macros§

Entry
Mark a struct type as a table value