use crate::fields;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use icu_locid::extensions::unicode::key;
use icu_provider::DataLocale;
use tinystr::tinystr;
use tinystr::TinyAsciiStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub struct Bag {
#[cfg_attr(feature = "serde", serde(rename = "hourCycle"))]
pub hour_cycle: Option<HourCycle>,
}
impl Bag {
#[cfg(feature = "experimental")]
pub fn from_hour_cycle(h: HourCycle) -> Self {
Self {
hour_cycle: Some(h),
}
}
pub(crate) fn from_data_locale(data_locale: &DataLocale) -> Self {
const H11: TinyAsciiStr<8> = tinystr!(8, "h11");
const H12: TinyAsciiStr<8> = tinystr!(8, "h12");
const H23: TinyAsciiStr<8> = tinystr!(8, "h23");
const H24: TinyAsciiStr<8> = tinystr!(8, "h24");
let hour_cycle = match data_locale
.get_unicode_ext(&key!("hc"))
.and_then(|v| v.as_single_subtag().copied())
{
Some(H11) => Some(HourCycle::H11),
Some(H12) => Some(HourCycle::H12),
Some(H23) => Some(HourCycle::H23),
Some(H24) => Some(HourCycle::H24),
_ => None,
};
Self { hour_cycle }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[allow(clippy::exhaustive_enums)] pub enum HourCycle {
#[cfg_attr(feature = "serde", serde(rename = "h24"))]
H24,
#[cfg_attr(feature = "serde", serde(rename = "h23"))]
H23,
#[cfg_attr(feature = "serde", serde(rename = "h12"))]
H12,
#[cfg_attr(feature = "serde", serde(rename = "h11"))]
H11,
}
impl HourCycle {
pub fn field(self) -> fields::Hour {
match self {
Self::H11 => fields::Hour::H11,
Self::H12 => fields::Hour::H12,
Self::H23 => fields::Hour::H23,
Self::H24 => fields::Hour::H24,
}
}
}