Skip to content

Commit 46e7b2c

Browse files
committed
Hm...
1 parent 3bf5565 commit 46e7b2c

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

juniper/src/types/scalars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use derive_more::with_trait::{Deref, Display, From, Into};
44
use serde::{Deserialize, Serialize};
55

66
use crate::{
7-
GraphQLScalar, Scalar,
7+
IntoFieldError,FieldResult, GraphQLScalar, Scalar,
88
ast::{InputValue, Selection, ToInputValue},
99
executor::{ExecutionResult, Executor, Registry},
1010
graphql_scalar,
@@ -16,7 +16,7 @@ use crate::{
1616
base::{GraphQLType, GraphQLValue},
1717
subscriptions::GraphQLSubscriptionValue,
1818
},
19-
value::{ParseScalarResult, ScalarValue, Value, WrongInputScalarTypeError},
19+
value::{FromScalarValue, ParseScalarResult, ScalarValue, Value, WrongInputScalarTypeError},
2020
};
2121

2222
/// An ID as defined by the GraphQL specification

juniper/src/value/scalar.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,33 @@ pub trait ScalarValue:
283283
///
284284
/// Implementations should not implement this method, but rather implement the
285285
/// [`TryScalarValueTo<T>`] conversion directly.
286-
fn try_to<'a, T>(&'a self) -> Result<T, <Self as TryScalarValueTo<'a, T>>::Error>
286+
fn try_to2<'a, T>(&'a self) -> Result<T, <Self as TryScalarValueTo<'a, T>>::Error>
287287
where
288288
T: 'a,
289289
Self: TryScalarValueTo<'a, T, Error: IntoFieldError<Self>>,
290290
{
291291
self.try_scalar_value_to()
292292
}
293293

294+
/// Tries to represent this [`ScalarValue`] as the specified type `T`.
295+
///
296+
/// This method could be used instead of other helpers in case the [`TryScalarValueTo::Error`]
297+
/// is needed.
298+
///
299+
/// # Implementation
300+
///
301+
/// This method is an ergonomic alias for the [`TryScalarValueTo<T>`] conversion.
302+
///
303+
/// Implementations should not implement this method, but rather implement the
304+
/// [`TryScalarValueTo<T>`] conversion directly.
305+
fn try_to<'a, T>(&'a self) -> Result<T, <Self as TryScalarValueTo<'a, Scalar<T>>>::Error>
306+
where
307+
T: 'a,
308+
Self: TryScalarValueTo<'a, Scalar<T>, Error: IntoFieldError<Self>>,
309+
{
310+
self.try_scalar_value_to().map(|s: Scalar<T>| s.0)
311+
}
312+
294313
/// Tries to represent this [`ScalarValue`] as a [`bool`] value.
295314
///
296315
/// Use the [`ScalarValue::try_to::<bool>()`] method in case the [`TryScalarValueTo::Error`] is
@@ -477,6 +496,22 @@ impl<'me, S: ScalarValue> TryScalarValueTo<'me, &'me Scalar<S>> for S {
477496
}
478497
}
479498

499+
impl<'me, S: ScalarValue> TryScalarValueTo<'me, Scalar<&'me str>> for S {
500+
type Error = FieldError<Self>;
501+
502+
fn try_scalar_value_to(&'me self) -> FieldResult<Scalar<&'me str>, Self> {
503+
self.try_scalar_value_to().map(Scalar)
504+
}
505+
}
506+
507+
impl<'me, T: FromScalarValue<S> + 'me, S: ScalarValue> TryScalarValueTo<'me, Scalar<T>> for S {
508+
type Error = FieldError<Self>;
509+
510+
fn try_scalar_value_to(&'me self) -> FieldResult<Scalar<T>, Self> {
511+
T::from_scalar_value(self).map(Scalar)
512+
}
513+
}
514+
480515
pub trait FromScalarValue<S = DefaultScalarValue>: Sized {
481516
/// Performs the conversion.
482517
fn from_scalar_value(v: &S) -> FieldResult<Self, S>;
@@ -519,8 +554,9 @@ impl<'a, S: ScalarValue> Display for ScalarValueFmt<'a, S> {
519554
/// expansions.
520555
#[derive(Debug, Deref, Display, RefCast)]
521556
#[display("{}", ScalarValueFmt(_0))]
557+
#[display(bound(T: ScalarValue))]
522558
#[repr(transparent)]
523-
pub struct Scalar<T: ScalarValue>(T);
559+
pub struct Scalar<T>(T);
524560

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

0 commit comments

Comments
 (0)