use std::borrow::Cow;
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct DatesV1 {
pub symbols: DateSymbolsV1,
pub patterns: PatternsV1,
}
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct DateSymbolsV1 {
pub months: months::ContextsV1,
pub weekdays: weekdays::ContextsV1,
pub day_periods: day_periods::ContextsV1,
}
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct PatternsV1 {
pub date: patterns::LengthPatternsV1,
pub time: patterns::LengthPatternsV1,
pub datetime: patterns::DateTimeFormatsV1,
}
macro_rules! symbols {
($name: ident, $expr: ty) => {
pub mod $name {
use super::*;
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature="provider_serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SymbolsV1(pub $expr);
symbols!();
}
};
($name: ident { $($tokens: tt)* }) => {
symbols!($name { $($tokens)* } -> ());
};
($name: ident { $element: ident: Option<$ty: ty>, $($tokens: tt)+ } -> ($($members:tt)*)) => {
symbols!($name { $($tokens)* } -> (
$($members)*
pub $element: Option<$ty>,
));
};
($name: ident { $element: ident: $ty: ty, $($tokens: tt)+ } -> ($($members:tt)*)) => {
symbols!($name { $($tokens)* } -> (
$($members)*
pub $element: $ty,
));
};
($name: ident { $element: ident: Option<$ty: ty> $(,)? } -> ($($members:tt)*)) => {
symbols!($name { } -> (
$($members)*
pub $element: Option<$ty>,
));
};
($name: ident { $element: ident: $ty: ty $(,)? } -> ($($members:tt)*)) => {
symbols!($name { } -> (
$($members)*
pub $element: $ty,
));
};
($name: ident { } -> ($($members: tt)*)) => {
pub mod $name {
use super::*;
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature="provider_serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SymbolsV1 {
$($members)*
}
symbols!();
}
};
() => {
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature="provider_serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FormatWidthsV1 {
pub abbreviated: SymbolsV1,
pub narrow: SymbolsV1,
pub short: Option<SymbolsV1>,
pub wide: SymbolsV1,
}
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature="provider_serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StandAloneWidthsV1 {
pub abbreviated: Option<SymbolsV1>,
pub narrow: Option<SymbolsV1>,
pub short: Option<SymbolsV1>,
pub wide: Option<SymbolsV1>,
}
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(feature="provider_serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ContextsV1 {
pub format: FormatWidthsV1,
pub stand_alone: Option<StandAloneWidthsV1>,
}
};
}
symbols!(months, [Cow<'static, str>; 12]);
symbols!(weekdays, [Cow<'static, str>; 7]);
symbols!(
day_periods {
am: Cow<'static, str>,
pm: Cow<'static, str>,
noon: Option<Cow<'static, str>>,
midnight: Option<Cow<'static, str>>,
}
);
pub mod patterns {
use super::*;
use crate::{
pattern::{self, Pattern},
skeleton::{Skeleton, SkeletonError},
};
use litemap::LiteMap;
use std::convert::TryFrom;
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct LengthPatternsV1 {
pub full: Cow<'static, str>,
pub long: Cow<'static, str>,
pub medium: Cow<'static, str>,
pub short: Cow<'static, str>,
}
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct PatternV1(pub Pattern);
impl From<Pattern> for PatternV1 {
fn from(pattern: Pattern) -> Self {
Self(pattern)
}
}
impl TryFrom<&str> for PatternV1 {
type Error = pattern::Error;
fn try_from(pattern_string: &str) -> Result<Self, Self::Error> {
let pattern = Pattern::from_bytes(pattern_string);
match pattern {
Ok(pattern) => Ok(Self::from(pattern)),
Err(err) => Err(err),
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct SkeletonV1(pub Skeleton);
impl TryFrom<&str> for SkeletonV1 {
type Error = SkeletonError;
fn try_from(skeleton_string: &str) -> Result<Self, Self::Error> {
match Skeleton::try_from(skeleton_string) {
Ok(skeleton) => Ok(Self(skeleton)),
Err(err) => Err(err),
}
}
}
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct SkeletonsV1(pub LiteMap<SkeletonV1, PatternV1>);
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct DateTimeFormatsV1 {
pub length_patterns: LengthPatternsV1,
pub skeletons: SkeletonsV1,
}
}