Skip to main content

falco_plugin/tables/import/
field_info.rs

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