async-graphql/src/scalars/chrono_tz.rs

19 lines
520 B
Rust
Raw Normal View History

use crate::{InputValueError, InputValueResult, Result, ScalarType, Value};
use async_graphql_derive::Scalar;
2020-03-27 01:03:30 +00:00
use chrono_tz::Tz;
use std::str::FromStr;
#[Scalar(internal, name = "TimeZone")]
impl ScalarType for Tz {
fn parse(value: Value) -> InputValueResult<Self> {
2020-03-27 01:03:30 +00:00
match value {
Value::String(s) => Ok(Tz::from_str(&s)?),
_ => Err(InputValueError::ExpectedType(value)),
2020-03-27 01:03:30 +00:00
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok(Tz::name(self).into())
}
}