Skip to content

Commit 2dc7d13

Browse files
committed
Evaluate error lazily
1 parent 1fea54e commit 2dc7d13

File tree

7 files changed

+113
-96
lines changed

7 files changed

+113
-96
lines changed

juniper/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,13 @@ impl<'a, S> VariableDefinitions<'a, S> {
609609
}
610610

611611
#[cfg(test)]
612-
mod tests {
612+
mod spec_input_value_fmt {
613613
use crate::graphql_input_value;
614614

615615
use super::InputValue;
616616

617617
#[test]
618-
fn test_input_value_fmt() {
618+
fn correct() {
619619
let value: InputValue = graphql_input_value!(null);
620620
assert_eq!(value.to_string(), "null");
621621

juniper/src/integrations/bigdecimal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ mod bigdecimal_scalar {
5454
.map_err(|e| format!("Failed to parse `BigDecimal` from `Float`: {e}").into())
5555
} else {
5656
v.try_to::<&str>()
57-
.map_err(|e| e.message().into())
57+
.map_err(|e| e.to_string().into())
5858
.and_then(|s| {
5959
BigDecimal::from_str(s).map_err(|e| {
6060
format!("Failed to parse `BigDecimal` from `String`: {e}").into()

juniper/src/integrations/rust_decimal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mod rust_decimal_scalar {
4848
.map_err(|e| format!("Failed to parse `Decimal` from `Float`: {e}").into())
4949
} else {
5050
v.try_to::<&str>()
51-
.map_err(|e| e.message().into())
51+
.map_err(|e| e.to_string().into())
5252
.and_then(|s| {
5353
Decimal::from_str(s)
5454
.map_err(|e| format!("Failed to parse `Decimal` from `String`: {e}").into())

juniper/src/types/pointers.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fmt, sync::Arc};
33
use arcstr::ArcStr;
44

55
use crate::{
6-
BoxFuture, FieldResult,
6+
BoxFuture,
77
ast::{FromInputValue, InputValue, Selection, ToInputValue},
88
executor::{ExecutionResult, Executor, Registry},
99
schema::meta::MetaType,
@@ -92,7 +92,9 @@ where
9292
S: ScalarValue,
9393
T: FromScalarValue<'s, S> + 's,
9494
{
95-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
95+
type Error = T::Error;
96+
97+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
9698
T::from_scalar_value(v).map(Self::new)
9799
}
98100
}
@@ -290,7 +292,9 @@ where
290292
S: ScalarValue,
291293
T: FromScalarValue<'s, S> + 's,
292294
{
293-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
295+
type Error = T::Error;
296+
297+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
294298
T::from_scalar_value(v).map(Self::new)
295299
}
296300
}

juniper/src/types/scalars.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ mod impl_string_scalar {
6969
where
7070
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
7171
{
72-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
72+
type Error = S::Error;
73+
74+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
7375
v.try_scalar_value_to()
74-
.map_err(IntoFieldError::into_field_error)
7576
}
7677
}
7778

@@ -189,13 +190,15 @@ type ArcStr = arcstr::ArcStr;
189190

190191
mod impl_arcstr_scalar {
191192
use super::ArcStr;
192-
use crate::{FieldResult, IntoValue as _, Scalar, ScalarValue, Value};
193+
use crate::{FromScalarValue, IntoValue as _, Scalar, ScalarValue, Value};
193194

194195
pub(super) fn to_output<S: ScalarValue>(v: &ArcStr) -> Value<S> {
195196
v.into_value()
196197
}
197198

198-
pub(super) fn from_input<S: ScalarValue>(v: &Scalar<S>) -> FieldResult<ArcStr, S> {
199+
pub(super) fn from_input<S: ScalarValue>(
200+
v: &Scalar<S>,
201+
) -> Result<ArcStr, <&str as FromScalarValue<S>>::Error> {
199202
if let Some(s) = v.downcast_type::<ArcStr>() {
200203
Ok(s.clone())
201204
} else {
@@ -210,13 +213,15 @@ type CompactString = compact_str::CompactString;
210213

211214
mod impl_compactstring_scalar {
212215
use super::CompactString;
213-
use crate::{FieldResult, IntoValue as _, Scalar, ScalarValue, Value};
216+
use crate::{FromScalarValue, IntoValue as _, Scalar, ScalarValue, Value};
214217

215218
pub(super) fn to_output<S: ScalarValue>(v: &CompactString) -> Value<S> {
216219
v.into_value()
217220
}
218221

219-
pub(super) fn from_input<S: ScalarValue>(v: &Scalar<S>) -> FieldResult<CompactString, S> {
222+
pub(super) fn from_input<S: ScalarValue>(
223+
v: &Scalar<S>,
224+
) -> Result<CompactString, <&str as FromScalarValue<S>>::Error> {
220225
if let Some(s) = v.downcast_type::<CompactString>() {
221226
Ok(s.clone())
222227
} else {
@@ -299,9 +304,10 @@ impl<'s, S> FromScalarValue<'s, S> for &'s str
299304
where
300305
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
301306
{
302-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
307+
type Error = S::Error;
308+
309+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
303310
v.try_scalar_value_to()
304-
.map_err(IntoFieldError::into_field_error)
305311
}
306312
}
307313

@@ -316,9 +322,10 @@ mod impl_boolean_scalar {
316322
where
317323
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
318324
{
319-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
325+
type Error = S::Error;
326+
327+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
320328
v.try_scalar_value_to()
321-
.map_err(IntoFieldError::into_field_error)
322329
}
323330
}
324331

@@ -343,9 +350,10 @@ mod impl_int_scalar {
343350
where
344351
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
345352
{
346-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
353+
type Error = S::Error;
354+
355+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
347356
v.try_scalar_value_to()
348-
.map_err(IntoFieldError::into_field_error)
349357
}
350358
}
351359

@@ -375,9 +383,10 @@ mod impl_float_scalar {
375383
where
376384
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
377385
{
378-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
386+
type Error = S::Error;
387+
388+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
379389
v.try_scalar_value_to()
380-
.map_err(IntoFieldError::into_field_error)
381390
}
382391
}
383392

juniper/src/value/scalar.rs

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ pub trait ScalarValue:
194194
+ for<'a> TryScalarValueTo<'a, f64, Error: Display + IntoFieldError<Self>>
195195
+ for<'a> TryScalarValueTo<'a, String, Error: Display + IntoFieldError<Self>>
196196
+ for<'a> TryScalarValueTo<'a, &'a str, Error: Display + IntoFieldError<Self>>
197-
+ for<'a> TryScalarValueTo<'a, &'a Self, Error: Display + IntoFieldError<Self>>
198197
+ TryInto<String>
199198
+ 'static
200199
{
@@ -248,14 +247,16 @@ pub trait ScalarValue:
248247
///
249248
/// ```rust
250249
/// # use arcstr::ArcStr;
251-
/// # use juniper::{FieldResult, GraphQLScalar, Scalar, ScalarValue, TryScalarValueTo, Value};
250+
/// # use juniper::{FromScalarValue, GraphQLScalar, Scalar, ScalarValue, TryScalarValueTo, Value};
252251
/// #
253252
/// #[derive(GraphQLScalar)]
254253
/// #[graphql(from_input_with = Self::from_input, transparent)]
255254
/// struct Name(ArcStr);
256255
///
257256
/// impl Name {
258-
/// fn from_input<S: ScalarValue>(v: &Scalar<S>) -> FieldResult<Self, S> {
257+
/// fn from_input<S: ScalarValue>(
258+
/// v: &Scalar<S>,
259+
/// ) -> Result<Self, <&str as FromScalarValue<S>>::Error> {
259260
/// // Check if our `ScalarValue` is represented by an `ArcStr` already, and if so,
260261
/// // do the cheap `Clone` instead of allocating a new `ArcStr` in its `From<&str>`
261262
/// // implementation.
@@ -282,7 +283,7 @@ pub trait ScalarValue:
282283
///
283284
/// Implementations should not implement this method, but rather implement the
284285
/// [`TryScalarValueTo<T>`] conversion directly.
285-
fn try_to<'a, T>(&'a self) -> FieldResult<T, Self>
286+
fn try_to<'a, T>(&'a self) -> Result<T, T::Error>
286287
where
287288
T: FromScalarValue<'a, Self> + 'a,
288289
{
@@ -303,7 +304,7 @@ pub trait ScalarValue:
303304
/// [`TryScalarValueTo<bool>`] conversions for all the supported boolean types.
304305
#[must_use]
305306
fn try_to_bool(&self) -> Option<bool> {
306-
self.try_scalar_value_to().ok()
307+
self.try_to().ok()
307308
}
308309

309310
/// Tries to represent this [`ScalarValue`] as an [`i32`] value.
@@ -321,7 +322,7 @@ pub trait ScalarValue:
321322
/// less to an integer, if requested.
322323
#[must_use]
323324
fn try_to_int(&self) -> Option<i32> {
324-
self.try_scalar_value_to().ok()
325+
self.try_to().ok()
325326
}
326327

327328
/// Tries to represent this [`ScalarValue`] as a [`f64`] value.
@@ -339,7 +340,7 @@ pub trait ScalarValue:
339340
/// all floating point values with 64 bit or less to a float, if requested.
340341
#[must_use]
341342
fn try_to_float(&self) -> Option<f64> {
342-
self.try_scalar_value_to().ok()
343+
self.try_to().ok()
343344
}
344345

345346
/// Tries to represent this [`ScalarValue`] as a [`String`] value.
@@ -359,7 +360,7 @@ pub trait ScalarValue:
359360
/// [`TryScalarValueTo<String>`] conversions for all the supported string types, if requested.
360361
#[must_use]
361362
fn try_to_string(&self) -> Option<String> {
362-
self.try_scalar_value_to().ok()
363+
self.try_to().ok()
363364
}
364365

365366
/// Tries to convert this [`ScalarValue`] into a [`String`] value.
@@ -396,7 +397,7 @@ pub trait ScalarValue:
396397
/// requested.
397398
#[must_use]
398399
fn try_as_str(&self) -> Option<&str> {
399-
self.try_scalar_value_to().ok()
400+
self.try_to().ok()
400401
}
401402

402403
/// Converts this [`ScalarValue`] into another one via [`i32`], [`f64`], [`bool`] or [`String`]
@@ -449,7 +450,7 @@ pub trait ScalarValue:
449450
/// all the required primitive types if `#[to_<type>]` and `#[as_<type>]` attributes are specified.
450451
pub trait TryScalarValueTo<'me, T: 'me> {
451452
/// Error if this [`ScalarValue`] doesn't represent the expected type.
452-
type Error;
453+
type Error: 'me;
453454

454455
/// Tries to represent this [`ScalarValue`] as the expected type.
455456
///
@@ -459,47 +460,25 @@ pub trait TryScalarValueTo<'me, T: 'me> {
459460
fn try_scalar_value_to(&'me self) -> Result<T, Self::Error>;
460461
}
461462

462-
impl<'me, S: ScalarValue> TryScalarValueTo<'me, &'me S> for S {
463-
type Error = Infallible;
464-
465-
fn try_scalar_value_to(&'me self) -> Result<&'me S, Self::Error> {
466-
Ok(self)
467-
}
468-
}
469-
470-
impl<'me, S: ScalarValue> TryScalarValueTo<'me, &'me Scalar<S>> for S {
471-
type Error = Infallible;
472-
473-
fn try_scalar_value_to(&'me self) -> Result<&'me Scalar<S>, Self::Error> {
474-
Ok(Scalar::ref_cast(self))
475-
}
476-
}
477-
478-
impl<'me, T, S> TryScalarValueTo<'me, Scalar<T>> for S
479-
where
480-
T: FromScalarValue<'me, S> + 'me,
481-
S: ScalarValue,
482-
{
483-
type Error = FieldError<Self>;
484-
485-
fn try_scalar_value_to(&'me self) -> FieldResult<Scalar<T>, Self> {
486-
T::from_scalar_value(self).map(Scalar)
487-
}
488-
}
489-
490463
pub trait FromScalarValue<'s, S: 's = DefaultScalarValue>: Sized {
464+
type Error: IntoFieldError<S> + 's;
465+
491466
/// Performs the conversion.
492-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S>;
467+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error>;
493468
}
494469

495470
impl<'s, S> FromScalarValue<'s, S> for &'s S {
496-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
471+
type Error = Infallible;
472+
473+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
497474
Ok(v)
498475
}
499476
}
500477

501-
impl<'s, S> FromScalarValue<'s, S> for &'s Scalar<S> {
502-
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
478+
impl<'s, S: ScalarValue> FromScalarValue<'s, S> for &'s Scalar<S> {
479+
type Error = Infallible;
480+
481+
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
503482
Ok(Scalar::ref_cast(v))
504483
}
505484
}
@@ -541,9 +520,8 @@ impl<'a, S: ScalarValue> Display for ScalarValueFmt<'a, S> {
541520
/// expansions.
542521
#[derive(Debug, Deref, Display, RefCast)]
543522
#[display("{}", ScalarValueFmt(_0))]
544-
#[display(bound(T: ScalarValue))]
545523
#[repr(transparent)]
546-
pub struct Scalar<T>(T);
524+
pub struct Scalar<T: ScalarValue>(T);
547525

548526
/// Extension of [`Any`] for using its methods directly on the value without `dyn`.
549527
pub trait AnyExt: Any {

0 commit comments

Comments
 (0)