leptos_struct_table/
rust_decimal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! Support for [::rust_decimal] crate.
use crate::*;
use ::rust_decimal::Decimal;
use leptos::prelude::*;

#[derive(Clone, Default)]
pub struct DecimalNumberRenderOptions {
    /// Specifies the number of digits to display after the decimal point
    pub precision: Option<usize>,
}
/// Implementation for [`Decimal`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
/// ```
/// # use leptos_struct_table::*;
/// # use leptos::prelude::*;
/// # use ::rust_decimal::Decimal;
/// #[derive(TableRow, Clone)]
/// #[table]
/// struct SomeStruct {
///     #[table(format(precision = 2usize))]
///     my_field: Decimal
/// }
/// ```
impl CellValue<Decimal> for Decimal {
    type RenderOptions = DecimalNumberRenderOptions;
    fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
        if let Some(value) = options.precision.as_ref() {
            format!("{:.value$}", self)
        } else {
            self.to_string()
        }
    }
}