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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::dynamic;
use crate::errors::InputValueError;
use crate::errors::InputValueResult;
use crate::types::GetInputTypeRef;
use crate::MaybeUndefined;
use crate::Result;

pub trait FromValue: Sized {
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self>;
}

impl FromValue for String {
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        Ok(value?.string().map(|s| s.to_string())?)
    }
}

impl FromValue for async_graphql::ID {
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        Ok(value?.string().map(|s| async_graphql::ID(s.to_string()))?)
    }
}

impl FromValue for bool {
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        Ok(value?.boolean()?)
    }
}

impl FromValue for f32 {
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        Ok(value?.f32()?)
    }
}

impl FromValue for f64 {
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        Ok(value?.f64()?)
    }
}

macro_rules! uint_from_value {
    ($($t:ty),*) => {
        $(
            impl FromValue for $t {
                fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
                    Self::try_from(value?.u64()?).map_err(|_| {
                        InputValueError::custom(format!(
                            "Only integers from {} to {} are accepted for {}.",
                            Self::MIN,
                            Self::MAX,
                            stringify!($t),
                        ))
                    })
                }
            }
        )*
    };
}
macro_rules! int_from_value {
    ($($t:ty),*) => {
        $(
            impl FromValue for $t {
                fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
                    Self::try_from(value?.u64()?).map_err(|_| {
                        InputValueError::custom(format!(
                            "Only integers from {} to {} are accepted for {}.",
                            Self::MIN,
                            Self::MAX,
                            stringify!($t),
                        ))
                    })
                }
            }
        )*
    };
}

uint_from_value!(u8, u16, u32, u64, usize);
int_from_value!(i8, i16, i32, i64, isize);

impl<T> FromValue for Option<T>
where
    T: FromValue + GetInputTypeRef,
{
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        match value.ok() {
            None => Ok(None),
            Some(value) if value.is_null() => Ok(None),
            Some(value) => Ok(Some(
                T::from_value(Ok(value)).map_err(InputValueError::propagate)?,
            )),
        }
    }
}

impl<T> FromValue for Result<T>
where
    T: FromValue + GetInputTypeRef,
{
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        let value = T::from_value(Ok(value?)).map_err(Into::into);
        Ok(value)
    }
}

impl<T> FromValue for MaybeUndefined<T>
where
    T: FromValue + GetInputTypeRef,
{
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        match value.ok() {
            None => Ok(MaybeUndefined::Undefined),
            Some(value) if value.is_null() => Ok(MaybeUndefined::Null),
            Some(value) => Ok(MaybeUndefined::Value(
                T::from_value(Ok(value)).map_err(InputValueError::propagate)?,
            )),
        }
    }
}

impl<T> FromValue for Vec<T>
where
    T: FromValue + GetInputTypeRef,
{
    fn from_value(value: Result<dynamic::ValueAccessor>) -> InputValueResult<Self> {
        value?
            .list()?
            .iter()
            .map(|v| T::from_value(Ok(v)).map_err(InputValueError::propagate))
            .collect()
    }
}