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
78
79
80
81
82
83
84
85
use crate::{InputValueError, InputValueResult, ScalarType, Value};
use async_graphql_derive::Scalar;
macro_rules! int_scalar {
($($ty:ty),*) => {
$(
#[Scalar(internal, name = "Int")]
impl ScalarType for $ty {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Number(n) => {
let n = n
.as_i64()
.ok_or_else(|| InputValueError::from("Invalid number"))?;
if n < Self::MIN as i64 || n > Self::MAX as i64 {
return Err(InputValueError::from(format!(
"Only integers from {} to {} are accepted.",
Self::MIN,
Self::MAX
)));
}
Ok(n as Self)
}
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Number(n) if n.is_i64() => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Number(serde_json::Number::from(*self as i64))
}
}
)*
};
}
macro_rules! uint_scalar {
($($ty:ty),*) => {
$(
#[Scalar(internal, name = "Int")]
impl ScalarType for $ty {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::Number(n) => {
let n = n
.as_u64()
.ok_or_else(|| InputValueError::from("Invalid number"))?;
if n > Self::MAX as u64 {
return Err(InputValueError::from(format!(
"Only integers from {} to {} are accepted.",
0,
Self::MAX
)));
}
Ok(n as Self)
}
_ => Err(InputValueError::ExpectedType(value)),
}
}
fn is_valid(value: &Value) -> bool {
match value {
Value::Number(n) if n.is_u64() => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::Number(serde_json::Number::from(*self as u64))
}
}
)*
};
}
int_scalar!(i8, i16, i32, i64);
uint_scalar!(u8, u16, u32, u64);