-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathcodebuild_time.rs
109 lines (91 loc) · 3.04 KB
/
codebuild_time.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::{
de::{Deserializer, Error as DeError, Visitor},
ser::Serializer,
Deserialize,
};
use std::fmt;
// Jan 2, 2006 3:04:05 PM
const CODEBUILD_TIME_FORMAT: &str = "%b %e, %Y %l:%M:%S %p";
struct TimeVisitor;
impl<'de> Visitor<'de> for TimeVisitor {
type Value = DateTime<Utc>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "valid codebuild time: {}", CODEBUILD_TIME_FORMAT)
}
fn visit_str<E: DeError>(self, val: &str) -> Result<Self::Value, E> {
NaiveDateTime::parse_from_str(val, CODEBUILD_TIME_FORMAT)
.map(|naive| naive.and_utc())
.map_err(|e| DeError::custom(format!("Parse error {} for {}", e, val)))
}
}
pub(crate) mod str_time {
use super::*;
pub(crate) fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
d.deserialize_str(TimeVisitor)
}
pub fn serialize<S: Serializer>(date: &DateTime<Utc>, ser: S) -> Result<S::Ok, S::Error> {
let s = format!("{}", date.format(CODEBUILD_TIME_FORMAT));
ser.serialize_str(&s)
}
}
pub(crate) mod optional_time {
use super::*;
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<String> = Option::deserialize(deserializer)?;
if let Some(val) = s {
let visitor = TimeVisitor {};
return visitor.visit_str(&val).map(Some);
}
Ok(None)
}
pub fn serialize<S: Serializer>(date: &Option<DateTime<Utc>>, ser: S) -> Result<S::Ok, S::Error> {
if let Some(date) = date {
return str_time::serialize(date, ser);
}
ser.serialize_none()
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestTime = DateTime<Utc>;
#[test]
fn test_deserialize_codebuild_time() {
#[derive(Deserialize)]
struct Test {
#[serde(with = "str_time")]
pub date: TestTime,
}
let data = aws_lambda_json_impl::json!({
"date": "Sep 1, 2017 4:12:29 PM"
});
let expected = NaiveDateTime::parse_from_str("Sep 1, 2017 4:12:29 PM", CODEBUILD_TIME_FORMAT)
.unwrap()
.and_utc();
let decoded: Test = aws_lambda_json_impl::from_value(data).unwrap();
assert_eq!(expected, decoded.date);
}
#[test]
fn test_deserialize_codebuild_optional_time() {
#[derive(Deserialize)]
struct Test {
#[serde(with = "optional_time")]
pub date: Option<TestTime>,
}
let data = aws_lambda_json_impl::json!({
"date": "Sep 1, 2017 4:12:29 PM"
});
let expected = NaiveDateTime::parse_from_str("Sep 1, 2017 4:12:29 PM", CODEBUILD_TIME_FORMAT)
.unwrap()
.and_utc();
let decoded: Test = aws_lambda_json_impl::from_value(data).unwrap();
assert_eq!(Some(expected), decoded.date);
}
}