async_graphql/types/external/
floats.rs1use crate::{InputValueError, InputValueResult, Number, Scalar, ScalarType, Value};
2
3#[Scalar(internal, name = "Float")]
5impl ScalarType for f32 {
6 fn parse(value: Value) -> InputValueResult<Self> {
7 match value {
8 Value::Number(n) => Ok(n
9 .as_f64()
10 .ok_or_else(|| InputValueError::from("Invalid number"))?
11 as Self),
12 _ => Err(InputValueError::expected_type(value)),
13 }
14 }
15
16 fn is_valid(value: &Value) -> bool {
17 matches!(value, Value::Number(_))
18 }
19
20 fn to_value(&self) -> Value {
21 match Number::from_f64(*self as f64) {
22 Some(n) => Value::Number(n),
23 None => Value::Null,
24 }
25 }
26}
27
28#[Scalar(internal, name = "Float")]
30impl ScalarType for f64 {
31 fn parse(value: Value) -> InputValueResult<Self> {
32 match value {
33 Value::Number(n) => Ok(n
34 .as_f64()
35 .ok_or_else(|| InputValueError::from("Invalid number"))?
36 as Self),
37 _ => Err(InputValueError::expected_type(value)),
38 }
39 }
40
41 fn is_valid(value: &Value) -> bool {
42 matches!(value, Value::Number(_))
43 }
44
45 fn to_value(&self) -> Value {
46 match Number::from_f64(*self) {
47 Some(n) => Value::Number(n),
48 None => Value::Null,
49 }
50 }
51}