leptos_struct_table/
rust_decimal.rs

1//! Support for [::rust_decimal] crate.
2use crate::*;
3use ::rust_decimal::Decimal;
4use leptos::*;
5
6#[derive(Default)]
7pub struct DecimalNumberRenderOptions {
8    /// Specifies the number of digits to display after the decimal point
9    pub precision: Option<usize>,
10}
11/// Implementation for [`Decimal`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
12/// ```
13/// # use leptos_struct_table::*;
14/// # use leptos::*;
15/// # use ::rust_decimal::Decimal;
16/// #[derive(TableRow, Clone)]
17/// #[table]
18/// struct SomeStruct {
19///     #[table(format(precision = 2usize))]
20///     my_field: Decimal
21/// }
22/// ```
23impl CellValue for Decimal {
24    type RenderOptions = DecimalNumberRenderOptions;
25    fn render_value(self, options: &Self::RenderOptions) -> impl IntoView {
26        if let Some(value) = options.precision.as_ref() {
27            format!("{:.value$}", self)
28        } else {
29            self.to_string()
30        }
31    }
32}