leptos_struct_table/
chrono.rs

1//! Support for [::chrono] crate.
2
3use crate::*;
4use ::chrono::{NaiveDate, NaiveDateTime, NaiveTime};
5use leptos::*;
6
7#[derive(Default)]
8pub struct RenderChronoOptions {
9    /// Specifies a format string, See [`::chrono::format::strftime`] for more information.
10    pub string: Option<String>,
11}
12
13macro_rules! chrono_cell_value_impl {
14    (
15        $(#[$outer:meta])*
16        $ty:ty
17    ) => {
18        $(#[$outer])*
19        impl CellValue for $ty {
20            type RenderOptions = RenderChronoOptions;
21
22            fn render_value(self, options: &Self::RenderOptions) -> impl IntoView {
23                if let Some(value) = options.string.as_ref() {
24                    self.format(&value).to_string()
25                } else {
26                    self.to_string()
27                }
28            }
29        }
30    };
31}
32
33chrono_cell_value_impl!(
34    /// Implementation for [`NaiveDate`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
35    /// ```
36    /// # use leptos_struct_table::*;
37    /// # use leptos::*;
38    /// # use ::chrono::NaiveDate;
39    /// #[derive(TableRow, Clone)]
40    /// #[table]
41    /// struct SomeStruct {
42    ///     #[table(format(string = "%Y-%m-%d"))]
43    ///     my_field: NaiveDate
44    /// }
45    /// ```
46    NaiveDate
47);
48
49chrono_cell_value_impl!(
50    /// Implementation for [`NaiveDateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
51    /// ```
52    /// # use leptos_struct_table::*;
53    /// # use leptos::*;
54    /// # use ::chrono::NaiveDateTime;
55    /// #[derive(TableRow, Clone)]
56    /// #[table]
57    /// struct SomeStruct {
58    ///     #[table(format(string = "%Y-%m-%d %H:%M:%S"))]
59    ///     my_field: NaiveDateTime
60    /// }
61    /// ```
62    NaiveDateTime
63);
64
65chrono_cell_value_impl!(
66    /// Implementation for [`NaiveTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
67    /// ```
68    /// # use leptos_struct_table::*;
69    /// # use leptos::*;
70    /// # use ::chrono::NaiveTime;
71    /// #[derive(TableRow, Clone)]
72    /// #[table]
73    /// struct SomeStruct {
74    ///     #[table(format(string = "%H:%M:%S"))]
75    ///     my_field: NaiveTime
76    /// }
77    /// ```
78    NaiveTime
79);