leptos_struct_table/
time.rs

1//! Support for [::time] crate.
2
3use crate::*;
4use ::time::format_description;
5use ::time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
6use leptos::*;
7
8#[derive(Default)]
9pub struct RenderTimeOptions {
10    /// Specifies a format string see [the time book](https://time-rs.github.io/book/api/format-description.html).
11    pub string: Option<String>,
12}
13
14/// Implementation for [`Date`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
15/// ```
16/// # use leptos_struct_table::*;
17/// # use leptos::*;
18/// # use ::time::Date;
19/// #[derive(TableRow, Clone)]
20/// #[table]
21/// struct SomeStruct {
22///     #[table(format(string = "[year]-[month]-[day]"))]
23///     my_field: Date
24/// }
25/// ```
26impl CellValue for Date {
27    type RenderOptions = RenderTimeOptions;
28
29    fn render_value(self, options: &Self::RenderOptions) -> impl IntoView {
30        if let Some(value) = options.string.as_ref() {
31            let format = format_description::parse(value)
32                .expect("Unable to construct a format description given the format string");
33            self.format(&format)
34                .expect("Unable to format given the format description")
35        } else {
36            self.to_string()
37        }
38    }
39}
40/// Implementation for [`Time`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
41/// ```
42/// # use leptos_struct_table::*;
43/// # use leptos::*;
44/// # use ::time::Time;
45/// #[derive(TableRow, Clone)]
46/// #[table]
47/// struct SomeStruct {
48///     #[table(format(string = "[hour]:[minute]:[second]"))]
49///     my_field: Time
50/// }
51/// ```
52impl CellValue for Time {
53    type RenderOptions = RenderTimeOptions;
54
55    fn render_value(self, options: &Self::RenderOptions) -> impl IntoView {
56        if let Some(value) = options.string.as_ref() {
57            let format = format_description::parse(value)
58                .expect("Unable to construct a format description given the format string");
59            self.format(&format)
60                .expect("Unable to format given the format description")
61        } else {
62            self.to_string()
63        }
64    }
65}
66
67/// Implementation for [`PrimitiveDateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
68/// ```
69/// # use leptos_struct_table::*;
70/// # use leptos::*;
71/// # use ::time::PrimitiveDateTime;
72/// #[derive(TableRow, Clone)]
73/// #[table]
74/// struct SomeStruct {
75///     #[table(format(string = "[year]-[month]-[day] [hour]:[minute]:[second]"))]
76///     my_field: PrimitiveDateTime
77/// }
78/// ```
79impl CellValue for PrimitiveDateTime {
80    type RenderOptions = RenderTimeOptions;
81
82    fn render_value(self, options: &Self::RenderOptions) -> impl IntoView {
83        if let Some(value) = options.string.as_ref() {
84            let format = format_description::parse(value)
85                .expect("Unable to construct a format description given the format string");
86            self.format(&format)
87                .expect("Unable to format given the format description")
88        } else {
89            self.to_string()
90        }
91    }
92}
93
94/// Implementation for [`OffsetDateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
95/// ```
96/// # use leptos_struct_table::*;
97/// # use leptos::*;
98/// # use ::time::OffsetDateTime;
99/// #[derive(TableRow, Clone)]
100/// #[table]
101/// struct SomeStruct {
102///     #[table(format(string = "[year]-[month]-[day] [hour]:[minute]:[second] Z[offset_hour]"))]
103///     my_field: OffsetDateTime
104/// }
105/// ```
106impl CellValue for OffsetDateTime {
107    type RenderOptions = RenderTimeOptions;
108
109    fn render_value(self, options: &Self::RenderOptions) -> impl IntoView {
110        if let Some(value) = options.string.as_ref() {
111            let format = format_description::parse(value)
112                .expect("Unable to construct a format description given the format string");
113            self.format(&format)
114                .expect("Unable to format given the format description")
115        } else {
116            self.to_string()
117        }
118    }
119}