leptos_struct_table/
time.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Support for [::time] crate.

use crate::*;
use ::time::format_description;
use ::time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
use leptos::prelude::*;

#[derive(Clone, Default)]
pub struct RenderTimeOptions {
    /// Specifies a format string see [the time book](https://time-rs.github.io/book/api/format-description.html).
    pub string: Option<String>,
}

/// Implementation for [`Date`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
/// ```
/// # use leptos_struct_table::*;
/// # use leptos::prelude::*;
/// # use ::time::Date;
/// #[derive(TableRow, Clone)]
/// #[table]
/// struct SomeStruct {
///     #[table(format(string = "[year]-[month]-[day]"))]
///     my_field: Date
/// }
/// ```
impl CellValue<Date> for Date {
    type RenderOptions = RenderTimeOptions;

    fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
        if let Some(value) = options.string.as_ref() {
            let format = format_description::parse(value)
                .expect("Unable to construct a format description given the format string");
            self.format(&format)
                .expect("Unable to format given the format description")
        } else {
            self.to_string()
        }
    }
}
/// Implementation for [`Time`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
/// ```
/// # use leptos_struct_table::*;
/// # use leptos::prelude::*;
/// # use ::time::Time;
/// #[derive(TableRow, Clone)]
/// #[table]
/// struct SomeStruct {
///     #[table(format(string = "[hour]:[minute]:[second]"))]
///     my_field: Time
/// }
/// ```
impl CellValue<Time> for Time {
    type RenderOptions = RenderTimeOptions;

    fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
        if let Some(value) = options.string.as_ref() {
            let format = format_description::parse(value)
                .expect("Unable to construct a format description given the format string");
            self.format(&format)
                .expect("Unable to format given the format description")
        } else {
            self.to_string()
        }
    }
}

/// Implementation for [`PrimitiveDateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
/// ```
/// # use leptos_struct_table::*;
/// # use leptos::prelude::*;
/// # use ::time::PrimitiveDateTime;
/// #[derive(TableRow, Clone)]
/// #[table]
/// struct SomeStruct {
///     #[table(format(string = "[year]-[month]-[day] [hour]:[minute]:[second]"))]
///     my_field: PrimitiveDateTime
/// }
/// ```
impl CellValue<PrimitiveDateTime> for PrimitiveDateTime {
    type RenderOptions = RenderTimeOptions;

    fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
        if let Some(value) = options.string.as_ref() {
            let format = format_description::parse(value)
                .expect("Unable to construct a format description given the format string");
            self.format(&format)
                .expect("Unable to format given the format description")
        } else {
            self.to_string()
        }
    }
}

/// Implementation for [`OffsetDateTime`] to work with the [`TableRow`] derive and the [`DefaultTableCellRenderer`]
/// ```
/// # use leptos_struct_table::*;
/// # use leptos::prelude::*;
/// # use ::time::OffsetDateTime;
/// #[derive(TableRow, Clone)]
/// #[table]
/// struct SomeStruct {
///     #[table(format(string = "[year]-[month]-[day] [hour]:[minute]:[second] Z[offset_hour]"))]
///     my_field: OffsetDateTime
/// }
/// ```
impl CellValue<OffsetDateTime> for OffsetDateTime {
    type RenderOptions = RenderTimeOptions;

    fn render_value(self, options: Self::RenderOptions) -> impl IntoView {
        if let Some(value) = options.string.as_ref() {
            let format = format_description::parse(value)
                .expect("Unable to construct a format description given the format string");
            self.format(&format)
                .expect("Unable to format given the format description")
        } else {
            self.to_string()
        }
    }
}