@@ -47,67 +47,33 @@ pub trait ParseScalarValue<S = DefaultScalarValue> {
4747/// The preferred way to define a new [`ScalarValue`] representation is defining an enum containing
4848/// a variant for each type that needs to be represented at the lowest level.
4949///
50- /// The following example introduces a new variant that is able to store 64-bit integers, and uses
51- /// a [`CompactString`] for a string representation.
50+ /// The following example introduces a new variant that is able to store 64-bit integers.
5251///
5352/// ```rust
5453/// # use std::{any::Any, fmt};
5554/// #
56- /// # use compact_str::CompactString;
5755/// use derive_more::with_trait::{Display, From, TryInto};
5856/// use juniper::ScalarValue;
5957/// use serde::{de, Deserialize, Deserializer, Serialize};
6058///
6159/// #[derive(Clone, Debug, Display, From, PartialEq, ScalarValue, Serialize, TryInto)]
6260/// #[serde(untagged)]
63- /// #[value(from_displayable_with = from_compact_str)]
6461/// enum MyScalarValue {
65- /// #[from]
6662/// #[value(to_float, to_int)]
6763/// Int(i32),
6864///
69- /// #[from]
7065/// Long(i64),
71- ///
72- /// #[from]
66+ ///
7367/// #[value(to_float)]
7468/// Float(f64),
7569///
76- /// #[from(&str, String, CompactString)]
7770/// #[value(as_str, to_string)]
78- /// String(CompactString),
79- ///
80- /// #[from]
71+ /// String(String),
72+ ///
8173/// #[value(to_bool)]
8274/// Boolean(bool),
8375/// }
8476///
85- /// // Custom implementation of `ScalarValue::from_displayable()` method
86- /// // for efficient conversions from `CompactString` into `MyScalarValue`.
87- /// fn from_compact_str<Str: Display + Any + ?Sized>(s: &Str) -> MyScalarValue {
88- /// use juniper::AnyExt as _; // allows downcasting directly on types without `dyn`
89- ///
90- /// if let Some(s) = s.downcast_ref::<CompactString>() {
91- /// MyScalarValue::String(s.clone())
92- /// } else {
93- /// s.to_string().into()
94- /// }
95- /// }
96- ///
97- /// // `derive_more::TryInto` is not capable for transitive conversions yet,
98- /// // so this impl is manual as a custom string type is used instead of `String`.
99- /// impl TryFrom<MyScalarValue> for String {
100- /// type Error = MyScalarValue;
101- ///
102- /// fn try_from(value: MyScalarValue) -> Result<Self, Self::Error> {
103- /// if let MyScalarValue::String(s) = value {
104- /// Ok(s.into())
105- /// } else {
106- /// Err(value)
107- /// }
108- /// }
109- /// }
110- ///
11177/// impl<'de> Deserialize<'de> for MyScalarValue {
11278/// fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
11379/// struct Visitor;
@@ -165,7 +131,7 @@ pub trait ParseScalarValue<S = DefaultScalarValue> {
165131/// }
166132///
167133/// fn visit_string<E: de::Error>(self, s: String) -> Result<Self::Value, E> {
168- /// Ok(MyScalarValue::String(s.into() ))
134+ /// Ok(MyScalarValue::String(s))
169135/// }
170136/// }
171137///
@@ -711,6 +677,15 @@ impl<'a, S: ScalarValue> IntoFieldError<S> for WrongInputScalarTypeError<'a, S>
711677 }
712678}
713679
680+ /// Conversion of a Rust data type into a [`ScalarValue`].
681+ ///
682+ /// # Implementation
683+ ///
684+ /// Implementing this trait for a type allows to specify this type directly in the `to_output()`
685+ /// function when implementing a [`GraphQLScalar`] via [derive macro](macro@GraphQLScalar).
686+ ///
687+ /// Also, `#[derive(`[`GraphQLScalar`](macro@GraphQLScalar)`)]` automatically implements this trait
688+ /// for a type.
714689pub trait ToScalarValue < S = DefaultScalarValue > {
715690 /// Converts this value into a [`ScalarValue`].
716691 #[ must_use]
0 commit comments