use laravel::time::civil::{Date, DateTime};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Date,
DateTime,
}
impl Kind {
pub(crate) const fn storage(self) -> &'static str {
match self {
Self::Date => "Y-m-d",
Self::DateTime => "Y-m-d H:i:s",
}
}
}
pub trait Storage: 'static {
const KIND: Kind;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DateOnly;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DateAndTime;
impl Storage for DateOnly {
const KIND: Kind = Kind::Date;
}
impl Storage for DateAndTime {
const KIND: Kind = Kind::DateTime;
}
pub trait Format: 'static {
const PARSE_SET: bool;
fn get() -> Option<&'static str> {
None
}
fn set() -> Option<&'static str> {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Configured;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Parsed;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MonthYear;
impl Format for Configured {
const PARSE_SET: bool = false;
}
impl Format for Parsed {
const PARSE_SET: bool = true;
}
impl Format for MonthYear {
const PARSE_SET: bool = true;
fn get() -> Option<&'static str> {
Some("m/Y")
}
fn set() -> Option<&'static str> {
Some("m/Y")
}
}
pub(crate) fn midnight(date: Date) -> DateTime {
date.at(0, 0, 0, 0)
}
#[macro_export]
macro_rules! date_format {
($(#[$meta:meta])* $vis:vis $name:ident, $pattern:literal) => {
$crate::date_format!($(#[$meta])* $vis $name, get = $pattern, set = $pattern);
};
($(#[$meta:meta])* $vis:vis $name:ident, get = $get:literal, set = $set:literal) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
$vis struct $name;
impl $crate::Format for $name {
const PARSE_SET: bool = true;
fn get() -> Option<&'static str> {
Some($get)
}
fn set() -> Option<&'static str> {
Some($set)
}
}
};
}