Skip to content

Commit

Permalink
Handle text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Aug 31, 2020
1 parent 1b1a33e commit df29aa7
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 59 deletions.
14 changes: 7 additions & 7 deletions components/datetime/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Year {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b'y' => Self::Calendar,
b'Y' => Self::WeekOf,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Month {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b'M' => Self::Format,
b'L' => Self::StandAlone,
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Day {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b'd' => Self::DayOfMonth,
b'D' => Self::DayOfYear,
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Hour {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b'K' => Self::H11,
b'h' => Self::H12,
Expand Down Expand Up @@ -249,7 +249,7 @@ impl Second {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b's' => Self::Second,
b'S' => Self::FractionalSecond,
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Weekday {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b'E' => Self::Format,
b'e' => Self::Local,
Expand Down Expand Up @@ -315,7 +315,7 @@ impl Period {
})
}

pub fn from_byte(b: u8) -> Result<Self, Error> {
pub const fn from_byte(b: u8) -> Result<Self, Error> {
let r = match b {
b'a' => Self::AmPm,
b'b' => Self::NoonMidnight,
Expand Down
20 changes: 18 additions & 2 deletions components/datetime/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,25 @@ impl<'p> FromIterator<PatternItem<'p>> for Pattern<'p> {
}

pub fn required_data(
_pattern: &Pattern,
pattern: &Pattern,
) -> (Option<fields::FieldLength>, Option<fields::FieldLength>) {
(None, None)
let mut result = (None, None);

for item in pattern.0.iter() {
if let PatternItem::Field(field) = item {
if let FieldSymbol::Month(..) = field.symbol {
if field.length as u8 > 2 {
result.0 = Some(field.length);
}
}
if let FieldSymbol::Weekday(..) = field.symbol {
if field.length as u8 > 2 {
result.0 = Some(field.length);
}
}
}
}
result
}

pub fn get_pattern<'l, 'p>(
Expand Down
157 changes: 111 additions & 46 deletions components/datetime/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,104 @@ impl<'d> WeekdayNames<'d> {
}
}

const PATTERNS: &[(Skeleton, Pattern)] = &[(
Skeleton(Cow::Borrowed(&[
Field {
symbol: FieldSymbol::Year(fields::Year::Calendar),
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Month(fields::Month::Format),
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Day(fields::Day::DayOfMonth),
length: FieldLength::Short,
},
])),
Pattern(Cow::Borrowed(&[
PatternItem::Field(Field {
symbol: FieldSymbol::Day(fields::Day::DayOfMonth),
length: FieldLength::TwoDigit,
}),
PatternItem::Literal(Cow::Borrowed("/")),
PatternItem::Field(Field {
symbol: FieldSymbol::Month(fields::Month::Format),
length: FieldLength::TwoDigit,
}),
PatternItem::Literal(Cow::Borrowed("/")),
PatternItem::Field(Field {
symbol: FieldSymbol::Year(fields::Year::Calendar),
length: FieldLength::Short,
}),
])),
)];
const PATTERNS: &[(Skeleton, Pattern)] = &[
(
Skeleton(Cow::Borrowed(&[
Field {
symbol: FieldSymbol::Year(fields::Year::Calendar),
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Month(fields::Month::Format),
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Day(fields::Day::DayOfMonth),
length: FieldLength::Short,
},
])),
Pattern(Cow::Borrowed(&[
PatternItem::Field(Field {
symbol: FieldSymbol::Day(fields::Day::DayOfMonth),
length: FieldLength::TwoDigit,
}),
PatternItem::Literal(Cow::Borrowed("/")),
PatternItem::Field(Field {
symbol: FieldSymbol::Month(fields::Month::Format),
length: FieldLength::TwoDigit,
}),
PatternItem::Literal(Cow::Borrowed("/")),
PatternItem::Field(Field {
symbol: FieldSymbol::Year(fields::Year::Calendar),
length: FieldLength::Short,
}),
])),
),
(
Skeleton(Cow::Borrowed(&[
Field {
symbol: FieldSymbol::Year(fields::Year::Calendar),
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Month(fields::Month::Format),
length: FieldLength::Abbreviated,
},
Field {
symbol: FieldSymbol::Day(fields::Day::DayOfMonth),
length: FieldLength::Short,
},
])),
Pattern(Cow::Borrowed(&[
PatternItem::Field(Field {
symbol: FieldSymbol::Day(fields::Day::DayOfMonth),
length: FieldLength::Short,
}),
PatternItem::Literal(Cow::Borrowed(" ")),
PatternItem::Field(Field {
symbol: FieldSymbol::Month(fields::Month::Format),
length: FieldLength::Abbreviated,
}),
PatternItem::Literal(Cow::Borrowed(" ")),
PatternItem::Field(Field {
symbol: FieldSymbol::Year(fields::Year::Calendar),
length: FieldLength::Short,
}),
])),
),
(
Skeleton(Cow::Borrowed(&[
Field {
symbol: FieldSymbol::Hour(fields::Hour::H23),
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Minute,
length: FieldLength::Short,
},
Field {
symbol: FieldSymbol::Second(fields::Second::Second),
length: FieldLength::Short,
},
])),
Pattern(Cow::Borrowed(&[
PatternItem::Field(Field {
symbol: FieldSymbol::Hour(fields::Hour::H23),
length: FieldLength::TwoDigit,
}),
PatternItem::Literal(Cow::Borrowed(":")),
PatternItem::Field(Field {
symbol: FieldSymbol::Minute,
length: FieldLength::TwoDigit,
}),
PatternItem::Literal(Cow::Borrowed(":")),
PatternItem::Field(Field {
symbol: FieldSymbol::Second(fields::Second::Second),
length: FieldLength::TwoDigit,
}),
])),
),
];

#[derive(Default)]
pub struct DummyDataProvider {}
Expand All @@ -116,14 +182,13 @@ impl DummyDataProvider {
}

pub fn get_pattern(&self, options: &DateTimeFormatOptions) -> &Pattern<'static> {
let pattern = match options {
match options {
DateTimeFormatOptions::Style(style) => self.get_pattern_for_style(style),
DateTimeFormatOptions::Components(bag) => {
let skeleton = bag.skeleton().collect();
pattern::get_best_pattern(&skeleton, &PATTERNS)
}
};
pattern
}
}

pub fn get_display_names(
Expand All @@ -138,18 +203,18 @@ impl DummyDataProvider {

fn get_month_names(&self, _length: fields::FieldLength) -> MonthNames<'static> {
MonthNames {
jan: "January".into(),
feb: "February".into(),
mar: "March".into(),
apr: "April".into(),
jan: "Jan".into(),
feb: "Feb".into(),
mar: "Mar".into(),
apr: "Apr".into(),
may: "May".into(),
jun: "June".into(),
jul: "July".into(),
aug: "August".into(),
sep: "September".into(),
oct: "October".into(),
nov: "November".into(),
dec: "December".into(),
jun: "Jun".into(),
jul: "Jul".into(),
aug: "Aug".into(),
sep: "Sep".into(),
oct: "Oct".into(),
nov: "Nov".into(),
dec: "Dec".into(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions components/datetime/tests/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ fn display_names() {
let dtf = DateTimeFormat::try_new(
&data_provider,
&options::DateTimeFormatOptions::Components(options::components::Bag {
year: Default::default(),
month: options::components::Month::Long,
day: Default::default(),
year: options::components::Numeric::Numeric,
month: options::components::Month::Short,
day: options::components::Numeric::Numeric,
..Default::default()
}),
);

let num = dtf.format(&dt);

let s = num.to_string();
assert_eq!(s, "05/08/2020");
assert_eq!(s, "5 Aug 2020");
}

0 comments on commit df29aa7

Please sign in to comment.