Skip to content

Commit 1fea54e

Browse files
committed
Upd
1 parent 1226536 commit 1fea54e

File tree

3 files changed

+62
-47
lines changed

3 files changed

+62
-47
lines changed

juniper/src/types/scalars.rs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{char, convert::identity, marker::PhantomData, rc::Rc, thread::JoinHandle};
1+
use std::{char, marker::PhantomData, rc::Rc, thread::JoinHandle};
22

33
use derive_more::with_trait::{Deref, Display, From, Into};
44
use serde::{Deserialize, Serialize};
@@ -65,6 +65,16 @@ type String = std::string::String;
6565
mod impl_string_scalar {
6666
use super::*;
6767

68+
impl<'s, S> FromScalarValue<'s, S> for String
69+
where
70+
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
71+
{
72+
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
73+
v.try_scalar_value_to()
74+
.map_err(IntoFieldError::into_field_error)
75+
}
76+
}
77+
6878
pub(super) fn to_output<S: ScalarValue>(v: &str) -> Value<S> {
6979
Value::scalar(v.to_owned())
7080
}
@@ -173,22 +183,19 @@ where
173183
})
174184
}
175185

176-
177186
#[graphql_scalar]
178187
#[graphql(name = "String", with = impl_arcstr_scalar, parse_token(String))]
179188
type ArcStr = arcstr::ArcStr;
180189

181190
mod impl_arcstr_scalar {
182191
use super::ArcStr;
183-
use crate::{IntoValue as _, Scalar, ScalarValue, Value, FieldResult};
192+
use crate::{FieldResult, IntoValue as _, Scalar, ScalarValue, Value};
184193

185194
pub(super) fn to_output<S: ScalarValue>(v: &ArcStr) -> Value<S> {
186195
v.into_value()
187196
}
188-
189-
pub(super) fn from_input<S: ScalarValue>(
190-
v: &Scalar<S>,
191-
) -> FieldResult<ArcStr, S> {
197+
198+
pub(super) fn from_input<S: ScalarValue>(v: &Scalar<S>) -> FieldResult<ArcStr, S> {
192199
if let Some(s) = v.downcast_type::<ArcStr>() {
193200
Ok(s.clone())
194201
} else {
@@ -203,15 +210,13 @@ type CompactString = compact_str::CompactString;
203210

204211
mod impl_compactstring_scalar {
205212
use super::CompactString;
206-
use crate::{IntoValue as _, Scalar, ScalarValue, FieldResult, Value};
213+
use crate::{FieldResult, IntoValue as _, Scalar, ScalarValue, Value};
207214

208215
pub(super) fn to_output<S: ScalarValue>(v: &CompactString) -> Value<S> {
209216
v.into_value()
210217
}
211218

212-
pub(super) fn from_input<S: ScalarValue>(
213-
v: &Scalar<S>,
214-
) -> FieldResult<CompactString, S> {
219+
pub(super) fn from_input<S: ScalarValue>(v: &Scalar<S>) -> FieldResult<CompactString, S> {
215220
if let Some(s) = v.downcast_type::<CompactString>() {
216221
Ok(s.clone())
217222
} else {
@@ -307,6 +312,16 @@ type Boolean = bool;
307312
mod impl_boolean_scalar {
308313
use super::*;
309314

315+
impl<'s, S> FromScalarValue<'s, S> for Boolean
316+
where
317+
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
318+
{
319+
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
320+
v.try_scalar_value_to()
321+
.map_err(IntoFieldError::into_field_error)
322+
}
323+
}
324+
310325
pub(super) fn to_output<S: ScalarValue>(v: &Boolean) -> Value<S> {
311326
Value::scalar(*v)
312327
}
@@ -324,6 +339,16 @@ type Int = i32;
324339
mod impl_int_scalar {
325340
use super::*;
326341

342+
impl<'s, S> FromScalarValue<'s, S> for Int
343+
where
344+
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
345+
{
346+
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
347+
v.try_scalar_value_to()
348+
.map_err(IntoFieldError::into_field_error)
349+
}
350+
}
351+
327352
pub(super) fn to_output<S: ScalarValue>(v: &Int) -> Value<S> {
328353
Value::scalar(*v)
329354
}
@@ -346,6 +371,16 @@ type Float = f64;
346371
mod impl_float_scalar {
347372
use super::*;
348373

374+
impl<'s, S> FromScalarValue<'s, S> for Float
375+
where
376+
S: TryScalarValueTo<'s, Self, Error: IntoFieldError<S>> + 's,
377+
{
378+
fn from_scalar_value(v: &'s S) -> FieldResult<Self, S> {
379+
v.try_scalar_value_to()
380+
.map_err(IntoFieldError::into_field_error)
381+
}
382+
}
383+
349384
pub(super) fn to_output<S: ScalarValue>(v: &Float) -> Value<S> {
350385
Value::scalar(*v)
351386
}

juniper/src/value/scalar.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,7 @@ pub trait ScalarValue:
270270
#[must_use]
271271
fn downcast_type<T: Any>(&self) -> Option<&T>;
272272

273-
/// Tries to represent this [`ScalarValue`] as the specified type `T`.
274-
///
275-
/// This method could be used instead of other helpers in case the [`TryScalarValueTo::Error`]
276-
/// is needed.
277-
///
278-
/// # Implementation
279-
///
280-
/// This method is an ergonomic alias for the [`TryScalarValueTo<T>`] conversion.
281-
///
282-
/// Implementations should not implement this method, but rather implement the
283-
/// [`TryScalarValueTo<T>`] conversion directly.
284-
fn try_to2<'a, T>(&'a self) -> Result<T, <Self as TryScalarValueTo<'a, T>>::Error>
285-
where
286-
T: 'a,
287-
Self: TryScalarValueTo<'a, T, Error: IntoFieldError<Self>>,
288-
{
289-
self.try_scalar_value_to()
290-
}
291-
273+
/// TODO: renew docs
292274
/// Tries to represent this [`ScalarValue`] as the specified type `T`.
293275
///
294276
/// This method could be used instead of other helpers in case the [`TryScalarValueTo::Error`]

juniper_codegen/src/graphql_scalar/mod.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ impl Attr {
250250
if attr.description.is_none() {
251251
attr.description = Description::parse_from_doc_attrs(attrs)?;
252252
}
253+
254+
// TODO: check whether `__builtin` is from inside `juniper` crate only.
253255

254256
Ok(attr)
255257
}
@@ -492,7 +494,7 @@ impl Definition {
492494
///
493495
/// [`FromScalarValue`]: juniper::FromScalarValue
494496
/// [1]: https://spec.graphql.org/October2021#sec-Scalars
495-
fn impl_from_scalar_value_tokens(&self) -> TokenStream {
497+
fn impl_from_scalar_value_tokens(&self) -> Option<TokenStream> {
496498
let scalar = &self.scalar;
497499

498500
let ref_lt = quote! { '___a };
@@ -509,20 +511,16 @@ impl Definition {
509511
..
510512
} => {
511513
if from_input == &parse_quote! { __builtin } {
512-
quote! {
513-
::juniper::TryScalarValueTo::try_scalar_value_to(input)
514-
.map_err(::juniper::executor::IntoFieldError::<#scalar>::into_field_error)
515-
}
516-
} else {
517-
quote! {
518-
use ::juniper::macros::helper::ToResultCall as _;
519-
520-
let input = ::juniper::ScalarValue::try_to(input)?;
521-
let func: fn(_) -> _ = #from_input;
522-
(&&func)
523-
.__to_result_call(input)
524-
.map_err(::juniper::executor::IntoFieldError::<#scalar>::into_field_error)
525-
}
514+
return None;
515+
}
516+
quote! {
517+
use ::juniper::macros::helper::ToResultCall as _;
518+
519+
let input = ::juniper::ScalarValue::try_to(input)?;
520+
let func: fn(_) -> _ = #from_input;
521+
(&&func)
522+
.__to_result_call(input)
523+
.map_err(::juniper::executor::IntoFieldError::<#scalar>::into_field_error)
526524
}
527525
}
528526
Methods::Delegated { field, .. } => {
@@ -542,7 +540,7 @@ impl Definition {
542540

543541
let (lt_impl_gens, _, where_clause) = generics.split_for_impl();
544542

545-
quote! {
543+
Some(quote! {
546544
#[automatically_derived]
547545
impl #lt_impl_gens ::juniper::FromScalarValue<#ref_lt, #scalar> for #ty #where_clause {
548546
fn from_scalar_value(
@@ -551,7 +549,7 @@ impl Definition {
551549
#body
552550
}
553551
}
554-
}
552+
})
555553
}
556554

557555
/// Returns generated code implementing [`FromInputValue`] trait for this

0 commit comments

Comments
 (0)