Skip to main content

falco_plugin/tables/import/
table_info.rs

1use crate::tables::FieldTypeId;
2use num_traits::FromPrimitive;
3use std::fmt::Debug;
4
5/// Information about a table
6#[repr(transparent)]
7pub struct TableInfo(falco_plugin_api::ss_plugin_table_info);
8
9impl Debug for TableInfo {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        f.debug_struct("TableInfo")
12            .field("name", &self.name())
13            .field("key_type", &self.key_type())
14            .finish()
15    }
16}
17
18impl TableInfo {
19    /// Returns the name of the table
20    ///
21    /// If the table name cannot be represented as UTF-8, returns "&lt;invalid table name&gt;"
22    pub fn name(&self) -> &str {
23        unsafe { std::ffi::CStr::from_ptr(self.0.name) }
24            .to_str()
25            .unwrap_or("<invalid table name>")
26    }
27
28    /// Returns the type of the table's key
29    ///
30    /// If the type cannot be represented as a [`FieldTypeId`], returns an error
31    /// with the raw field type value.
32    pub fn key_type(&self) -> Result<FieldTypeId, u32> {
33        match FieldTypeId::from_u32(self.0.key_type) {
34            Some(field_type) => Ok(field_type),
35            None => Err(self.0.key_type),
36        }
37    }
38}