Remove `FormatError`

pull/1235/head
Jason Rodney Hansen 3 years ago committed by Ivan Tham
parent 31ed91dc2e
commit 0b7911d921

@ -112,22 +112,22 @@ impl Increment for DateTimeIncrementor {
static FORMATS: Lazy<Vec<Format>> = Lazy::new(|| { static FORMATS: Lazy<Vec<Format>> = Lazy::new(|| {
vec![ vec![
Format::new("%Y-%m-%d %H:%M:%S").unwrap(), // 2021-11-24 07:12:23 Format::new("%Y-%m-%d %H:%M:%S"), // 2021-11-24 07:12:23
Format::new("%Y/%m/%d %H:%M:%S").unwrap(), // 2021/11/24 07:12:23 Format::new("%Y/%m/%d %H:%M:%S"), // 2021/11/24 07:12:23
Format::new("%Y-%m-%d %H:%M").unwrap(), // 2021-11-24 07:12 Format::new("%Y-%m-%d %H:%M"), // 2021-11-24 07:12
Format::new("%Y/%m/%d %H:%M").unwrap(), // 2021/11/24 07:12 Format::new("%Y/%m/%d %H:%M"), // 2021/11/24 07:12
Format::new("%Y-%m-%d").unwrap(), // 2021-11-24 Format::new("%Y-%m-%d"), // 2021-11-24
Format::new("%Y/%m/%d").unwrap(), // 2021/11/24 Format::new("%Y/%m/%d"), // 2021/11/24
Format::new("%a %b %d %Y").unwrap(), // Wed Nov 24 2021 Format::new("%a %b %d %Y"), // Wed Nov 24 2021
Format::new("%d-%b-%Y").unwrap(), // 24-Nov-2021 Format::new("%d-%b-%Y"), // 24-Nov-2021
Format::new("%Y %b %d").unwrap(), // 2021 Nov 24 Format::new("%Y %b %d"), // 2021 Nov 24
Format::new("%b %d, %Y").unwrap(), // Nov 24, 2021 Format::new("%b %d, %Y"), // Nov 24, 2021
Format::new("%-I:%M:%S %P").unwrap(), // 7:21:53 am Format::new("%-I:%M:%S %P"), // 7:21:53 am
Format::new("%-I:%M %P").unwrap(), // 7:21 am Format::new("%-I:%M %P"), // 7:21 am
Format::new("%-I:%M:%S %p").unwrap(), // 7:21:53 AM Format::new("%-I:%M:%S %p"), // 7:21:53 AM
Format::new("%-I:%M %p").unwrap(), // 7:21 AM Format::new("%-I:%M %p"), // 7:21 AM
Format::new("%H:%M:%S").unwrap(), // 23:24:23 Format::new("%H:%M:%S"), // 23:24:23
Format::new("%H:%M").unwrap(), // 23:24 Format::new("%H:%M"), // 23:24
] ]
}); });
@ -140,7 +140,7 @@ struct Format {
} }
impl Format { impl Format {
fn new(fmt: &'static str) -> Result<Self, FormatError> { fn new(fmt: &'static str) -> Self {
let mut remaining = fmt; let mut remaining = fmt;
let mut fields = Vec::new(); let mut fields = Vec::new();
let mut regex = String::new(); let mut regex = String::new();
@ -149,54 +149,32 @@ impl Format {
while let Some(i) = remaining.find('%') { while let Some(i) = remaining.find('%') {
let after = &remaining[i + 1..]; let after = &remaining[i + 1..];
let mut chars = after.chars(); let mut chars = after.chars();
let c = chars let c = chars.next().unwrap();
.next()
.ok_or(FormatError::UnexpectedEndOfFormatString)?;
let spec_len = if c == '-' { let spec_len = if c == '-' {
if let Some(c) = chars.next() { 1 + chars.next().unwrap().len_utf8()
1 + c.len_utf8()
} else {
return Err(FormatError::UnexpectedEndOfFormatString);
}
} else { } else {
c.len_utf8() c.len_utf8()
}; };
if i < remaining.len() - spec_len {
let specifier = &after[..spec_len]; let specifier = &after[..spec_len];
if let Some(field) = DateField::from_specifier(specifier) { let field = DateField::from_specifier(specifier).unwrap();
fields.push(field); fields.push(field);
max_len += field.max_len + remaining[..i].len(); max_len += field.max_len + remaining[..i].len();
regex += &remaining[..i]; regex += &remaining[..i];
regex += &format!("({})", field.regex); regex += &format!("({})", field.regex);
remaining = &after[spec_len..]; remaining = &after[spec_len..];
} else {
return Err(FormatError::UnsupportedSpecifier(
&remaining[i..i + 1 + spec_len],
));
}
} else {
return Err(FormatError::UnexpectedEndOfFormatString);
}
} }
let regex = Regex::new(&regex).map_err(FormatError::Regex)?; let regex = Regex::new(&regex).unwrap();
Ok(Self { Self {
fmt, fmt,
fields, fields,
regex, regex,
max_len, max_len,
})
} }
} }
#[derive(Clone, Debug)]
enum FormatError {
UnexpectedEndOfFormatString,
UnsupportedSpecifier(&'static str),
Regex(regex::Error),
} }
impl PartialEq for Format { impl PartialEq for Format {

Loading…
Cancel
Save