Skip to content

Commit 1ee58fb

Browse files
committed
Strip redundant ScalarValueFmt
1 parent d3e1697 commit 1ee58fb

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

juniper/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use indexmap::IndexMap;
88
use crate::{
99
executor::Variables,
1010
parser::Spanning,
11-
value::{DefaultScalarValue, ScalarValue, ScalarValueFmt},
11+
value::{DefaultScalarValue, Scalar, ScalarValue},
1212
};
1313

1414
/// Type literal in a syntax tree.
@@ -434,7 +434,7 @@ impl<S: ScalarValue> fmt::Display for InputValue<S> {
434434
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
435435
match self {
436436
Self::Null => write!(f, "null"),
437-
Self::Scalar(s) => fmt::Display::fmt(&ScalarValueFmt(s), f),
437+
Self::Scalar(s) => fmt::Display::fmt(<&Scalar<_>>::from(s), f),
438438
Self::Enum(v) => write!(f, "{v}"),
439439
Self::Variable(v) => write!(f, "${v}"),
440440
Self::List(v) => {

juniper/src/value/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{any::TypeId, borrow::Cow, fmt, mem};
66
use arcstr::ArcStr;
77
use compact_str::CompactString;
88

9-
pub(crate) use self::scalar::ScalarValueFmt;
109
pub use self::{
1110
object::Object,
1211
scalar::{
@@ -165,7 +164,7 @@ impl<S: ScalarValue> fmt::Display for Value<S> {
165164
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166165
match self {
167166
Self::Null => write!(f, "null"),
168-
Self::Scalar(s) => fmt::Display::fmt(&ScalarValueFmt(s), f),
167+
Self::Scalar(s) => fmt::Display::fmt(<&Scalar<_>>::from(s), f),
169168
Self::List(list) => {
170169
write!(f, "[")?;
171170
for (idx, item) in list.iter().enumerate() {

juniper/src/value/scalar.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait ParseScalarValue<S = DefaultScalarValue> {
5454
/// # use std::{any::Any, fmt};
5555
/// #
5656
/// # use compact_str::CompactString;
57-
/// use derive_more::{Display, From, TryInto};
57+
/// use derive_more::with_trait::{Display, From, TryInto};
5858
/// use juniper::ScalarValue;
5959
/// use serde::{de, Deserialize, Deserializer, Serialize};
6060
///
@@ -84,7 +84,7 @@ pub trait ParseScalarValue<S = DefaultScalarValue> {
8484
///
8585
/// // Custom implementation of `ScalarValue::from_displayable()` method
8686
/// // for efficient conversions from `CompactString` into `MyScalarValue`.
87-
/// fn from_compact_str<Str: fmt::Display + Any + ?Sized>(s: &Str) -> MyScalarValue {
87+
/// fn from_compact_str<Str: Display + Any + ?Sized>(s: &Str) -> MyScalarValue {
8888
/// use juniper::AnyExt as _; // allows downcasting directly on types without `dyn`
8989
///
9090
/// if let Some(s) = s.downcast_ref::<CompactString>() {
@@ -180,7 +180,7 @@ pub trait ParseScalarValue<S = DefaultScalarValue> {
180180
/// [`Serialize`]: trait@serde::Serialize
181181
pub trait ScalarValue:
182182
fmt::Debug
183-
+ fmt::Display
183+
+ Display
184184
+ PartialEq
185185
+ Clone
186186
+ DeserializeOwned
@@ -474,7 +474,7 @@ pub trait ScalarValue:
474474
}
475475
}
476476

477-
/// Creates this [`ScalarValue`] from the provided [`fmt::Display`] type.
477+
/// Creates this [`ScalarValue`] from the provided [`Display`]able type.
478478
///
479479
/// This method should be implemented if [`ScalarValue`] implementation uses some custom string
480480
/// type inside to enable efficient conversion from values of this type.
@@ -485,7 +485,7 @@ pub trait ScalarValue:
485485
///
486486
/// See the [example in trait documentation](ScalarValue#example) for how it can be used.
487487
#[must_use]
488-
fn from_displayable<Str: fmt::Display + Any + ?Sized>(s: &Str) -> Self {
488+
fn from_displayable<Str: Display + Any + ?Sized>(s: &Str) -> Self {
489489
s.to_string().into()
490490
}
491491
}
@@ -549,13 +549,13 @@ impl<'s, S: ScalarValue> FromScalarValue<'s, S> for &'s Scalar<S> {
549549
type Error = Infallible;
550550

551551
fn from_scalar_value(v: &'s S) -> Result<Self, Self::Error> {
552-
Ok(Scalar::ref_cast(v))
552+
Ok(v.into())
553553
}
554554
}
555555

556556
/// Error of a [`ScalarValue`] not matching the expected type.
557557
#[derive(Clone, Debug, Display, Error)]
558-
#[display("Expected `{type_name}`, found: {}", ScalarValueFmt(*input))]
558+
#[display("Expected `{type_name}`, found: {}", <&Scalar<_>>::from(*input))]
559559
pub struct WrongInputScalarTypeError<'a, S: ScalarValue> {
560560
/// Type name of the expected GraphQL scalar.
561561
pub type_name: ArcStr,
@@ -570,10 +570,22 @@ impl<'a, S: ScalarValue> IntoFieldError<S> for WrongInputScalarTypeError<'a, S>
570570
}
571571
}
572572

573-
/// [`Display`]-formatter for a [`ScalarValue`] to render as a [`Value`].
574-
pub(crate) struct ScalarValueFmt<'a, S: ScalarValue>(pub &'a S);
573+
/// Transparent wrapper over a value, indicating it being a [`ScalarValue`].
574+
///
575+
/// Used in [`GraphQLScalar`] definitions to distinguish a concrete type for a generic
576+
/// [`ScalarValue`], since Rust type inference fail do so for a generic value directly in macro
577+
/// expansions.
578+
#[derive(Debug, Deref, RefCast)]
579+
#[repr(transparent)]
580+
pub struct Scalar<T: ScalarValue>(T);
581+
582+
impl<'a, T: ScalarValue> From<&'a T> for &'a Scalar<T> {
583+
fn from(value: &'a T) -> Self {
584+
Scalar::ref_cast(value)
585+
}
586+
}
575587

576-
impl<'a, S: ScalarValue> Display for ScalarValueFmt<'a, S> {
588+
impl<S: ScalarValue> Display for Scalar<S> {
577589
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
578590
if let Some(s) = self.0.try_as_str() {
579591
write!(f, "\"{s}\"")
@@ -583,16 +595,6 @@ impl<'a, S: ScalarValue> Display for ScalarValueFmt<'a, S> {
583595
}
584596
}
585597

586-
/// Transparent wrapper over a value, indicating it being a [`ScalarValue`].
587-
///
588-
/// Used in [`GraphQLScalar`] definitions to distinguish a concrete type for a generic
589-
/// [`ScalarValue`], since Rust type inference fail do so for a generic value directly in macro
590-
/// expansions.
591-
#[derive(Debug, Deref, Display, RefCast)]
592-
#[display("{}", ScalarValueFmt(_0))]
593-
#[repr(transparent)]
594-
pub struct Scalar<T: ScalarValue>(T);
595-
596598
/// Extension of [`Any`] for using its methods directly on the value without `dyn`.
597599
pub trait AnyExt: Any {
598600
/// Returns `true` if the this type is the same as `T`.

0 commit comments

Comments
 (0)