async-graphql/src/scalars/floats.rs
sunli dc7c8d5280 Merge branch 'parser'
Implement a new GraphQL query parser and remove the dependency on graphql-parser.
2020-05-09 17:55:04 +08:00

34 lines
970 B
Rust

use crate::{Result, ScalarType, Value};
use async_graphql_derive::Scalar;
macro_rules! impl_float_scalars {
($($ty:ty),*) => {
$(
#[Scalar(internal)]
impl ScalarType for $ty {
fn type_name() -> &'static str {
"Float"
}
fn description() -> Option<&'static str> {
Some("The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).")
}
fn parse(value: &Value) -> Option<Self> {
match value {
Value::Int(n) => Some(*n as Self),
Value::Float(n) => Some(*n as Self),
_ => None
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok((*self).into())
}
}
)*
};
}
impl_float_scalars!(f32, f64);