Skip to content

Commit e9a1527

Browse files
committed
Strip InputValue from from_input for scalars
1 parent 20ff0ea commit e9a1527

File tree

19 files changed

+295
-346
lines changed

19 files changed

+295
-346
lines changed

juniper/src/executor_tests/variables.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ impl TestComplexScalar {
1818
graphql_value!("SerializedValue")
1919
}
2020

21-
fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<Self, String> {
22-
v.as_string_value()
21+
fn from_input(s: &impl ScalarValue) -> Result<Self, String> {
22+
s.as_str()
2323
.filter(|s| *s == "SerializedValue")
2424
.map(|_| Self)
25-
.ok_or_else(|| format!(r#"Expected "SerializedValue" string, found: {v}"#))
25+
.ok_or_else(|| format!(r#"Expected "SerializedValue" string, found: {s}"#))
2626
}
2727
}
2828

juniper/src/integrations/bigdecimal.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use std::str::FromStr as _;
1212

13-
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
13+
use crate::{ScalarValue, Value, graphql_scalar};
1414

1515
// TODO: Try remove on upgrade of `bigdecimal` crate.
1616
mod for_minimal_versions_check_only {
@@ -43,18 +43,18 @@ mod bigdecimal_scalar {
4343
Value::scalar(v.to_string())
4444
}
4545

46-
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<BigDecimal, String> {
47-
if let Some(i) = v.as_int_value() {
46+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<BigDecimal, String> {
47+
if let Some(i) = s.as_int() {
4848
Ok(BigDecimal::from(i))
49-
} else if let Some(f) = v.as_float_value() {
49+
} else if let Some(f) = s.as_float() {
5050
// See akubera/bigdecimal-rs#103 for details:
5151
// https://github.com/akubera/bigdecimal-rs/issues/103
5252
let mut buf = ryu::Buffer::new();
5353
BigDecimal::from_str(buf.format(f))
5454
.map_err(|e| format!("Failed to parse `BigDecimal` from `Float`: {e}"))
5555
} else {
56-
v.as_string_value()
57-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
56+
s.as_str()
57+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
5858
.and_then(|s| {
5959
BigDecimal::from_str(s)
6060
.map_err(|e| format!("Failed to parse `BigDecimal` from `String`: {e}"))

juniper/src/integrations/bson.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! [s1]: https://graphql-scalars.dev/docs/scalars/object-id
1414
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
1515
16-
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
16+
use crate::{ScalarValue, Value, graphql_scalar};
1717

1818
// TODO: Try remove on upgrade of `bson` crate.
1919
mod for_minimal_versions_check_only {
@@ -44,9 +44,9 @@ mod object_id {
4444
Value::scalar(v.to_hex())
4545
}
4646

47-
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<ObjectId, String> {
48-
v.as_string_value()
49-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
47+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<ObjectId, String> {
48+
s.as_str()
49+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
5050
.and_then(|s| {
5151
ObjectId::parse_str(s).map_err(|e| format!("Failed to parse `ObjectID`: {e}"))
5252
})
@@ -83,9 +83,9 @@ mod date_time {
8383
)
8484
}
8585

86-
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<DateTime, String> {
87-
v.as_string_value()
88-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
86+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<DateTime, String> {
87+
s.as_str()
88+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
8989
.and_then(|s| {
9090
DateTime::parse_rfc3339_str(s)
9191
.map_err(|e| format!("Failed to parse `DateTime`: {e}"))

juniper/src/integrations/chrono.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::fmt;
2323

2424
use chrono::{FixedOffset, TimeZone};
2525

26-
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
26+
use crate::{ScalarValue, Value, graphql_scalar};
2727

2828
/// Date in the proleptic Gregorian calendar (without time zone).
2929
///
@@ -58,12 +58,9 @@ mod local_date {
5858
Value::scalar(v.format(FORMAT).to_string())
5959
}
6060

61-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalDate, String>
62-
where
63-
S: ScalarValue,
64-
{
65-
v.as_string_value()
66-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
61+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<LocalDate, String> {
62+
s.as_str()
63+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
6764
.and_then(|s| {
6865
LocalDate::parse_from_str(s, FORMAT)
6966
.map_err(|e| format!("Invalid `LocalDate`: {e}"))
@@ -124,12 +121,9 @@ mod local_time {
124121
)
125122
}
126123

127-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalTime, String>
128-
where
129-
S: ScalarValue,
130-
{
131-
v.as_string_value()
132-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
124+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<LocalTime, String> {
125+
s.as_str()
126+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
133127
.and_then(|s| {
134128
// First, try to parse the most used format.
135129
// At the end, try to parse the full format for the parsing
@@ -172,12 +166,9 @@ mod local_date_time {
172166
Value::scalar(v.format(FORMAT).to_string())
173167
}
174168

175-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalDateTime, String>
176-
where
177-
S: ScalarValue,
178-
{
179-
v.as_string_value()
180-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
169+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<LocalDateTime, String> {
170+
s.as_str()
171+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
181172
.and_then(|s| {
182173
LocalDateTime::parse_from_str(s, FORMAT)
183174
.map_err(|e| format!("Invalid `LocalDateTime`: {e}"))
@@ -225,13 +216,12 @@ mod date_time {
225216
)
226217
}
227218

228-
pub(super) fn from_input<S, Tz>(v: &InputValue<S>) -> Result<DateTime<Tz>, String>
219+
pub(super) fn from_input<Tz>(s: &impl ScalarValue) -> Result<DateTime<Tz>, String>
229220
where
230-
S: ScalarValue,
231221
Tz: TimeZone + FromFixedOffset,
232222
{
233-
v.as_string_value()
234-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
223+
s.as_str()
224+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
235225
.and_then(|s| {
236226
DateTime::<FixedOffset>::parse_from_rfc3339(s)
237227
.map_err(|e| format!("Invalid `DateTime`: {e}"))

juniper/src/integrations/chrono_tz.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! [1]: http://www.iana.org/time-zones
1212
//! [s1]: https://graphql-scalars.dev/docs/scalars/time-zone
1313
14-
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
14+
use crate::{ScalarValue, Value, graphql_scalar};
1515

1616
// TODO: Try remove on upgrade of `chrono-tz` crate.
1717
mod for_minimal_versions_check_only {
@@ -45,9 +45,9 @@ mod tz {
4545
Value::scalar(v.name().to_owned())
4646
}
4747

48-
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<TimeZone, String> {
49-
v.as_string_value()
50-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
48+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<TimeZone, String> {
49+
s.as_str()
50+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
5151
.and_then(|s| {
5252
s.parse::<TimeZone>()
5353
.map_err(|e| format!("Failed to parse `TimeZone`: {e}"))

juniper/src/integrations/jiff.rs

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use std::str;
5454

5555
use derive_more::with_trait::{Debug, Display, Error, Into};
5656

57-
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
57+
use crate::{ScalarValue, Value, graphql_scalar};
5858

5959
/// Representation of a civil date in the Gregorian calendar.
6060
///
@@ -90,12 +90,9 @@ mod local_date {
9090
Value::scalar(v.strftime(FORMAT).to_string())
9191
}
9292

93-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalDate, String>
94-
where
95-
S: ScalarValue,
96-
{
97-
v.as_string_value()
98-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
93+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<LocalDate, String> {
94+
s.as_str()
95+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
9996
.and_then(|s| {
10097
LocalDate::strptime(FORMAT, s).map_err(|e| format!("Invalid `LocalDate`: {e}"))
10198
})
@@ -153,12 +150,9 @@ mod local_time {
153150
)
154151
}
155152

156-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalTime, String>
157-
where
158-
S: ScalarValue,
159-
{
160-
v.as_string_value()
161-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
153+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<LocalTime, String> {
154+
s.as_str()
155+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
162156
.and_then(|s| {
163157
// First, try to parse the most used format.
164158
// At the end, try to parse the full format for the parsing
@@ -207,12 +201,9 @@ mod local_date_time {
207201
Value::scalar(v.strftime(FORMAT).to_string())
208202
}
209203

210-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalDateTime, String>
211-
where
212-
S: ScalarValue,
213-
{
214-
v.as_string_value()
215-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
204+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<LocalDateTime, String> {
205+
s.as_str()
206+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
216207
.and_then(|s| {
217208
LocalDateTime::strptime(FORMAT, s)
218209
.map_err(|e| format!("Invalid `LocalDateTime`: {e}"))
@@ -254,12 +245,9 @@ mod date_time {
254245
Value::scalar(v.strftime(FORMAT).to_string())
255246
}
256247

257-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<DateTime, String>
258-
where
259-
S: ScalarValue,
260-
{
261-
v.as_string_value()
262-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
248+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<DateTime, String> {
249+
s.as_str()
250+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
263251
.and_then(|s| DateTime::from_str(s).map_err(|e| format!("Invalid `DateTime`: {e}")))
264252
}
265253
}
@@ -299,12 +287,9 @@ mod zoned_date_time {
299287
Value::scalar(v.to_string())
300288
}
301289

302-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<ZonedDateTime, String>
303-
where
304-
S: ScalarValue,
305-
{
306-
v.as_string_value()
307-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
290+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<ZonedDateTime, String> {
291+
s.as_str()
292+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
308293
.and_then(|s| {
309294
ZonedDateTime::from_str(s).map_err(|e| format!("Invalid `ZonedDateTime`: {e}"))
310295
})
@@ -341,12 +326,9 @@ mod duration {
341326
Value::scalar(v.to_string())
342327
}
343328

344-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<Duration, String>
345-
where
346-
S: ScalarValue,
347-
{
348-
v.as_string_value()
349-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
329+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<Duration, String> {
330+
s.as_str()
331+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
350332
.and_then(|s| Duration::from_str(s).map_err(|e| format!("Invalid `Duration`: {e}")))
351333
}
352334
}
@@ -395,12 +377,9 @@ mod time_zone_or_utc_offset {
395377
))
396378
}
397379

398-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<TimeZoneOrUtcOffset, String>
399-
where
400-
S: ScalarValue,
401-
{
402-
v.as_string_value()
403-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
380+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<TimeZoneOrUtcOffset, String> {
381+
s.as_str()
382+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
404383
.and_then(|s| {
405384
TimeZoneOrUtcOffset::get(s)
406385
.map_err(TimeZoneParsingError::InvalidTimeZone)
@@ -478,12 +457,9 @@ mod time_zone {
478457
Value::scalar(v.to_string())
479458
}
480459

481-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<TimeZone, String>
482-
where
483-
S: ScalarValue,
484-
{
485-
v.as_string_value()
486-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
460+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<TimeZone, String> {
461+
s.as_str()
462+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
487463
.and_then(|s| s.parse().map_err(|e| format!("Invalid `TimeZone`: {e}")))
488464
}
489465
}
@@ -531,12 +507,9 @@ mod utc_offset {
531507
Value::scalar(buf)
532508
}
533509

534-
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<UtcOffset, String>
535-
where
536-
S: ScalarValue,
537-
{
538-
v.as_string_value()
539-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
510+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<UtcOffset, String> {
511+
s.as_str()
512+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
540513
.and_then(|s| utc_offset_from_str(s).map_err(|e| format!("Invalid `UtcOffset`: {e}")))
541514
}
542515
}

juniper/src/integrations/rust_decimal.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use std::str::FromStr as _;
1212

13-
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
13+
use crate::{ScalarValue, Value, graphql_scalar};
1414

1515
/// 128 bit representation of a fixed-precision decimal number.
1616
///
@@ -40,14 +40,14 @@ mod rust_decimal_scalar {
4040
Value::scalar(v.to_string())
4141
}
4242

43-
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<Decimal, String> {
44-
if let Some(i) = v.as_int_value() {
43+
pub(super) fn from_input(s: &impl ScalarValue) -> Result<Decimal, String> {
44+
if let Some(i) = s.as_int() {
4545
Ok(Decimal::from(i))
46-
} else if let Some(f) = v.as_float_value() {
46+
} else if let Some(f) = s.as_float() {
4747
Decimal::try_from(f).map_err(|e| format!("Failed to parse `Decimal` from `Float`: {e}"))
4848
} else {
49-
v.as_string_value()
50-
.ok_or_else(|| format!("Expected `String`, found: {v}"))
49+
s.as_str()
50+
.ok_or_else(|| format!("Expected `String`, found: {s}"))
5151
.and_then(|s| {
5252
Decimal::from_str(s)
5353
.map_err(|e| format!("Failed to parse `Decimal` from `String`: {e}"))

0 commit comments

Comments
 (0)