Merge pull request #251 from ehuss/datetime-space

0.5: Support space separator in datetime.
This commit is contained in:
Alex Crichton 2018-07-11 09:05:26 -05:00 committed by GitHub
commit 625d2a18bb
3 changed files with 19 additions and 3 deletions

View file

@ -176,7 +176,8 @@ impl FromStr for Datetime {
// Next parse the "partial-time" if available
let partial_time = if full_date.is_some() &&
chars.clone().next() == Some('T') {
(chars.clone().next() == Some('T')
|| chars.clone().next() == Some(' ')) {
chars.next();
true
} else {

View file

@ -1019,6 +1019,17 @@ impl<'a> Deserializer<'a> {
-> Result<(Span, &'a str), Error> {
let start = self.tokens.substr_offset(date);
// Check for space separated date and time.
if let Some((_, Token::Whitespace(s))) = self.peek()? {
if s == " " {
self.next()?;
// Skip past the hour.
if let Some((_, Token::Keylike(_))) = self.peek()? {
self.next()?;
}
}
}
if colon_eaten || self.eat(Token::Colon)? {
// minutes
match self.next()? {

View file

@ -6,10 +6,14 @@ use toml::Value;
#[test]
fn times() {
fn good(s: &str) {
fn dogood(s: &str, serialized: &str) {
let to_parse = format!("foo = {}", s);
let value = Value::from_str(&to_parse).unwrap();
assert_eq!(value["foo"].as_datetime().unwrap().to_string(), s);
assert_eq!(value["foo"].as_datetime().unwrap().to_string(), serialized);
}
fn good(s: &str) {
dogood(s, s);
dogood(&s.replace("T", " "), s);
}
good("1997-09-09T09:09:09Z");