1use laravel::time::civil::{Date, DateTime};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Kind {
8 Date,
10 DateTime,
12}
13
14impl Kind {
15 pub(crate) const fn storage(self) -> &'static str {
16 match self {
17 Self::Date => "Y-m-d",
18 Self::DateTime => "Y-m-d H:i:s",
19 }
20 }
21}
22
23pub trait Storage: 'static {
25 const KIND: Kind;
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
31pub struct DateOnly;
32#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34pub struct DateAndTime;
35
36impl Storage for DateOnly {
37 const KIND: Kind = Kind::Date;
38}
39impl Storage for DateAndTime {
40 const KIND: Kind = Kind::DateTime;
41}
42
43pub trait Format: 'static {
46 const PARSE_SET: bool;
48 fn get() -> Option<&'static str> {
50 None
51 }
52 fn set() -> Option<&'static str> {
54 None
55 }
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
60pub struct Configured;
61#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub struct Parsed;
64#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
66pub struct MonthYear;
67
68impl Format for Configured {
69 const PARSE_SET: bool = false;
70}
71impl Format for Parsed {
72 const PARSE_SET: bool = true;
73}
74impl Format for MonthYear {
75 const PARSE_SET: bool = true;
76 fn get() -> Option<&'static str> {
77 Some("m/Y")
78 }
79 fn set() -> Option<&'static str> {
80 Some("m/Y")
81 }
82}
83
84pub(crate) fn midnight(date: Date) -> DateTime {
85 date.at(0, 0, 0, 0)
86}
87
88#[macro_export]
99macro_rules! date_format {
100 ($(#[$meta:meta])* $vis:vis $name:ident, $pattern:literal) => {
101 $crate::date_format!($(#[$meta])* $vis $name, get = $pattern, set = $pattern);
102 };
103 ($(#[$meta:meta])* $vis:vis $name:ident, get = $get:literal, set = $set:literal) => {
104 $(#[$meta])*
105 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
106 $vis struct $name;
107
108 impl $crate::Format for $name {
109 const PARSE_SET: bool = true;
110 fn get() -> Option<&'static str> {
111 Some($get)
112 }
113 fn set() -> Option<&'static str> {
114 Some($set)
115 }
116 }
117 };
118}