Skip to content

Commit cc4c788

Browse files
committed
Improve handling of String and u64
1 parent 6e4fd58 commit cc4c788

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

graphql_client/src/serde_with.rs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ pub trait GraphQLVisitor<'de>: Sized {
2626
&Self::type_name(),
2727
))
2828
}
29-
/// Convert a string ID to the target type.
29+
30+
/// Visit an integer
31+
fn visit_u64<E>(v: u64) -> Result<Self, E>
32+
where
33+
E: de::Error,
34+
{
35+
Err(de::Error::invalid_type(
36+
de::Unexpected::Unsigned(v),
37+
&Self::type_name(),
38+
))
39+
}
40+
41+
/// Visit a borrowed string
3042
fn visit_str<E>(v: &str) -> Result<Self, E>
3143
where
3244
E: de::Error,
@@ -36,7 +48,19 @@ pub trait GraphQLVisitor<'de>: Sized {
3648
&Self::type_name(),
3749
))
3850
}
39-
/// Produce a default value for a null ID.
51+
52+
/// Visit a string
53+
fn visit_string<E>(v: String) -> Result<Self, E>
54+
where
55+
E: de::Error,
56+
{
57+
Err(de::Error::invalid_type(
58+
de::Unexpected::Str(&v),
59+
&Self::type_name(),
60+
))
61+
}
62+
63+
/// Visit a missing optional value
4064
fn visit_none<E>() -> Result<Self, E>
4165
where
4266
E: de::Error,
@@ -47,7 +71,7 @@ pub trait GraphQLVisitor<'de>: Sized {
4771
))
4872
}
4973

50-
/// Produce a default value for a unit.
74+
/// Visit a null value
5175
fn visit_unit<E>() -> Result<Self, E>
5276
where
5377
E: de::Error,
@@ -58,7 +82,7 @@ pub trait GraphQLVisitor<'de>: Sized {
5882
))
5983
}
6084

61-
/// Convert a sequence of IDs to the target type.
85+
/// Visit a sequence
6286
fn visit_seq<A>(seq: A) -> Result<Self, A::Error>
6387
where
6488
A: SeqAccess<'de>,
@@ -83,12 +107,26 @@ impl GraphQLVisitor<'_> for String {
83107
Ok(v.to_string())
84108
}
85109

110+
fn visit_u64<E>(v: u64) -> Result<Self, E>
111+
where
112+
E: de::Error,
113+
{
114+
Ok(v.to_string())
115+
}
116+
86117
fn visit_str<E>(v: &str) -> Result<Self, E>
87118
where
88119
E: de::Error,
89120
{
90121
Ok(v.to_string())
91122
}
123+
124+
fn visit_string<E>(v: String) -> Result<Self, E>
125+
where
126+
E: de::Error,
127+
{
128+
Ok(v)
129+
}
92130
}
93131

94132
impl<'de, T: GraphQLVisitor<'de>> GraphQLVisitor<'de> for Option<T> {
@@ -103,13 +141,27 @@ impl<'de, T: GraphQLVisitor<'de>> GraphQLVisitor<'de> for Option<T> {
103141
T::visit_i64(v).map(Some)
104142
}
105143

144+
fn visit_u64<E>(v: u64) -> Result<Self, E>
145+
where
146+
E: de::Error,
147+
{
148+
T::visit_u64(v).map(Some)
149+
}
150+
106151
fn visit_str<E>(v: &str) -> Result<Self, E>
107152
where
108153
E: de::Error,
109154
{
110155
T::visit_str(v).map(Some)
111156
}
112157

158+
fn visit_string<E>(v: String) -> Result<Self, E>
159+
where
160+
E: de::Error,
161+
{
162+
T::visit_string(v).map(Some)
163+
}
164+
113165
fn visit_none<E>() -> Result<Self, E>
114166
where
115167
E: de::Error,
@@ -188,7 +240,7 @@ where
188240
where
189241
E: de::Error,
190242
{
191-
T::visit_i64(value as i64)
243+
T::visit_u64(value)
192244
}
193245

194246
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>

0 commit comments

Comments
 (0)