async-graphql/src/scalars/floats.rs

34 lines
976 B
Rust
Raw Normal View History

2020-03-19 09:20:12 +00:00
use crate::{impl_scalar_internal, Result, Scalar, Value};
2020-03-02 00:24:49 +00:00
macro_rules! impl_float_scalars {
($($ty:ty),*) => {
$(
2020-03-19 09:20:12 +00:00
impl Scalar 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 {
2020-03-03 11:15:18 +00:00
Value::Int(n) => Some(n.as_i64().unwrap() 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
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok((*self).into())
}
}
2020-03-06 15:58:43 +00:00
2020-03-09 10:05:52 +00:00
impl_scalar_internal!($ty);
2020-03-02 00:24:49 +00:00
)*
};
}
impl_float_scalars!(f32, f64);