use crate::{Error, Result};
use std::{fmt, str::FromStr};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Token {
Literal(char),
Escaped(char),
Day2,
DayShort,
Day,
DayName,
IsoWeekday,
OrdinalSuffix,
Weekday,
DayOfYear,
IsoWeek,
MonthName,
Month2,
MonthShort,
Month,
DaysInMonth,
LeapYear,
IsoYear,
ExpandedYear,
ExpandedYearSigned,
Year4,
Year2,
MeridiemLower,
MeridiemUpper,
Swatch,
Hour12,
Hour24,
Hour12Padded,
Hour24Padded,
Minute,
Second,
Micro,
Milli,
ZoneName,
Dst,
OffsetCompact,
OffsetColon,
OffsetColonZ,
ZoneAbbreviation,
OffsetSeconds,
Iso8601,
Rfc2822,
Timestamp,
}
impl Token {
pub(crate) const fn only_formats(self) -> bool {
matches!(
self,
Self::IsoWeekday
| Self::Weekday
| Self::IsoWeek
| Self::DaysInMonth
| Self::LeapYear
| Self::IsoYear
| Self::Swatch
| Self::Dst
| Self::OffsetSeconds
| Self::Iso8601
| Self::Rfc2822
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PhpDatePattern {
pub(crate) tokens: Vec<Token>,
source: Box<str>,
}
impl fmt::Display for PhpDatePattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&self.source)
}
}
impl FromStr for PhpDatePattern {
type Err = Error;
fn from_str(pattern: &str) -> Result<Self> {
Self::compile(pattern)
}
}
impl PhpDatePattern {
pub fn compile(pattern: &str) -> Result<Self> {
Self::build(pattern, false)
}
pub fn compile_strict(pattern: &str) -> Result<Self> {
Self::build(pattern, true)
}
#[must_use]
pub const fn as_str(&self) -> &str {
&self.source
}
#[must_use]
pub fn is_parseable(&self) -> bool {
!self.tokens.iter().any(|token| token.only_formats())
}
fn build(pattern: &str, strict: bool) -> Result<Self> {
let mut tokens = Vec::with_capacity(pattern.len());
let mut chars = pattern.chars();
while let Some(character) = chars.next() {
tokens.push(match character {
'\\' => Token::Escaped(
chars.next().ok_or_else(|| Error::argument("Date format ends with an incomplete escape."))?,
),
'd' => Token::Day2,
'D' => Token::DayShort,
'j' => Token::Day,
'l' => Token::DayName,
'N' => Token::IsoWeekday,
'S' => Token::OrdinalSuffix,
'w' => Token::Weekday,
'z' => Token::DayOfYear,
'W' => Token::IsoWeek,
'F' => Token::MonthName,
'm' => Token::Month2,
'M' => Token::MonthShort,
'n' => Token::Month,
't' => Token::DaysInMonth,
'L' => Token::LeapYear,
'o' => Token::IsoYear,
'X' => Token::ExpandedYear,
'x' => Token::ExpandedYearSigned,
'Y' => Token::Year4,
'y' => Token::Year2,
'a' => Token::MeridiemLower,
'A' => Token::MeridiemUpper,
'B' => Token::Swatch,
'g' => Token::Hour12,
'G' => Token::Hour24,
'h' => Token::Hour12Padded,
'H' => Token::Hour24Padded,
'i' => Token::Minute,
's' => Token::Second,
'u' => Token::Micro,
'v' => Token::Milli,
'e' => Token::ZoneName,
'I' => Token::Dst,
'O' => Token::OffsetCompact,
'P' => Token::OffsetColon,
'p' => Token::OffsetColonZ,
'T' => Token::ZoneAbbreviation,
'Z' => Token::OffsetSeconds,
'c' => Token::Iso8601,
'r' => Token::Rfc2822,
'U' => Token::Timestamp,
other if strict && other.is_ascii_alphabetic() => {
return Err(Error::argument(format!("PHP date token \"{other}\" is not supported.")));
}
other => Token::Literal(other),
});
}
Ok(Self { tokens, source: pattern.into() })
}
}