dynamic_graphql/
from_value.rs1use crate::MaybeUndefined;
2use crate::Result;
3use crate::dynamic;
4use crate::errors::InputValueError;
5use crate::errors::InputValueResult;
6use crate::types::GetInputTypeRef;
7
8pub trait FromValue: Sized {
9 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self>;
10}
11
12impl FromValue for String {
13 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
14 Ok(value?.string().map(|s| s.to_string())?)
15 }
16}
17
18impl FromValue for async_graphql::ID {
19 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
20 Ok(value?.string().map(|s| async_graphql::ID(s.to_string()))?)
21 }
22}
23
24impl FromValue for bool {
25 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
26 Ok(value?.boolean()?)
27 }
28}
29
30impl FromValue for f32 {
31 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
32 Ok(value?.f32()?)
33 }
34}
35
36impl FromValue for f64 {
37 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
38 Ok(value?.f64()?)
39 }
40}
41
42macro_rules! uint_from_value {
43 ($($t:ty),*) => {
44 $(
45 impl FromValue for $t {
46 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
47 Self::try_from(value?.u64()?).map_err(|_| {
48 InputValueError::custom(format!(
49 "Only integers from {} to {} are accepted for {}.",
50 Self::MIN,
51 Self::MAX,
52 stringify!($t),
53 ))
54 })
55 }
56 }
57 )*
58 };
59}
60macro_rules! int_from_value {
61 ($($t:ty),*) => {
62 $(
63 impl FromValue for $t {
64 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
65 Self::try_from(value?.i64()?).map_err(|_| {
66 InputValueError::custom(format!(
67 "Only integers from {} to {} are accepted for {}.",
68 Self::MIN,
69 Self::MAX,
70 stringify!($t),
71 ))
72 })
73 }
74 }
75 )*
76 };
77}
78
79uint_from_value!(u8, u16, u32, u64, usize);
80int_from_value!(i8, i16, i32, i64, isize);
81
82impl<T> FromValue for Option<T>
83where
84 T: FromValue + GetInputTypeRef,
85{
86 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
87 match value.ok() {
88 None => Ok(None),
89 Some(value) if value.is_null() => Ok(None),
90 Some(value) => Ok(Some(
91 T::from_value(Ok(value)).map_err(InputValueError::propagate)?,
92 )),
93 }
94 }
95}
96
97impl<T> FromValue for Result<T>
98where
99 T: FromValue + GetInputTypeRef,
100{
101 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
102 let value = T::from_value(Ok(value?)).map_err(Into::into);
103 Ok(value)
104 }
105}
106
107impl<T> FromValue for MaybeUndefined<T>
108where
109 T: FromValue + GetInputTypeRef,
110{
111 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
112 match value.ok() {
113 None => Ok(MaybeUndefined::Undefined),
114 Some(value) if value.is_null() => Ok(MaybeUndefined::Null),
115 Some(value) => Ok(MaybeUndefined::Value(
116 T::from_value(Ok(value)).map_err(InputValueError::propagate)?,
117 )),
118 }
119 }
120}
121
122impl<T> FromValue for Vec<T>
123where
124 T: FromValue + GetInputTypeRef,
125{
126 fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
127 value?
128 .list()?
129 .iter()
130 .map(|v| T::from_value(Ok(v)).map_err(InputValueError::propagate))
131 .collect()
132 }
133}