use std::collections::HashMap;
use crate::{temporal_field::TemporalField, text_style::TextStyle};
#[derive(Debug)]
pub struct DateTimeTextProvider {}
impl DateTimeTextProvider {
pub fn new() -> Self {
DateTimeTextProvider {}
}
pub fn get_text(
&self,
field: TemporalField,
value: i64,
style: TextStyle,
) -> Option<&'static str> {
self.create_store(field)
.and_then(|store| store.get_text(value, style))
}
pub fn get_text_iterator(
&self,
field: TemporalField,
style: Option<TextStyle>,
) -> Option<Vec<(&'static str, i64)>> {
self.create_store(field)
.and_then(|store| store.get_text_iterator(style))
}
fn create_store(&self, field: TemporalField) -> Option<Store> {
match field {
TemporalField::Era => Some(Store::new(HashMap::from([
(
TextStyle::Full,
HashMap::from([(0, "Before Christ"), (1, "Anno Domini")]),
),
(TextStyle::Short, HashMap::from([(0, "BC"), (1, "AD")])),
(TextStyle::Narrow, HashMap::from([(0, "B"), (1, "A")])),
]))),
TemporalField::MonthOfYear => Some(Store::new(HashMap::from([
(
TextStyle::Full,
HashMap::from([
(1, "January"),
(2, "February"),
(3, "March"),
(4, "April"),
(5, "May"),
(6, "June"),
(7, "July"),
(8, "August"),
(9, "September"),
(10, "October"),
(11, "November"),
(12, "December"),
]),
),
(
TextStyle::FullStandalone,
HashMap::from([
(1, "January"),
(2, "February"),
(3, "March"),
(4, "April"),
(5, "May"),
(6, "June"),
(7, "July"),
(8, "August"),
(9, "September"),
(10, "October"),
(11, "November"),
(12, "December"),
]),
),
(
TextStyle::Short,
HashMap::from([
(1, "Jan"),
(2, "Feb"),
(3, "Mar"),
(4, "Apr"),
(5, "May"),
(6, "Jun"),
(7, "Jul"),
(8, "Aug"),
(9, "Sep"),
(10, "Oct"),
(11, "Nov"),
(12, "Dec"),
]),
),
(
TextStyle::ShortStandalone,
HashMap::from([
(1, "Jan"),
(2, "Feb"),
(3, "Mar"),
(4, "Apr"),
(5, "May"),
(6, "Jun"),
(7, "Jul"),
(8, "Aug"),
(9, "Sep"),
(10, "Oct"),
(11, "Nov"),
(12, "Dec"),
]),
),
(
TextStyle::Narrow,
HashMap::from([
(1, "J"),
(2, "F"),
(3, "M"),
(4, "A"),
(5, "M"),
(6, "J"),
(7, "J"),
(8, "A"),
(9, "S"),
(10, "O"),
(11, "N"),
(12, "D"),
]),
),
(
TextStyle::NarrowStandalone,
HashMap::from([
(1, "J"),
(2, "F"),
(3, "M"),
(4, "A"),
(5, "M"),
(6, "J"),
(7, "J"),
(8, "A"),
(9, "S"),
(10, "O"),
(11, "N"),
(12, "D"),
]),
),
]))),
TemporalField::DayOfWeek => Some(Store::new(HashMap::from([
(
TextStyle::Full,
HashMap::from([
(1, "Monday"),
(2, "Tuesday"),
(3, "Wednesday"),
(4, "Thursday"),
(5, "Friday"),
(6, "Saturday"),
(7, "Sunday"),
]),
),
(
TextStyle::FullStandalone,
HashMap::from([
(1, "Monday"),
(2, "Tuesday"),
(3, "Wednesday"),
(4, "Thursday"),
(5, "Friday"),
(6, "Saturday"),
(7, "Sunday"),
]),
),
(
TextStyle::Short,
HashMap::from([
(1, "Mon"),
(2, "Tue"),
(3, "Wed"),
(4, "Thu"),
(5, "Fri"),
(6, "Sat"),
(7, "Sun"),
]),
),
(
TextStyle::ShortStandalone,
HashMap::from([
(1, "Mon"),
(2, "Tue"),
(3, "Wed"),
(4, "Thu"),
(5, "Fri"),
(6, "Sat"),
(7, "Sun"),
]),
),
(
TextStyle::Narrow,
HashMap::from([
(1, "M"),
(2, "T"),
(3, "W"),
(4, "T"),
(5, "F"),
(6, "S"),
(7, "S"),
]),
),
(
TextStyle::NarrowStandalone,
HashMap::from([
(1, "M"),
(2, "T"),
(3, "W"),
(4, "T"),
(5, "F"),
(6, "S"),
(7, "S"),
]),
),
]))),
TemporalField::LocaleDayOfWeek => Some(Store::new(HashMap::from([
(
TextStyle::Full,
HashMap::from([
(1, "Sunday"),
(2, "Monday"),
(3, "Tuesday"),
(4, "Wednesday"),
(5, "Thursday"),
(6, "Friday"),
(7, "Saturday"),
]),
),
(
TextStyle::FullStandalone,
HashMap::from([
(1, "Sunday"),
(2, "Monday"),
(3, "Tuesday"),
(4, "Wednesday"),
(5, "Thursday"),
(6, "Friday"),
(7, "Saturday"),
]),
),
(
TextStyle::Short,
HashMap::from([
(1, "Sun"),
(2, "Mon"),
(3, "Tue"),
(4, "Wed"),
(5, "Thu"),
(6, "Fri"),
(7, "Sat"),
]),
),
(
TextStyle::ShortStandalone,
HashMap::from([
(1, "Sun"),
(2, "Mon"),
(3, "Tue"),
(4, "Wed"),
(5, "Thu"),
(6, "Fri"),
(7, "Sat"),
]),
),
(
TextStyle::Narrow,
HashMap::from([
(1, "S"),
(2, "M"),
(3, "T"),
(4, "W"),
(5, "T"),
(6, "F"),
(7, "S"),
]),
),
(
TextStyle::NarrowStandalone,
HashMap::from([
(1, "S"),
(2, "M"),
(3, "T"),
(4, "W"),
(5, "T"),
(6, "F"),
(7, "S"),
]),
),
]))),
TemporalField::AmPmOfDay => Some(Store::new(HashMap::from([
(TextStyle::Full, HashMap::from([(0, "AM"), (1, "PM")])),
(TextStyle::Short, HashMap::from([(0, "AM"), (1, "PM")])),
(TextStyle::Narrow, HashMap::from([(0, "a"), (1, "b")])),
]))),
TemporalField::QuarterOfYear => Some(Store::new(HashMap::from([
(
TextStyle::Full,
HashMap::from([
(1, "1st quarter"),
(2, "2nd quarter"),
(3, "3rd quarter"),
(4, "4th quarter"),
]),
),
(
TextStyle::FullStandalone,
HashMap::from([
(1, "1st quarter"),
(2, "2nd quarter"),
(3, "3rd quarter"),
(4, "4th quarter"),
]),
),
(
TextStyle::Short,
HashMap::from([(1, "Q1"), (2, "Q2"), (3, "Q3"), (4, "Q4")]),
),
(
TextStyle::ShortStandalone,
HashMap::from([(1, "Q1"), (2, "Q2"), (3, "Q3"), (4, "Q4")]),
),
(
TextStyle::Narrow,
HashMap::from([(1, "1"), (2, "2"), (3, "3"), (4, "4")]),
),
(
TextStyle::NarrowStandalone,
HashMap::from([(1, "1"), (2, "2"), (3, "3"), (4, "4")]),
),
]))),
_ => None,
}
}
}
#[derive(Debug)]
pub struct Store {
value_text_map: HashMap<TextStyle, HashMap<i64, &'static str>>,
parsable: HashMap<Option<TextStyle>, Vec<(&'static str, i64)>>,
}
impl Store {
pub fn new(value_text_map: HashMap<TextStyle, HashMap<i64, &'static str>>) -> Self {
let mut map: HashMap<Option<TextStyle>, Vec<(&'static str, i64)>> = HashMap::new();
let mut all_list: Vec<(&'static str, i64)> = Vec::new();
for (&vtm_key, vtm_value) in value_text_map.iter() {
let mut reverse: HashMap<&'static str, (&'static str, i64)> = HashMap::new();
for (entry_key, &entry_value) in vtm_value.iter() {
reverse
.entry(entry_value)
.and_modify(|existing| {
if entry_key < &existing.1 {
*existing = (entry_value, *entry_key);
}
})
.or_insert((entry_value, *entry_key));
}
let mut list: Vec<(&'static str, i64)> = reverse.into_values().collect();
list.sort_by(|a, b| b.0.len().cmp(&a.0.len()));
all_list.append(&mut list.clone());
map.insert(Some(vtm_key), list);
}
all_list.sort_by(|a, b| b.0.len().cmp(&a.0.len()));
map.insert(None, all_list);
Store {
value_text_map,
parsable: map,
}
}
pub fn get_text(&self, value: i64, style: TextStyle) -> Option<&'static str> {
self.value_text_map
.get(&style)
.and_then(|map| map.get(&value).cloned())
}
pub fn get_text_iterator(&self, style: Option<TextStyle>) -> Option<Vec<(&'static str, i64)>> {
self.parsable.get(&style).cloned()
}
}