Skip to content

Commit fea4146

Browse files
committed
Remake to accept ref
1 parent dee7a5b commit fea4146

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

juniper/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ pub use crate::{
103103
},
104104
validation::RuleError,
105105
value::{
106-
AnyExt, DefaultScalarValue, FromScalarValue, IntoValue, Object, ParseScalarResult,
107-
ParseScalarValue, Scalar, ScalarValue, TryToPrimitive, Value, WrongInputScalarTypeError, IntoScalarValue,
106+
AnyExt, DefaultScalarValue, FromScalarValue, ToScalarValue, IntoValue, Object,
107+
ParseScalarResult, ParseScalarValue, Scalar, ScalarValue, TryToPrimitive, Value,
108+
WrongInputScalarTypeError,
108109
},
109110
};
110111

juniper/src/types/pointers.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
async_await::GraphQLValueAsync,
1212
base::{Arguments, GraphQLType, GraphQLValue},
1313
},
14-
value::{FromScalarValue, ScalarValue},
14+
value::{FromScalarValue, ToScalarValue, ScalarValue},
1515
};
1616

1717
impl<S, T> GraphQLType<S> for Box<T>
@@ -99,6 +99,15 @@ where
9999
}
100100
}
101101

102+
impl<T, S> ToScalarValue<S> for Box<T>
103+
where
104+
T: ToScalarValue<S> + ?Sized,
105+
{
106+
fn to_scalar_value(&self) -> S {
107+
(**self).to_scalar_value()
108+
}
109+
}
110+
102111
impl<T, S> FromInputValue<S> for Box<T>
103112
where
104113
S: ScalarValue,
@@ -204,6 +213,15 @@ where
204213
}
205214
}
206215

216+
impl<T, S> ToScalarValue<S> for &T
217+
where
218+
T: ToScalarValue<S> + ?Sized,
219+
{
220+
fn to_scalar_value(&self) -> S {
221+
(**self).to_scalar_value()
222+
}
223+
}
224+
207225
impl<T, S> ToInputValue<S> for &T
208226
where
209227
S: fmt::Debug,
@@ -299,6 +317,15 @@ where
299317
}
300318
}
301319

320+
impl<T, S> ToScalarValue<S> for Arc<T>
321+
where
322+
T: ToScalarValue<S> + ?Sized,
323+
{
324+
fn to_scalar_value(&self) -> S {
325+
(**self).to_scalar_value()
326+
}
327+
}
328+
302329
impl<T, S> FromInputValue<S> for Arc<T>
303330
where
304331
S: ScalarValue,

juniper/src/types/scalars.rs

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
subscriptions::GraphQLSubscriptionValue,
1818
},
1919
value::{
20-
FromScalarValue, ParseScalarResult, ScalarValue, TryToPrimitive, Value,
20+
FromScalarValue, ParseScalarResult, ScalarValue, ToScalarValue, TryToPrimitive, Value,
2121
WrongInputScalarTypeError,
2222
},
2323
};
@@ -76,6 +76,15 @@ mod impl_string_scalar {
7676
}
7777
}
7878

79+
impl<S> ToScalarValue<S> for String
80+
where
81+
Self: Into<S>,
82+
{
83+
fn to_scalar_value(&self) -> S {
84+
self.clone().into()
85+
}
86+
}
87+
7988
pub(super) fn to_output<S: ScalarValue>(v: &str) -> Value<S> {
8089
Value::scalar(v.to_owned())
8190
}
@@ -291,15 +300,6 @@ where
291300
}
292301
}
293302

294-
impl<S> ToInputValue<S> for &str
295-
where
296-
S: ScalarValue,
297-
{
298-
fn to_input_value(&self) -> InputValue<S> {
299-
InputValue::scalar(String::from(*self))
300-
}
301-
}
302-
303303
impl<'s, S> FromScalarValue<'s, S> for &'s str
304304
where
305305
S: TryToPrimitive<'s, Self, Error: IntoFieldError<S>> + 's,
@@ -311,6 +311,21 @@ where
311311
}
312312
}
313313

314+
impl<S: ScalarValue> ToScalarValue<S> for str {
315+
fn to_scalar_value(&self) -> S {
316+
S::from_displayable(self)
317+
}
318+
}
319+
320+
impl<S> ToInputValue<S> for &str
321+
where
322+
str: ToScalarValue<S>,
323+
{
324+
fn to_input_value(&self) -> InputValue<S> {
325+
InputValue::scalar::<S>(self.to_scalar_value())
326+
}
327+
}
328+
314329
#[graphql_scalar]
315330
#[graphql(with = impl_boolean_scalar, from_input_with = __builtin)]
316331
type Boolean = bool;
@@ -329,6 +344,15 @@ mod impl_boolean_scalar {
329344
}
330345
}
331346

347+
impl<S> ToScalarValue<S> for Boolean
348+
where
349+
Self: Into<S>,
350+
{
351+
fn to_scalar_value(&self) -> S {
352+
(*self).into()
353+
}
354+
}
355+
332356
pub(super) fn to_output<S: ScalarValue>(v: &Boolean) -> Value<S> {
333357
Value::scalar(*v)
334358
}
@@ -357,6 +381,15 @@ mod impl_int_scalar {
357381
}
358382
}
359383

384+
impl<S> ToScalarValue<S> for Int
385+
where
386+
Self: Into<S>,
387+
{
388+
fn to_scalar_value(&self) -> S {
389+
(*self).into()
390+
}
391+
}
392+
360393
pub(super) fn to_output<S: ScalarValue>(v: &Int) -> Value<S> {
361394
Value::scalar(*v)
362395
}
@@ -390,6 +423,15 @@ mod impl_float_scalar {
390423
}
391424
}
392425

426+
impl<S> ToScalarValue<S> for Float
427+
where
428+
Self: Into<S>,
429+
{
430+
fn to_scalar_value(&self) -> S {
431+
(*self).into()
432+
}
433+
}
434+
393435
pub(super) fn to_output<S: ScalarValue>(v: &Float) -> Value<S> {
394436
Value::scalar(*v)
395437
}

juniper/src/value/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use compact_str::CompactString;
99
pub use self::{
1010
object::Object,
1111
scalar::{
12-
AnyExt, DefaultScalarValue, FromScalarValue, ParseScalarResult, ParseScalarValue, Scalar,
13-
ScalarValue, TryToPrimitive, WrongInputScalarTypeError, IntoScalarValue,
12+
AnyExt, DefaultScalarValue, FromScalarValue, ToScalarValue, ParseScalarResult,
13+
ParseScalarValue, Scalar, ScalarValue, TryToPrimitive, WrongInputScalarTypeError,
1414
},
1515
};
1616
use crate::{

juniper/src/value/scalar.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use std::{
1010
fmt, ptr,
1111
};
1212

13-
use crate::{FieldError, IntoFieldError, parser::{ParseError, ScalarToken}, Value};
13+
use crate::{
14+
FieldError, IntoFieldError,
15+
parser::{ParseError, ScalarToken},
16+
};
1417
#[cfg(doc)]
1518
use crate::{GraphQLScalar, GraphQLValue, Value};
1619

@@ -567,16 +570,10 @@ impl<'a, S: ScalarValue> IntoFieldError<S> for WrongInputScalarTypeError<'a, S>
567570
}
568571
}
569572

570-
pub trait IntoScalarValue<S = DefaultScalarValue>: Sized {
573+
pub trait ToScalarValue<S = DefaultScalarValue> {
571574
/// Converts this value into a [`ScalarValue`].
572575
#[must_use]
573-
fn into_scalar_value(self) -> S;
574-
}
575-
576-
impl<S: ScalarValue> IntoScalarValue<S> for S {
577-
fn into_scalar_value(self) -> Self {
578-
self
579-
}
576+
fn to_scalar_value(&self) -> S;
580577
}
581578

582579
/// Transparent wrapper over a value, indicating it being a [`ScalarValue`].

0 commit comments

Comments
 (0)