2020-05-26 10:34:43 +00:00
use crate ::{ InputValueError , InputValueResult , ScalarType , Value } ;
2020-04-27 09:58:10 +00:00
use async_graphql_derive ::Scalar ;
2020-03-02 00:24:49 +00:00
2020-05-28 02:26:07 +00:00
/// The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
#[ Scalar(internal, name = " Float " ) ]
impl ScalarType for f32 {
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 ) ) ,
}
}
2020-03-02 00:24:49 +00:00
2020-05-28 02:26:07 +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-05-28 02:26:07 +00:00
}
fn to_value ( & self ) -> Value {
Value ::Float ( * self as f64 )
}
2020-03-02 00:24:49 +00:00
}
2020-05-28 02:26:07 +00:00
/// The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
#[ Scalar(internal, name = " Float " ) ]
impl ScalarType for f64 {
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_value ( & self ) -> Value {
Value ::Float ( * self as f64 )
}
}