Skip to main content

leptos_struct_table/
jiff.rs

1//! Support for [::jiff] crate.
2
3use crate::*;
4use ::jiff::{
5    Timestamp, Zoned,
6    civil::{Date, DateTime, Time},
7};
8use leptos::prelude::*;
9
10#[derive(Clone, Default)]
11pub struct RenderJiffOptions {
12    /// Specifies a format string, See [`::jiff::fmt::strtime`] for more information.
13    pub string: Option<String>,
14}
15
16macro_rules! jiff_cell_value_impl {
17    (
18        $(#[$outer:meta])*
19        $ty:ty
20    ) => {
21        $(#[$outer])*
22        impl CellValue<$ty> for $ty {
23            type RenderOptions = RenderJiffOptions;
24
25            fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
26                if let Some(value) = options.string.as_ref() {
27                    self.strftime(value).to_string()
28                } else {
29                    self.to_string()
30                }
31            }
32        }
33    };
34}
35
36jiff_cell_value_impl!(
37    /// Implementation for [`Date`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
38    /// ```
39    /// # use leptos_struct_table::*;
40    /// # use leptos::prelude::*;
41    /// # use ::jiff::civil::Date;
42    /// #[derive(TableRow, Clone)]
43    /// #[table]
44    /// struct SomeStruct {
45    ///     #[table(format(string = "%Y-%m-%d"))]
46    ///     my_field: Date
47    /// }
48    /// ```
49    Date
50);
51
52jiff_cell_value_impl!(
53    /// Implementation for [`Time`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
54    /// ```
55    /// # use leptos_struct_table::*;
56    /// # use leptos::prelude::*;
57    /// # use ::jiff::civil::Time;
58    /// #[derive(TableRow, Clone)]
59    /// #[table]
60    /// struct SomeStruct {
61    ///     #[table(format(string = "%H:%M:%S"))]
62    ///     my_field: Time
63    /// }
64    /// ```
65    Time
66);
67
68jiff_cell_value_impl!(
69    /// Implementation for [`DateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
70    /// ```
71    /// # use leptos_struct_table::*;
72    /// # use leptos::prelude::*;
73    /// # use ::jiff::civil::DateTime;
74    /// #[derive(TableRow, Clone)]
75    /// #[table]
76    /// struct SomeStruct {
77    ///     #[table(format(string = "%Y-%m-%d %H:%M:%S"))]
78    ///     my_field: DateTime
79    /// }
80    /// ```
81    DateTime
82);
83
84jiff_cell_value_impl!(
85    /// Implementation for [`Timestamp`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
86    /// ```
87    /// # use leptos_struct_table::*;
88    /// # use leptos::prelude::*;
89    /// # use ::jiff::Timestamp;
90    /// #[derive(TableRow, Clone)]
91    /// #[table]
92    /// struct SomeStruct {
93    ///     #[table(format(string = "%Y-%m-%dT%H:%M:%S%z"))]
94    ///     my_field: Timestamp
95    /// }
96    /// ```
97    Timestamp
98);
99
100jiff_cell_value_impl!(
101    /// Implementation for [`Zoned`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
102    /// ```
103    /// # use leptos_struct_table::*;
104    /// # use leptos::prelude::*;
105    /// # use ::jiff::Zoned;
106    /// #[derive(TableRow, Clone)]
107    /// #[table]
108    /// struct SomeStruct {
109    ///     #[table(format(string = "%Y-%m-%d %H:%M:%S %:Q"))]
110    ///     my_field: Zoned
111    /// }
112    /// ```
113    Zoned
114);