2020-05-10 10:27:46 +00:00
use crate ::{ InputValueError , InputValueResult , Result , ScalarType , Value } ;
2020-04-27 09:58:10 +00:00
use async_graphql_derive ::Scalar ;
2020-03-02 00:24:49 +00:00
macro_rules ! impl_float_scalars {
( $( $ty :ty ) , * ) = > {
$(
2020-04-27 09:58:10 +00:00
#[ 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-05-11 13:47:24 +00:00
fn parse ( value : Value ) -> InputValueResult < Self > {
2020-03-02 00:24:49 +00:00
match value {
2020-05-11 13:47:24 +00:00
Value ::Int ( n ) = > Ok ( n as Self ) ,
Value ::Float ( n ) = > Ok ( n as Self ) ,
_ = > Err ( InputValueError ::ExpectedType ( value ) )
2020-05-10 10:27:46 +00:00
}
}
fn is_valid ( value : & Value ) -> bool {
match value {
Value ::Int ( _ ) | Value ::Float ( _ ) = > true ,
_ = > false
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 ) ;