async-graphql/src/scalars/floats.rs
sunli 8e9aff105e Support Upload Stream #15
I think the previous implementation is not elegant enough, the `QueryBuilder::set_files_holder` function looks disgusting, so I refactored it.
By the way, the performance of parsing InputValue has been optimized, and unnecessary clones have been removed.
2020-05-11 21:47:24 +08:00

41 lines
1.2 KiB
Rust

use crate::{InputValueError, InputValueResult, 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) -> InputValueResult<Self> {
match value {
Value::Int(n) => Ok(n as Self),
Value::Float(n) => Ok(n as Self),
_ => Err(InputValueError::ExpectedType(value))
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Int(_) | Value::Float(_) => true,
_ => false
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok((*self).into())
}
}
)*
};
}
impl_float_scalars!(f32, f64);