If a field does not implement Display (e.g., an Option<T>), you can specify a custom display function using #[tabled(display = "func")].
Function Signatures
- Simple function:
fn(&Type) -> String - Function with arguments:
#[tabled(display("func_name", arg1, arg2, self))]. You can pass specific values or self to the function.
Global Type Formatting
You can apply a display function to all fields of a specific type within a struct using #[tabled(display(Type, "func", "default"))] to reduce boilerplate.
use tabled::Tabled;
// Example 1: Simple display function
#[derive(Tabled)]
pub struct Record {
pub id: i64,
#[tabled(display = "display_option")]
pub valid: Option<bool>
}
fn display_option(o: &Option<bool>) -> String {
match o {
Some(s) => format!("is valid thing = {}", s),
None => format!("is not valid"),
}
}
// Example 2: Using self and arguments
#[derive(Tabled)]
pub struct RecordWithArgs {
pub id: i64,
#[tabled(display("Self::display_valid", self, 1))]
pub valid: Option<bool>
}
impl RecordWithArgs {
fn display_valid(&self, arg: usize) -> String {
match self.valid {
Some(s) => format!("is valid thing = {} {}", s, arg),
None => format!("is not valid {}", arg),
}
}
}
// Example 3: Applying to all fields of a specific type
#[derive(Tabled)]
#[tabled(display(Option, "tabled::derive::display::option", "undefined"))]
pub struct RecordBulk {
pub id: i64,
pub name: Option<String>,
pub birthdate: Option<usize>,
}