async_graphql_value/
lib.rs

1//! Value for GraphQL. Used in the [`async-graphql`](https://crates.io/crates/async-graphql) crate.
2
3#![warn(missing_docs)]
4#![allow(clippy::uninlined_format_args)]
5#![forbid(unsafe_code)]
6
7mod deserializer;
8mod extensions;
9mod macros;
10mod serializer;
11mod value_serde;
12mod variables;
13
14use std::{
15    borrow::{Borrow, Cow},
16    fmt::{self, Display, Formatter, Write},
17    ops::Deref,
18    sync::Arc,
19};
20
21use bytes::Bytes;
22pub use deserializer::{DeserializerError, from_value};
23pub use extensions::Extensions;
24#[doc(hidden)]
25pub use indexmap;
26use indexmap::IndexMap;
27use serde::{Deserialize, Deserializer, Serialize, Serializer};
28pub use serde_json::Number;
29pub use serializer::{SerializerError, to_value};
30#[cfg(feature = "raw_value")]
31pub use value_serde::RAW_VALUE_TOKEN;
32pub use variables::Variables;
33
34/// A GraphQL name.
35///
36/// [Reference](https://spec.graphql.org/June2018/#Name).
37#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
38pub struct Name(Arc<str>);
39
40impl Serialize for Name {
41    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
42        serializer.serialize_str(&self.0)
43    }
44}
45
46impl Name {
47    /// Create a new name.
48    pub fn new(name: impl AsRef<str>) -> Self {
49        Self(name.as_ref().into())
50    }
51
52    /// Get the name as a string.
53    #[must_use]
54    pub fn as_str(&self) -> &str {
55        &self.0
56    }
57}
58
59impl AsRef<str> for Name {
60    fn as_ref(&self) -> &str {
61        &self.0
62    }
63}
64
65impl Borrow<str> for Name {
66    fn borrow(&self) -> &str {
67        &self.0
68    }
69}
70
71impl Deref for Name {
72    type Target = str;
73
74    fn deref(&self) -> &Self::Target {
75        &self.0
76    }
77}
78
79impl Display for Name {
80    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
81        Display::fmt(&self.0, f)
82    }
83}
84
85impl PartialEq<String> for Name {
86    fn eq(&self, other: &String) -> bool {
87        self.as_str() == other
88    }
89}
90impl PartialEq<str> for Name {
91    fn eq(&self, other: &str) -> bool {
92        self.as_str() == other
93    }
94}
95impl PartialEq<Name> for String {
96    fn eq(&self, other: &Name) -> bool {
97        self == other.as_str()
98    }
99}
100impl PartialEq<Name> for str {
101    fn eq(&self, other: &Name) -> bool {
102        other == self
103    }
104}
105impl<'a> PartialEq<&'a str> for Name {
106    fn eq(&self, other: &&'a str) -> bool {
107        self == *other
108    }
109}
110impl PartialEq<Name> for &'_ str {
111    fn eq(&self, other: &Name) -> bool {
112        other == self
113    }
114}
115
116impl<'de> Deserialize<'de> for Name {
117    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
118        Ok(Self(
119            String::deserialize(deserializer)?.into_boxed_str().into(),
120        ))
121    }
122}
123
124/// A resolved GraphQL value, for example `1` or `"Hello World!"`.
125///
126/// It can be serialized and deserialized. Enums will be converted to strings.
127/// Attempting to serialize `Upload` will fail, and `Enum` and `Upload` cannot
128/// be deserialized.
129///
130/// [Reference](https://spec.graphql.org/June2018/#Value).
131#[derive(Clone, Debug, Default, Eq)]
132pub enum ConstValue {
133    /// `null`.
134    #[default]
135    Null,
136    /// A number.
137    Number(Number),
138    /// A string.
139    String(String),
140    /// A boolean.
141    Boolean(bool),
142    /// A binary.
143    Binary(Bytes),
144    /// An enum. These are typically in `SCREAMING_SNAKE_CASE`.
145    Enum(Name),
146    /// A list of values.
147    List(Vec<ConstValue>),
148    /// An object. This is a map of keys to values.
149    Object(IndexMap<Name, ConstValue>),
150}
151
152impl PartialEq for ConstValue {
153    fn eq(&self, other: &ConstValue) -> bool {
154        match (self, other) {
155            (ConstValue::Null, ConstValue::Null) => true,
156            (ConstValue::Number(a), ConstValue::Number(b)) => a == b,
157            (ConstValue::Boolean(a), ConstValue::Boolean(b)) => a == b,
158            (ConstValue::String(a), ConstValue::String(b)) => a == b,
159            (ConstValue::Enum(a), ConstValue::String(b)) => a == b,
160            (ConstValue::String(a), ConstValue::Enum(b)) => a == b,
161            (ConstValue::Enum(a), ConstValue::Enum(b)) => a == b,
162            (ConstValue::Binary(a), ConstValue::Binary(b)) => a == b,
163            (ConstValue::List(a), ConstValue::List(b)) => {
164                if a.len() != b.len() {
165                    return false;
166                }
167                a.iter().zip(b.iter()).all(|(a, b)| a == b)
168            }
169            (ConstValue::Object(a), ConstValue::Object(b)) => {
170                if a.len() != b.len() {
171                    return false;
172                }
173                for (a_key, a_value) in a.iter() {
174                    if let Some(b_value) = b.get(a_key.as_str()) {
175                        if b_value != a_value {
176                            return false;
177                        }
178                    } else {
179                        return false;
180                    }
181                }
182
183                true
184            }
185            _ => false,
186        }
187    }
188}
189
190impl From<()> for ConstValue {
191    fn from((): ()) -> Self {
192        ConstValue::Null
193    }
194}
195
196macro_rules! from_integer {
197    ($($ty:ident),*) => {
198        $(
199            impl From<$ty> for ConstValue {
200                #[inline]
201                fn from(n: $ty) -> Self {
202                    ConstValue::Number(n.into())
203                }
204            }
205        )*
206    };
207}
208
209from_integer!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
210
211impl From<f32> for ConstValue {
212    #[inline]
213    fn from(f: f32) -> Self {
214        From::from(f as f64)
215    }
216}
217
218impl From<f64> for ConstValue {
219    #[inline]
220    fn from(f: f64) -> Self {
221        Number::from_f64(f).map_or(ConstValue::Null, ConstValue::Number)
222    }
223}
224
225impl From<bool> for ConstValue {
226    #[inline]
227    fn from(value: bool) -> Self {
228        ConstValue::Boolean(value)
229    }
230}
231
232impl From<String> for ConstValue {
233    #[inline]
234    fn from(value: String) -> Self {
235        ConstValue::String(value)
236    }
237}
238
239impl From<&String> for ConstValue {
240    #[inline]
241    fn from(value: &String) -> Self {
242        ConstValue::String(value.clone())
243    }
244}
245
246impl From<Name> for ConstValue {
247    #[inline]
248    fn from(value: Name) -> Self {
249        ConstValue::Enum(value)
250    }
251}
252
253impl<'a> From<&'a str> for ConstValue {
254    #[inline]
255    fn from(value: &'a str) -> Self {
256        ConstValue::String(value.into())
257    }
258}
259
260impl<'a> From<Cow<'a, str>> for ConstValue {
261    #[inline]
262    fn from(f: Cow<'a, str>) -> Self {
263        ConstValue::String(f.into_owned())
264    }
265}
266
267impl<T: Into<ConstValue>> FromIterator<T> for ConstValue {
268    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
269        ConstValue::List(iter.into_iter().map(Into::into).collect())
270    }
271}
272
273impl<'a, T: Clone + Into<ConstValue>> From<&'a [T]> for ConstValue {
274    fn from(f: &'a [T]) -> Self {
275        ConstValue::List(f.iter().cloned().map(Into::into).collect())
276    }
277}
278
279impl<T: Into<ConstValue>> From<Vec<T>> for ConstValue {
280    fn from(f: Vec<T>) -> Self {
281        ConstValue::List(f.into_iter().map(Into::into).collect())
282    }
283}
284
285impl From<IndexMap<Name, ConstValue>> for ConstValue {
286    fn from(f: IndexMap<Name, ConstValue>) -> Self {
287        ConstValue::Object(f)
288    }
289}
290
291impl ConstValue {
292    /// Convert this `ConstValue` into a `Value`.
293    #[must_use]
294    pub fn into_value(self) -> Value {
295        match self {
296            Self::Null => Value::Null,
297            Self::Number(num) => Value::Number(num),
298            Self::String(s) => Value::String(s),
299            Self::Boolean(b) => Value::Boolean(b),
300            Self::Binary(bytes) => Value::Binary(bytes),
301            Self::Enum(v) => Value::Enum(v),
302            Self::List(items) => {
303                Value::List(items.into_iter().map(ConstValue::into_value).collect())
304            }
305            Self::Object(map) => Value::Object(
306                map.into_iter()
307                    .map(|(key, value)| (key, value.into_value()))
308                    .collect(),
309            ),
310        }
311    }
312
313    /// Attempt to convert the value into JSON. This is equivalent to the
314    /// `TryFrom` implementation.
315    ///
316    /// # Errors
317    ///
318    /// Fails if serialization fails (see enum docs for more info).
319    pub fn into_json(self) -> serde_json::Result<serde_json::Value> {
320        self.try_into()
321    }
322
323    /// Attempt to convert JSON into a value. This is equivalent to the
324    /// `TryFrom` implementation.
325    ///
326    /// # Errors
327    ///
328    /// Fails if deserialization fails (see enum docs for more info).
329    pub fn from_json(json: serde_json::Value) -> serde_json::Result<Self> {
330        json.try_into()
331    }
332}
333
334impl Display for ConstValue {
335    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
336        match self {
337            Self::Number(num) => write!(f, "{}", *num),
338            Self::String(val) => write_quoted(val, f),
339            Self::Boolean(true) => f.write_str("true"),
340            Self::Boolean(false) => f.write_str("false"),
341            Self::Binary(bytes) => write_binary(bytes, f),
342            Self::Null => f.write_str("null"),
343            Self::Enum(name) => f.write_str(name),
344            Self::List(items) => write_list(items, f),
345            Self::Object(map) => write_object(map, f),
346        }
347    }
348}
349
350impl TryFrom<serde_json::Value> for ConstValue {
351    type Error = serde_json::Error;
352    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
353        Self::deserialize(value)
354    }
355}
356
357impl TryFrom<ConstValue> for serde_json::Value {
358    type Error = serde_json::Error;
359    fn try_from(value: ConstValue) -> Result<Self, Self::Error> {
360        serde_json::to_value(value)
361    }
362}
363
364/// A GraphQL value, for example `1`, `$name` or `"Hello World!"`. This is
365/// [`ConstValue`](enum.ConstValue.html) with variables.
366///
367/// It can be serialized and deserialized. Enums will be converted to strings.
368/// Attempting to serialize `Upload` or `Variable` will fail, and `Enum`,
369/// `Upload` and `Variable` cannot be deserialized.
370///
371/// [Reference](https://spec.graphql.org/June2018/#Value).
372#[derive(Clone, Debug, Default, PartialEq, Eq)]
373pub enum Value {
374    /// A variable, without the `$`.
375    Variable(Name),
376    /// `null`.
377    #[default]
378    Null,
379    /// A number.
380    Number(Number),
381    /// A string.
382    String(String),
383    /// A boolean.
384    Boolean(bool),
385    /// A binary.
386    Binary(Bytes),
387    /// An enum. These are typically in `SCREAMING_SNAKE_CASE`.
388    Enum(Name),
389    /// A list of values.
390    List(Vec<Value>),
391    /// An object. This is a map of keys to values.
392    Object(IndexMap<Name, Value>),
393}
394
395impl Value {
396    /// Attempt to convert the value into a const value by using a function to
397    /// get a variable.
398    pub fn into_const_with<E>(
399        self,
400        mut f: impl FnMut(Name) -> Result<ConstValue, E>,
401    ) -> Result<ConstValue, E> {
402        self.into_const_with_mut(&mut f)
403    }
404
405    fn into_const_with_mut<E>(
406        self,
407        f: &mut impl FnMut(Name) -> Result<ConstValue, E>,
408    ) -> Result<ConstValue, E> {
409        Ok(match self {
410            Self::Variable(name) => f(name)?,
411            Self::Null => ConstValue::Null,
412            Self::Number(num) => ConstValue::Number(num),
413            Self::String(s) => ConstValue::String(s),
414            Self::Boolean(b) => ConstValue::Boolean(b),
415            Self::Binary(v) => ConstValue::Binary(v),
416            Self::Enum(v) => ConstValue::Enum(v),
417            Self::List(items) => ConstValue::List(
418                items
419                    .into_iter()
420                    .map(|value| value.into_const_with_mut(f))
421                    .collect::<Result<_, _>>()?,
422            ),
423            Self::Object(map) => ConstValue::Object(
424                map.into_iter()
425                    .map(|(key, value)| Ok((key, value.into_const_with_mut(f)?)))
426                    .collect::<Result<_, _>>()?,
427            ),
428        })
429    }
430
431    /// Attempt to convert the value into a const value.
432    ///
433    /// Will fail if the value contains variables.
434    #[must_use]
435    pub fn into_const(self) -> Option<ConstValue> {
436        self.into_const_with(|_| Err(())).ok()
437    }
438
439    /// Attempt to convert the value into JSON. This is equivalent to the
440    /// `TryFrom` implementation.
441    ///
442    /// # Errors
443    ///
444    /// Fails if serialization fails (see enum docs for more info).
445    pub fn into_json(self) -> serde_json::Result<serde_json::Value> {
446        self.try_into()
447    }
448
449    /// Attempt to convert JSON into a value. This is equivalent to the
450    /// `TryFrom` implementation.
451    ///
452    /// # Errors
453    ///
454    /// Fails if deserialization fails (see enum docs for more info).
455    pub fn from_json(json: serde_json::Value) -> serde_json::Result<Self> {
456        json.try_into()
457    }
458}
459
460impl Display for Value {
461    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
462        match self {
463            Self::Variable(name) => write!(f, "${}", name),
464            Self::Number(num) => write!(f, "{}", *num),
465            Self::String(val) => write_quoted(val, f),
466            Self::Boolean(true) => f.write_str("true"),
467            Self::Boolean(false) => f.write_str("false"),
468            Self::Binary(bytes) => write_binary(bytes, f),
469            Self::Null => f.write_str("null"),
470            Self::Enum(name) => f.write_str(name),
471            Self::List(items) => write_list(items, f),
472            Self::Object(map) => write_object(map, f),
473        }
474    }
475}
476
477impl From<ConstValue> for Value {
478    fn from(value: ConstValue) -> Self {
479        value.into_value()
480    }
481}
482
483impl TryFrom<serde_json::Value> for Value {
484    type Error = serde_json::Error;
485    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
486        Self::deserialize(value)
487    }
488}
489impl TryFrom<Value> for serde_json::Value {
490    type Error = serde_json::Error;
491    fn try_from(value: Value) -> Result<Self, Self::Error> {
492        serde_json::to_value(value)
493    }
494}
495
496fn write_quoted(s: &str, f: &mut Formatter<'_>) -> fmt::Result {
497    f.write_char('"')?;
498    for c in s.chars() {
499        match c {
500            '\r' => f.write_str("\\r"),
501            '\n' => f.write_str("\\n"),
502            '\t' => f.write_str("\\t"),
503            '"' => f.write_str("\\\""),
504            '\\' => f.write_str("\\\\"),
505            c if c.is_control() => write!(f, "\\u{:04}", c as u32),
506            c => f.write_char(c),
507        }?
508    }
509    f.write_char('"')
510}
511
512fn write_binary(bytes: &[u8], f: &mut Formatter<'_>) -> fmt::Result {
513    f.write_char('[')?;
514    let mut iter = bytes.iter().copied();
515    if let Some(value) = iter.next() {
516        value.fmt(f)?;
517    }
518    for value in iter {
519        f.write_str(", ")?;
520        value.fmt(f)?;
521    }
522    f.write_char(']')
523}
524
525fn write_list<T: Display>(list: impl IntoIterator<Item = T>, f: &mut Formatter<'_>) -> fmt::Result {
526    f.write_char('[')?;
527    let mut iter = list.into_iter();
528    if let Some(item) = iter.next() {
529        item.fmt(f)?;
530    }
531    for item in iter {
532        f.write_str(", ")?;
533        item.fmt(f)?;
534    }
535    f.write_char(']')
536}
537
538fn write_object<K: Display, V: Display>(
539    object: impl IntoIterator<Item = (K, V)>,
540    f: &mut Formatter<'_>,
541) -> fmt::Result {
542    f.write_char('{')?;
543    let mut iter = object.into_iter();
544    if let Some((name, value)) = iter.next() {
545        write!(f, "{}: {}", name, value)?;
546    }
547    for (name, value) in iter {
548        f.write_str(", ")?;
549        write!(f, "{}: {}", name, value)?;
550    }
551    f.write_char('}')
552}