async-graphql/src/scalars/chrono_tz.rs

23 lines
558 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)]
impl ScalarType for Tz {
2020-03-27 01:03:30 +00:00
fn type_name() -> &'static str {
"TimeZone"
}
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),
2020-03-27 01:03:30 +00:00
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok(Tz::name(self).into())
}
}