-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8a9bc4
commit 0535b38
Showing
6 changed files
with
273 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::fields; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
Unknown, | ||
FieldTooLong, | ||
UnknownSubstitution(u8), | ||
} | ||
|
||
impl From<fields::Error> for Error { | ||
fn from(input: fields::Error) -> Self { | ||
match input { | ||
fields::Error::Unknown => Self::Unknown, | ||
fields::Error::TooLong => Self::FieldTooLong, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
mod error; | ||
mod parser; | ||
|
||
use crate::fields::{self, Field, FieldLength, FieldSymbol}; | ||
pub use error::Error; | ||
use parser::Parser; | ||
use std::iter::FromIterator; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub enum PatternItem { | ||
Field(fields::Field), | ||
Literal(String), | ||
} | ||
|
||
impl From<Field> for PatternItem { | ||
fn from(input: Field) -> Self { | ||
Self::Field(input) | ||
} | ||
} | ||
|
||
impl From<(FieldSymbol, FieldLength)> for PatternItem { | ||
fn from(input: (FieldSymbol, FieldLength)) -> Self { | ||
Field { | ||
symbol: input.0, | ||
length: input.1, | ||
} | ||
.into() | ||
} | ||
} | ||
|
||
impl<'p> From<&str> for PatternItem { | ||
fn from(input: &str) -> Self { | ||
Self::Literal(input.into()) | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone, PartialEq)] | ||
pub struct Pattern(pub Vec<PatternItem>); | ||
|
||
impl Pattern { | ||
pub fn from_bytes(input: &[u8]) -> Result<Self, Error> { | ||
Parser::new(input).parse().map(Pattern) | ||
} | ||
|
||
// TODO(#277): This should be turned into a utility for all ICU4X. | ||
pub fn from_bytes_combination( | ||
input: &[u8], | ||
date: Pattern, | ||
time: Pattern, | ||
) -> Result<Self, Error> { | ||
Parser::new(input).parse_placeholders(vec![time, date]).map(Pattern) | ||
} | ||
} | ||
|
||
impl FromIterator<PatternItem> for Pattern { | ||
fn from_iter<I: IntoIterator<Item = PatternItem>>(iter: I) -> Self { | ||
let items: Vec<PatternItem> = iter.into_iter().collect(); | ||
Self(items) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn pattern_parse() { | ||
assert_eq!( | ||
Pattern::from_bytes(b"dd/MM/y").expect("Parsing pattern failed."), | ||
vec![ | ||
(fields::Day::DayOfMonth.into(), FieldLength::TwoDigit).into(), | ||
"/".into(), | ||
(fields::Month::Format.into(), FieldLength::TwoDigit).into(), | ||
"/".into(), | ||
(fields::Year::Calendar.into(), FieldLength::One).into(), | ||
] | ||
.into_iter() | ||
.collect() | ||
); | ||
|
||
assert_eq!( | ||
Pattern::from_bytes(b"HH:mm:ss").expect("Parsing pattern failed."), | ||
vec![ | ||
(fields::Hour::H23.into(), FieldLength::TwoDigit).into(), | ||
":".into(), | ||
(FieldSymbol::Minute, FieldLength::TwoDigit).into(), | ||
":".into(), | ||
(fields::Second::Second.into(), FieldLength::TwoDigit).into(), | ||
] | ||
.into_iter() | ||
.collect() | ||
); | ||
} | ||
} |
Oops, something went wrong.