async-graphql/src/datetime.rs
2020-03-01 21:35:39 +08:00

26 lines
660 B
Rust

use crate::{QueryError, Scalar, Result, Value};
use chrono::{DateTime, TimeZone, Utc};
impl Scalar for DateTime<Utc> {
fn type_name() -> &'static str {
"DateTime"
}
fn parse(value: Value) -> Result<Self> {
match value {
Value::String(s) => Ok(Utc.datetime_from_str(&s, "%+")?),
_ => {
return Err(QueryError::ExpectedType {
expect: Self::type_name().to_string(),
actual: value,
}
.into())
}
}
}
fn into_json(self) -> Result<serde_json::Value> {
Ok(self.to_rfc3339().into())
}
}