leptos_struct_table/
chrono.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Support for [::chrono] crate.

use crate::*;
use ::chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use leptos::prelude::*;

#[derive(Clone, Default)]
pub struct RenderChronoOptions {
    /// Specifies a format string, See [`::chrono::format::strftime`] for more information.
    pub string: Option<String>,
}

macro_rules! chrono_cell_value_impl {
    (
        $(#[$outer:meta])*
        $ty:ty
    ) => {
        $(#[$outer])*
        impl CellValue<$ty> for $ty {
            type RenderOptions = RenderChronoOptions;

            fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
                if let Some(value) = options.string.as_ref() {
                    self.format(&value).to_string()
                } else {
                    self.to_string()
                }
            }
        }
    };
}

chrono_cell_value_impl!(
    /// Implementation for [`NaiveDate`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
    /// ```
    /// # use leptos_struct_table::*;
    /// # use leptos::prelude::*;
    /// # use ::chrono::NaiveDate;
    /// #[derive(TableRow, Clone)]
    /// #[table]
    /// struct SomeStruct {
    ///     #[table(format(string = "%Y-%m-%d"))]
    ///     my_field: NaiveDate
    /// }
    /// ```
    NaiveDate
);

chrono_cell_value_impl!(
    /// Implementation for [`NaiveDateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
    /// ```
    /// # use leptos_struct_table::*;
    /// # use leptos::prelude::*;
    /// # use ::chrono::NaiveDateTime;
    /// #[derive(TableRow, Clone)]
    /// #[table]
    /// struct SomeStruct {
    ///     #[table(format(string = "%Y-%m-%d %H:%M:%S"))]
    ///     my_field: NaiveDateTime
    /// }
    /// ```
    NaiveDateTime
);

chrono_cell_value_impl!(
    /// Implementation for [`NaiveTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
    /// ```
    /// # use leptos_struct_table::*;
    /// # use leptos::prelude::*;
    /// # use ::chrono::NaiveTime;
    /// #[derive(TableRow, Clone)]
    /// #[table]
    /// struct SomeStruct {
    ///     #[table(format(string = "%H:%M:%S"))]
    ///     my_field: NaiveTime
    /// }
    /// ```
    NaiveTime
);