Skip to main content

whichtime_sys/dictionaries/
mod.rs

1//! Option B: Compile-time PHF dictionaries
2//!
3//! This module provides locale-specific dictionaries using compile-time perfect hash functions.
4//! Benefits:
5//! - O(1) guaranteed lookup with no runtime hashing
6//! - Zero heap allocations
7//! - Maps stored in read-only memory (.rodata section)
8
9pub mod de;
10pub mod en;
11pub mod es;
12pub mod fr;
13pub mod it;
14pub mod ja;
15pub mod nl;
16pub mod pt;
17pub mod ru;
18pub mod sv;
19pub mod uk;
20pub mod zh;
21
22use std::str::FromStr;
23
24// Re-export common types used across locales
25pub use crate::types::{TimeUnit, Weekday};
26
27/// Locale-independent categories of casual date expressions.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum CasualDateType {
30    /// Immediate current instant.
31    Now,
32    /// Current day.
33    Today,
34    /// Current night.
35    Tonight,
36    /// Following day.
37    Tomorrow,
38    /// Previous day.
39    Yesterday,
40    /// Day after tomorrow.
41    Overmorrow,
42    /// Day before yesterday.
43    DayBeforeYesterday,
44    /// Morning of the current day.
45    ThisMorning,
46    /// Afternoon of the current day.
47    ThisAfternoon,
48    /// Evening of the current day.
49    ThisEvening,
50}
51
52/// Locale-independent categories of casual time expressions.
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub enum CasualTimeType {
55    /// Around 12:00 PM.
56    Noon,
57    /// Around 12:00 AM.
58    Midnight,
59    /// Morning period.
60    Morning,
61    /// Afternoon period.
62    Afternoon,
63    /// Evening period.
64    Evening,
65    /// Night period.
66    Night,
67}
68
69/// Relative modifiers used with weekdays and time units.
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum RelativeModifier {
72    /// Current period.
73    This,
74    /// Following period.
75    Next,
76    /// Previous period.
77    Last,
78}
79
80/// Supported parser locales.
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82pub enum Locale {
83    /// English.
84    En,
85    /// German.
86    De,
87    /// Spanish.
88    Es,
89    /// French.
90    Fr,
91    /// Italian.
92    It,
93    /// Japanese.
94    Ja,
95    /// Dutch.
96    Nl,
97    /// Portuguese.
98    Pt,
99    /// Russian.
100    Ru,
101    /// Swedish.
102    Sv,
103    /// Ukrainian.
104    Uk,
105    /// Chinese.
106    Zh,
107}
108
109impl FromStr for Locale {
110    type Err = crate::Error;
111    /// Get locale from string code
112    fn from_str(s: &str) -> Result<Self, Self::Err> {
113        match s.to_lowercase().as_str() {
114            "en" | "english" => Ok(Locale::En),
115            "de" | "german" | "deutsch" => Ok(Locale::De),
116            "es" | "spanish" | "español" => Ok(Locale::Es),
117            "fr" | "french" | "français" => Ok(Locale::Fr),
118            "it" | "italian" | "italiano" => Ok(Locale::It),
119            "ja" | "japanese" | "日本語" => Ok(Locale::Ja),
120            "nl" | "dutch" | "nederlands" => Ok(Locale::Nl),
121            "pt" | "portuguese" | "português" => Ok(Locale::Pt),
122            "ru" | "russian" | "русский" => Ok(Locale::Ru),
123            "sv" | "swedish" | "svenska" => Ok(Locale::Sv),
124            "uk" | "ukrainian" | "українська" => Ok(Locale::Uk),
125            "zh" | "chinese" | "中文" => Ok(Locale::Zh),
126            _ => Err(Self::Err::LocaleNotFound(s.to_string())),
127        }
128    }
129}