2020-03-01 16:52:05 +00:00
|
|
|
use crate::{QueryError, Result, Scalar, Value};
|
2020-03-01 10:54:34 +00:00
|
|
|
use chrono::{DateTime, TimeZone, Utc};
|
2020-03-02 00:24:49 +00:00
|
|
|
use std::borrow::Cow;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-01 13:35:39 +00:00
|
|
|
impl Scalar for DateTime<Utc> {
|
2020-03-01 10:54:34 +00:00
|
|
|
fn type_name() -> &'static str {
|
|
|
|
"DateTime"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(value: Value) -> Result<Self> {
|
|
|
|
match value {
|
|
|
|
Value::String(s) => Ok(Utc.datetime_from_str(&s, "%+")?),
|
|
|
|
_ => {
|
2020-03-01 13:35:39 +00:00
|
|
|
return Err(QueryError::ExpectedType {
|
2020-03-02 00:24:49 +00:00
|
|
|
expect: Cow::Borrowed(Self::type_name()),
|
2020-03-01 10:54:34 +00:00
|
|
|
actual: value,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:52:05 +00:00
|
|
|
fn parse_from_json(value: serde_json::Value) -> Result<Self> {
|
|
|
|
match value {
|
|
|
|
serde_json::Value::String(s) => Ok(Utc.datetime_from_str(&s, "%+")?),
|
|
|
|
_ => {
|
|
|
|
return Err(QueryError::ExpectedJsonType {
|
2020-03-02 00:24:49 +00:00
|
|
|
expect: Cow::Borrowed(Self::type_name()),
|
2020-03-01 16:52:05 +00:00
|
|
|
actual: value,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
fn to_json(&self) -> Result<serde_json::Value> {
|
2020-03-01 10:54:34 +00:00
|
|
|
Ok(self.to_rfc3339().into())
|
|
|
|
}
|
|
|
|
}
|