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::{from_value, DeserializerError};
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::{to_value, SerializerError};
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, Eq)]
132pub enum ConstValue {
133    /// `null`.
134    Null,
135    /// A number.
136    Number(Number),
137    /// A string.
138    String(String),
139    /// A boolean.
140    Boolean(bool),
141    /// A binary.
142    Binary(Bytes),
143    /// An enum. These are typically in `SCREAMING_SNAKE_CASE`.
144    Enum(Name),
145    /// A list of values.
146    List(Vec<ConstValue>),
147    /// An object. This is a map of keys to values.
148    Object(IndexMap<Name, ConstValue>),
149}
150
151impl PartialEq for ConstValue {
152    fn eq(&self, other: &ConstValue) -> bool {
153        match (self, other) {
154            (ConstValue::Null, ConstValue::Null) => true,
155            (ConstValue::Number(a), ConstValue::Number(b)) => a == b,
156            (ConstValue::Boolean(a), ConstValue::Boolean(b)) => a == b,
157            (ConstValue::String(a), ConstValue::String(b)) => a == b,
158            (ConstValue::Enum(a), ConstValue::String(b)) => a == b,
159            (ConstValue::String(a), ConstValue::Enum(b)) => a == b,
160            (ConstValue::Enum(a), ConstValue::Enum(b)) => a == b,
161            (ConstValue::Binary(a), ConstValue::Binary(b)) => a == b,
162            (ConstValue::List(a), ConstValue::List(b)) => {
163                if a.len() != b.len() {
164                    return false;
165                }
166                a.iter().zip(b.iter()).all(|(a, b)| a == b)
167            }
168            (ConstValue::Object(a), ConstValue::Object(b)) => {
169                if a.len() != b.len() {
170                    return false;
171                }
172                for (a_key, a_value) in a.iter() {
173                    if let Some(b_value) = b.get(a_key.as_str()) {
174                        if b_value != a_value {
175                            return false;
176                        }
177                    } else {
178                        return false;
179                    }
180                }
181
182                true
183            }
184            _ => false,
185        }
186    }
187}
188
189impl From<()> for ConstValue {
190    fn from((): ()) -> Self {
191        ConstValue::Null
192    }
193}
194
195macro_rules! from_integer {
196    ($($ty:ident),*) => {
197        $(
198            impl From<$ty> for ConstValue {
199                #[inline]
200                fn from(n: $ty) -> Self {
201                    ConstValue::Number(n.into())
202                }
203            }
204        )*
205    };
206}
207
208from_integer!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
209
210impl From<f32> for ConstValue {
211    #[inline]
212    fn from(f: f32) -> Self {
213        From::from(f as f64)
214    }
215}
216
217impl From<f64> for ConstValue {
218    #[inline]
219    fn from(f: f64) -> Self {
220        Number::from_f64(f).map_or(ConstValue::Null, ConstValue::Number)
221    }
222}
223
224impl From<bool> for ConstValue {
225    #[inline]
226    fn from(value: bool) -> Self {
227        ConstValue::Boolean(value)
228    }
229}
230
231impl From<String> for ConstValue {
232    #[inline]
233    fn from(value: String) -> Self {
234        ConstValue::String(value)
235    }
236}
237
238impl From<&String> for ConstValue {
239    #[inline]
240    fn from(value: &String) -> Self {
241        ConstValue::String(value.clone())
242    }
243}
244
245impl From<Name> for ConstValue {
246    #[inline]
247    fn from(value: Name) -> Self {
248        ConstValue::Enum(value)
249    }
250}
251
252impl<'a> From<&'a str> for ConstValue {
253    #[inline]
254    fn from(value: &'a str) -> Self {
255        ConstValue::String(value.into())
256    }
257}
258
259impl<'a> From<Cow<'a, str>> for ConstValue {
260    #[inline]
261    fn from(f: Cow<'a, str>) -> Self {
262        ConstValue::String(f.into_owned())
263    }
264}
265
266impl<T: Into<ConstValue>> FromIterator<T> for ConstValue {
267    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
268        ConstValue::List(iter.into_iter().map(Into::into).collect())
269    }
270}
271
272impl<'a, T: Clone + Into<ConstValue>> From<&'a [T]> for ConstValue {
273    fn from(f: &'a [T]) -> Self {
274        ConstValue::List(f.iter().cloned().map(Into::into).collect())
275    }
276}
277
278impl<T: Into<ConstValue>> From<Vec<T>> for ConstValue {
279    fn from(f: Vec<T>) -> Self {
280        ConstValue::List(f.into_iter().map(Into::into).collect())
281    }
282}
283
284impl From<IndexMap<Name, ConstValue>> for ConstValue {
285    fn from(f: IndexMap<Name, ConstValue>) -> Self {
286        ConstValue::Object(f)
287    }
288}
289
290impl ConstValue {
291    /// Convert this `ConstValue` into a `Value`.
292    #[must_use]
293    pub fn into_value(self) -> Value {
294        match self {
295            Self::Null => Value::Null,
296            Self::Number(num) => Value::Number(num),
297            Self::String(s) => Value::String(s),
298            Self::Boolean(b) => Value::Boolean(b),
299            Self::Binary(bytes) => Value::Binary(bytes),
300            Self::Enum(v) => Value::Enum(v),
301            Self::List(items) => {
302                Value::List(items.into_iter().map(ConstValue::into_value).collect())
303            }
304            Self::Object(map) => Value::Object(
305                map.into_iter()
306                    .map(|(key, value)| (key, value.into_value()))
307                    .collect(),
308            ),
309        }
310    }
311
312    /// Attempt to convert the value into JSON. This is equivalent to the
313    /// `TryFrom` implementation.
314    ///
315    /// # Errors
316    ///
317    /// Fails if serialization fails (see enum docs for more info).
318    pub fn into_json(self) -> serde_json::Result<serde_json::Value> {
319        self.try_into()
320    }
321
322    /// Attempt to convert JSON into a value. This is equivalent to the
323    /// `TryFrom` implementation.
324    ///
325    /// # Errors
326    ///
327    /// Fails if deserialization fails (see enum docs for more info).
328    pub fn from_json(json: serde_json::Value) -> serde_json::Result<Self> {
329        json.try_into()
330    }
331}
332
333impl Default for ConstValue {
334    fn default() -> Self {
335        Self::Null
336    }
337}
338
339impl Display for ConstValue {
340    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
341        match self {
342            Self::Number(num) => write!(f, "{}", *num),
343            Self::String(val) => write_quoted(val, f),
344            Self::Boolean(true) => f.write_str("true"),
345            Self::Boolean(false) => f.write_str("false"),
346            Self::Binary(bytes) => write_binary(bytes, f),
347            Self::Null => f.write_str("null"),
348            Self::Enum(name) => f.write_str(name),
349            Self::List(items) => write_list(items, f),
350            Self::Object(map) => write_object(map, f),
351        }
352    }
353}
354
355impl TryFrom<serde_json::Value> for ConstValue {
356    type Error = serde_json::Error;
357    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
358        Self::deserialize(value)
359    }
360}
361
362impl TryFrom<ConstValue> for serde_json::Value {
363    type Error = serde_json::Error;
364    fn try_from(value: ConstValue) -> Result<Self, Self::Error> {
365        serde_json::to_value(value)
366    }
367}
368
369/// A GraphQL value, for example `1`, `$name` or `"Hello World!"`. This is
370/// [`ConstValue`](enum.ConstValue.html) with variables.
371///
372/// It can be serialized and deserialized. Enums will be converted to strings.
373/// Attempting to serialize `Upload` or `Variable` will fail, and `Enum`,
374/// `Upload` and `Variable` cannot be deserialized.
375///
376/// [Reference](https://spec.graphql.org/June2018/#Value).
377#[derive(Clone, Debug, PartialEq, Eq)]
378pub enum Value {
379    /// A variable, without the `$`.
380    Variable(Name),
381    /// `null`.
382    Null,
383    /// A number.
384    Number(Number),
385    /// A string.
386    String(String),
387    /// A boolean.
388    Boolean(bool),
389    /// A binary.
390    Binary(Bytes),
391    /// An enum. These are typically in `SCREAMING_SNAKE_CASE`.
392    Enum(Name),
393    /// A list of values.
394    List(Vec<Value>),
395    /// An object. This is a map of keys to values.
396    Object(IndexMap<Name, Value>),
397}
398
399impl Value {
400    /// Attempt to convert the value into a const value by using a function to
401    /// get a variable.
402    pub fn into_const_with<E>(
403        self,
404        mut f: impl FnMut(Name) -> Result<ConstValue, E>,
405    ) -> Result<ConstValue, E> {
406        self.into_const_with_mut(&mut f)
407    }
408
409    fn into_const_with_mut<E>(
410        self,
411        f: &mut impl FnMut(Name) -> Result<ConstValue, E>,
412    ) -> Result<ConstValue, E> {
413        Ok(match self {
414            Self::Variable(name) => f(name)?,
415            Self::Null => ConstValue::Null,
416            Self::Number(num) => ConstValue::Number(num),
417            Self::String(s) => ConstValue::String(s),
418            Self::Boolean(b) => ConstValue::Boolean(b),
419            Self::Binary(v) => ConstValue::Binary(v),
420            Self::Enum(v) => ConstValue::Enum(v),
421            Self::List(items) => ConstValue::List(
422                items
423                    .into_iter()
424                    .map(|value| value.into_const_with_mut(f))
425                    .collect::<Result<_, _>>()?,
426            ),
427            Self::Object(map) => ConstValue::Object(
428                map.into_iter()
429                    .map(|(key, value)| Ok((key, value.into_const_with_mut(f)?)))
430                    .collect::<Result<_, _>>()?,
431            ),
432        })
433    }
434
435    /// Attempt to convert the value into a const value.
436    ///
437    /// Will fail if the value contains variables.
438    #[must_use]
439    pub fn into_const(self) -> Option<ConstValue> {
440        self.into_const_with(|_| Err(())).ok()
441    }
442
443    /// Attempt to convert the value into JSON. This is equivalent to the
444    /// `TryFrom` implementation.
445    ///
446    /// # Errors
447    ///
448    /// Fails if serialization fails (see enum docs for more info).
449    pub fn into_json(self) -> serde_json::Result<serde_json::Value> {
450        self.try_into()
451    }
452
453    /// Attempt to convert JSON into a value. This is equivalent to the
454    /// `TryFrom` implementation.
455    ///
456    /// # Errors
457    ///
458    /// Fails if deserialization fails (see enum docs for more info).
459    pub fn from_json(json: serde_json::Value) -> serde_json::Result<Self> {
460        json.try_into()
461    }
462}
463
464impl Default for Value {
465    fn default() -> Self {
466        Self::Null
467    }
468}
469
470impl Display for Value {
471    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
472        match self {
473            Self::Variable(name) => write!(f, "${}", name),
474            Self::Number(num) => write!(f, "{}", *num),
475            Self::String(val) => write_quoted(val, f),
476            Self::Boolean(true) => f.write_str("true"),
477            Self::Boolean(false) => f.write_str("false"),
478            Self::Binary(bytes) => write_binary(bytes, f),
479            Self::Null => f.write_str("null"),
480            Self::Enum(name) => f.write_str(name),
481            Self::List(items) => write_list(items, f),
482            Self::Object(map) => write_object(map, f),
483        }
484    }
485}
486
487impl From<ConstValue> for Value {
488    fn from(value: ConstValue) -> Self {
489        value.into_value()
490    }
491}
492
493impl TryFrom<serde_json::Value> for Value {
494    type Error = serde_json::Error;
495    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
496        Self::deserialize(value)
497    }
498}
499impl TryFrom<Value> for serde_json::Value {
500    type Error = serde_json::Error;
501    fn try_from(value: Value) -> Result<Self, Self::Error> {
502        serde_json::to_value(value)
503    }
504}
505
506fn write_quoted(s: &str, f: &mut Formatter<'_>) -> fmt::Result {
507    f.write_char('"')?;
508    for c in s.chars() {
509        match c {
510            '\r' => f.write_str("\\r"),
511            '\n' => f.write_str("\\n"),
512            '\t' => f.write_str("\\t"),
513            '"' => f.write_str("\\\""),
514            '\\' => f.write_str("\\\\"),
515            c if c.is_control() => write!(f, "\\u{:04}", c as u32),
516            c => f.write_char(c),
517        }?
518    }
519    f.write_char('"')
520}
521
522fn write_binary(bytes: &[u8], f: &mut Formatter<'_>) -> fmt::Result {
523    f.write_char('[')?;
524    let mut iter = bytes.iter().copied();
525    if let Some(value) = iter.next() {
526        value.fmt(f)?;
527    }
528    for value in iter {
529        f.write_str(", ")?;
530        value.fmt(f)?;
531    }
532    f.write_char(']')
533}
534
535fn write_list<T: Display>(list: impl IntoIterator<Item = T>, f: &mut Formatter<'_>) -> fmt::Result {
536    f.write_char('[')?;
537    let mut iter = list.into_iter();
538    if let Some(item) = iter.next() {
539        item.fmt(f)?;
540    }
541    for item in iter {
542        f.write_str(", ")?;
543        item.fmt(f)?;
544    }
545    f.write_char(']')
546}
547
548fn write_object<K: Display, V: Display>(
549    object: impl IntoIterator<Item = (K, V)>,
550    f: &mut Formatter<'_>,
551) -> fmt::Result {
552    f.write_char('{')?;
553    let mut iter = object.into_iter();
554    if let Some((name, value)) = iter.next() {
555        write!(f, "{}: {}", name, value)?;
556    }
557    for (name, value) in iter {
558        f.write_str(", ")?;
559        write!(f, "{}: {}", name, value)?;
560    }
561    f.write_char('}')
562}