async_graphql/types/external/
integers.rs1use crate::{InputValueError, InputValueResult, Number, Scalar, ScalarType, Value};
2
3#[Scalar(internal, name = "Int")]
5impl ScalarType for i8 {
6 fn parse(value: Value) -> InputValueResult<Self> {
7 match value {
8 Value::Number(n) => {
9 let n = n
10 .as_i64()
11 .ok_or_else(|| InputValueError::from("Invalid number"))?;
12 if n < Self::MIN as i64 || n > Self::MAX as i64 {
13 return Err(InputValueError::from(format!(
14 "Only integers from {} to {} are accepted.",
15 Self::MIN,
16 Self::MAX
17 )));
18 }
19 Ok(n as Self)
20 }
21 _ => Err(InputValueError::expected_type(value)),
22 }
23 }
24
25 fn is_valid(value: &Value) -> bool {
26 matches!(value, Value::Number(n) if n.is_i64())
27 }
28
29 fn to_value(&self) -> Value {
30 Value::Number(Number::from(*self as i64))
31 }
32}
33
34#[Scalar(internal, name = "Int")]
36impl ScalarType for i16 {
37 fn parse(value: Value) -> InputValueResult<Self> {
38 match value {
39 Value::Number(n) => {
40 let n = n
41 .as_i64()
42 .ok_or_else(|| InputValueError::from("Invalid number"))?;
43 if n < Self::MIN as i64 || n > Self::MAX as i64 {
44 return Err(InputValueError::from(format!(
45 "Only integers from {} to {} are accepted.",
46 Self::MIN,
47 Self::MAX
48 )));
49 }
50 Ok(n as Self)
51 }
52 _ => Err(InputValueError::expected_type(value)),
53 }
54 }
55
56 fn is_valid(value: &Value) -> bool {
57 matches!(value, Value::Number(n) if n.is_i64() )
58 }
59
60 fn to_value(&self) -> Value {
61 Value::Number(Number::from(*self as i64))
62 }
63}
64
65#[Scalar(internal, name = "Int")]
67impl ScalarType for i32 {
68 fn parse(value: Value) -> InputValueResult<Self> {
69 match value {
70 Value::Number(n) => {
71 let n = n
72 .as_i64()
73 .ok_or_else(|| InputValueError::from("Invalid number"))?;
74 if n < Self::MIN as i64 || n > Self::MAX as i64 {
75 return Err(InputValueError::from(format!(
76 "Only integers from {} to {} are accepted.",
77 Self::MIN,
78 Self::MAX
79 )));
80 }
81 Ok(n as Self)
82 }
83 _ => Err(InputValueError::expected_type(value)),
84 }
85 }
86
87 fn is_valid(value: &Value) -> bool {
88 matches!(value, Value::Number(n) if n.is_i64())
89 }
90
91 fn to_value(&self) -> Value {
92 Value::Number(Number::from(*self as i64))
93 }
94}
95
96#[Scalar(internal, name = "Int")]
98impl ScalarType for i64 {
99 fn parse(value: Value) -> InputValueResult<Self> {
100 match value {
101 Value::Number(n) => {
102 let n = n
103 .as_i64()
104 .ok_or_else(|| InputValueError::from("Invalid number"))?;
105 Ok(n as Self)
106 }
107 _ => Err(InputValueError::expected_type(value)),
108 }
109 }
110
111 fn is_valid(value: &Value) -> bool {
112 matches!(value, Value::Number(n) if n.is_i64())
113 }
114
115 fn to_value(&self) -> Value {
116 Value::Number(Number::from(*self))
117 }
118}
119
120#[Scalar(internal, name = "Int")]
122impl ScalarType for u8 {
123 fn parse(value: Value) -> InputValueResult<Self> {
124 match value {
125 Value::Number(n) => {
126 let n = n
127 .as_u64()
128 .ok_or_else(|| InputValueError::from("Invalid number"))?;
129 if n > Self::MAX as u64 {
130 return Err(InputValueError::from(format!(
131 "Only integers from {} to {} are accepted.",
132 0,
133 Self::MAX
134 )));
135 }
136 Ok(n as Self)
137 }
138 _ => Err(InputValueError::expected_type(value)),
139 }
140 }
141
142 fn is_valid(value: &Value) -> bool {
143 matches!(value, Value::Number(n) if n.is_u64())
144 }
145
146 fn to_value(&self) -> Value {
147 Value::Number(Number::from(*self as u64))
148 }
149}
150
151#[Scalar(internal, name = "Int")]
153impl ScalarType for u16 {
154 fn parse(value: Value) -> InputValueResult<Self> {
155 match value {
156 Value::Number(n) => {
157 let n = n
158 .as_u64()
159 .ok_or_else(|| InputValueError::from("Invalid number"))?;
160 if n > Self::MAX as u64 {
161 return Err(InputValueError::from(format!(
162 "Only integers from {} to {} are accepted.",
163 0,
164 Self::MAX
165 )));
166 }
167 Ok(n as Self)
168 }
169 _ => Err(InputValueError::expected_type(value)),
170 }
171 }
172
173 fn is_valid(value: &Value) -> bool {
174 matches!(value, Value::Number(n) if n.is_u64())
175 }
176
177 fn to_value(&self) -> Value {
178 Value::Number(Number::from(*self as u64))
179 }
180}
181
182#[Scalar(internal, name = "Int")]
184impl ScalarType for u32 {
185 fn parse(value: Value) -> InputValueResult<Self> {
186 match value {
187 Value::Number(n) => {
188 let n = n
189 .as_u64()
190 .ok_or_else(|| InputValueError::from("Invalid number"))?;
191 if n > Self::MAX as u64 {
192 return Err(InputValueError::from(format!(
193 "Only integers from {} to {} are accepted.",
194 0,
195 Self::MAX
196 )));
197 }
198 Ok(n as Self)
199 }
200 _ => Err(InputValueError::expected_type(value)),
201 }
202 }
203
204 fn is_valid(value: &Value) -> bool {
205 matches!(value, Value::Number(n) if n.is_u64())
206 }
207
208 fn to_value(&self) -> Value {
209 Value::Number(Number::from(*self as u64))
210 }
211}
212
213#[Scalar(internal, name = "Int")]
215impl ScalarType for u64 {
216 fn parse(value: Value) -> InputValueResult<Self> {
217 match value {
218 Value::Number(n) => {
219 let n = n
220 .as_u64()
221 .ok_or_else(|| InputValueError::from("Invalid number"))?;
222 Ok(n as Self)
223 }
224 _ => Err(InputValueError::expected_type(value)),
225 }
226 }
227
228 fn is_valid(value: &Value) -> bool {
229 matches!(value, Value::Number(n) if n.is_u64())
230 }
231
232 fn to_value(&self) -> Value {
233 Value::Number(Number::from(*self))
234 }
235}
236
237#[Scalar(internal, name = "Int")]
239impl ScalarType for usize {
240 fn parse(value: Value) -> InputValueResult<Self> {
241 match value {
242 Value::Number(n) => {
243 let n = n
244 .as_u64()
245 .ok_or_else(|| InputValueError::from("Invalid number"))?;
246 if n > Self::MAX as u64 {
247 return Err(InputValueError::from(format!(
248 "Only integers from {} to {} are accepted.",
249 0,
250 Self::MAX
251 )));
252 }
253 Ok(n as Self)
254 }
255 _ => Err(InputValueError::expected_type(value)),
256 }
257 }
258
259 fn is_valid(value: &Value) -> bool {
260 matches!(value, Value::Number(n) if n.is_u64())
261 }
262
263 fn to_value(&self) -> Value {
264 Value::Number(Number::from(*self as u64))
265 }
266}
267
268#[Scalar(internal, name = "Int")]
270impl ScalarType for isize {
271 fn parse(value: Value) -> InputValueResult<Self> {
272 match value {
273 Value::Number(n) => {
274 let n = n
275 .as_i64()
276 .ok_or_else(|| InputValueError::from("Invalid number"))?;
277 if n < Self::MIN as i64 || n > Self::MAX as i64 {
278 return Err(InputValueError::from(format!(
279 "Only integers from {} to {} are accepted.",
280 Self::MIN,
281 Self::MAX
282 )));
283 }
284 Ok(n as Self)
285 }
286 _ => Err(InputValueError::expected_type(value)),
287 }
288 }
289
290 fn is_valid(value: &Value) -> bool {
291 matches!(value, Value::Number(n) if n.is_i64())
292 }
293
294 fn to_value(&self) -> Value {
295 Value::Number(Number::from(*self as i64))
296 }
297}