1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::{InputValueError, InputValueResult, Result, ScalarType, Value};
use async_graphql_derive::Scalar;

macro_rules! impl_integer_scalars {
    ($($ty:ty),*) => {
        $(
        #[Scalar(internal)]
        impl ScalarType for $ty {
            fn type_name() -> &'static str {
                "Int"
            }

            fn description() -> Option<&'static str> {
                Some("The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.")
            }

            fn parse(value: Value) -> InputValueResult<Self> {
                match value {
                    Value::Int(n) => Ok(n as Self),
                    _ => Err(InputValueError::ExpectedType(value))
                }
            }

            fn is_valid(value: &Value) -> bool {
                match value {
                    Value::Int(_) => true,
                    _ => false
                }
            }

            fn to_json(&self) -> Result<serde_json::Value> {
                Ok((*self).into())
            }
        }
        )*
    };
}

impl_integer_scalars!(i8, i16, i32, u8, u16, u32);

macro_rules! impl_int64_scalars {
    ($($ty:ty),*) => {
        $(
        #[Scalar(internal)]
        impl ScalarType for $ty {
            fn type_name() -> &'static str {
                "Int64"
            }

            fn description() -> Option<&'static str> {
                Some("The `Int64` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^64) and 2^64 - 1.")
            }

            fn parse(value: Value) -> InputValueResult<Self> {
                match value {
                    Value::Int(n) => Ok(n as Self),
                    Value::String(s) => Ok(s.parse()?),
                    _ => Err(InputValueError::ExpectedType(value))
                }
            }

            fn is_valid(value: &Value) -> bool {
                match value {
                    Value::Int(_) | Value::String(_) => true,
                    _ => false
                }
            }

            fn to_json(&self) -> Result<serde_json::Value> {
                Ok(self.to_string().into())
            }
        }
        )*
    };
}

impl_int64_scalars!(i64, u64);