Commit 982f6e98 authored by Andrew Chin's avatar Andrew Chin

Parse RFC3339 date formats for HookDate

Also moved the map_err call to the end, which I think makes the function
a little easier to read
parent 94fb7c64
......@@ -5,3 +5,4 @@
// except according to those terms.
mod types;
mod webhooks;
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crates::serde_json::from_str;
use webhooks::*;
#[test]
fn test_hookdate_deseriazlie() {
let _hook: HookDate = from_str("\"2019-01-20 15:00:12 UTC\"").unwrap();
let _hook: HookDate = from_str("\"2019-03-01T19:39:17Z\"").unwrap();
let _hook: HookDate = from_str("\"2019-03-01T17:50:02.036-05:00\"").unwrap();
}
......@@ -44,14 +44,16 @@ impl<'de> Deserialize<'de> for HookDate {
Utc.datetime_from_str(&val, "%Y-%m-%d %H:%M:%S UTC")
.or_else(|_| {
DateTime::parse_from_str(&val, "%Y-%m-%d %H:%M:%S %z")
.map_err(|err| {
D::Error::invalid_value(
Unexpected::Other("hook date"),
&format!("{:?}", err).as_str(),
)
})
.map(|dt| dt.with_timezone(&Utc))
DateTime::parse_from_rfc3339(&val).map(|dt| dt.with_timezone(&Utc))
})
.or_else(|_| {
DateTime::parse_from_str(&val, "%Y-%m-%d %H:%M:%S %z").map(|dt| dt.with_timezone(&Utc))
})
.map_err(|err| {
D::Error::invalid_value(
Unexpected::Other("hook date"),
&format!("Unsupported format: {} {:?}", val, err).as_str(),
)
})
.map(HookDate)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment