async-graphql/src/datetime.rs

26 lines
660 B
Rust
Raw Normal View History

2020-03-01 13:35:39 +00:00
use crate::{QueryError, Scalar, Result, Value};
2020-03-01 10:54:34 +00:00
use chrono::{DateTime, TimeZone, Utc};
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-01 10:54:34 +00:00
expect: Self::type_name().to_string(),
actual: value,
}
.into())
}
}
}
fn into_json(self) -> Result<serde_json::Value> {
Ok(self.to_rfc3339().into())
}
}