async-graphql/src/scalars/floats.rs

34 lines
970 B
Rust
Raw Normal View History

use crate::{Result, ScalarType, Value};
use async_graphql_derive::Scalar;
2020-03-02 00:24:49 +00:00
macro_rules! impl_float_scalars {
($($ty:ty),*) => {
$(
#[Scalar(internal)]
impl ScalarType for $ty {
2020-03-02 00:24:49 +00:00
fn type_name() -> &'static str {
2020-03-03 11:15:18 +00:00
"Float"
2020-03-02 00:24:49 +00:00
}
2020-03-03 11:15:18 +00:00
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).")
}
2020-03-04 02:38:07 +00:00
fn parse(value: &Value) -> Option<Self> {
2020-03-02 00:24:49 +00:00
match value {
Value::Int(n) => Some(*n as Self),
2020-03-04 02:38:07 +00:00
Value::Float(n) => Some(*n as Self),
2020-03-03 11:15:18 +00:00
_ => None
2020-03-02 00:24:49 +00:00
}
}
2020-03-25 03:39:28 +00:00
fn to_json(&self) -> Result<serde_json::Value> {
Ok((*self).into())
2020-03-02 00:24:49 +00:00
}
}
)*
};
}
impl_float_scalars!(f32, f64);