async_graphql/types/external/
non_zero_integers.rs

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