//! Value for GraphQL. Used in the [`async-graphql`](https://crates.io/crates/async-graphql) crate. #![warn(missing_docs)] #![forbid(unsafe_code)] mod deserializer; mod macros; mod serializer; mod value_serde; mod variables; use std::borrow::{Borrow, Cow}; use std::fmt::{self, Display, Formatter, Write}; use std::ops::Deref; use std::sync::Arc; use bytes::Bytes; use indexmap::IndexMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; pub use deserializer::{from_value, DeserializerError}; #[doc(hidden)] pub use indexmap; pub use serde_json::Number; pub use serializer::{to_value, SerializerError}; pub use variables::Variables; /// A GraphQL name. /// /// [Reference](https://spec.graphql.org/June2018/#Name). #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Name(Arc); impl Serialize for Name { fn serialize(&self, serializer: S) -> std::result::Result { serializer.serialize_str(&self.0) } } impl Name { /// Create a new name. pub fn new(name: impl AsRef) -> Self { Self(name.as_ref().into()) } /// Get the name as a string. #[must_use] pub fn as_str(&self) -> &str { &self.0 } } impl AsRef for Name { fn as_ref(&self) -> &str { &self.0 } } impl Borrow for Name { fn borrow(&self) -> &str { &self.0 } } impl Deref for Name { type Target = str; fn deref(&self) -> &Self::Target { &self.0 } } impl Display for Name { fn fmt(&self, f: &mut Formatter) -> fmt::Result { Display::fmt(&self.0, f) } } impl PartialEq for Name { fn eq(&self, other: &String) -> bool { self.as_str() == other } } impl PartialEq for Name { fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq for String { fn eq(&self, other: &Name) -> bool { self == other.as_str() } } impl PartialEq for str { fn eq(&self, other: &Name) -> bool { other == self } } impl<'a> PartialEq<&'a str> for Name { fn eq(&self, other: &&'a str) -> bool { self == *other } } impl<'a> PartialEq for &'a str { fn eq(&self, other: &Name) -> bool { other == self } } impl<'de> Deserialize<'de> for Name { fn deserialize>(deserializer: D) -> Result { Ok(Self( String::deserialize(deserializer)?.into_boxed_str().into(), )) } } /// A resolved GraphQL value, for example `1` or `"Hello World!"`. /// /// It can be serialized and deserialized. Enums will be converted to strings. Attempting to /// serialize `Upload` will fail, and `Enum` and `Upload` cannot be deserialized. /// /// [Reference](https://spec.graphql.org/June2018/#Value). #[derive(Clone, Debug, Eq)] pub enum ConstValue { /// `null`. Null, /// A number. Number(Number), /// A string. String(String), /// A boolean. Boolean(bool), /// A binary. Binary(Bytes), /// An enum. These are typically in `SCREAMING_SNAKE_CASE`. Enum(Name), /// A list of values. List(Vec), /// An object. This is a map of keys to values. Object(IndexMap), } impl PartialEq for ConstValue { fn eq(&self, other: &ConstValue) -> bool { match (self, other) { (ConstValue::Null, ConstValue::Null) => true, (ConstValue::Number(a), ConstValue::Number(b)) => a == b, (ConstValue::Boolean(a), ConstValue::Boolean(b)) => a == b, (ConstValue::String(a), ConstValue::String(b)) => a == b, (ConstValue::Enum(a), ConstValue::String(b)) => a == b, (ConstValue::String(a), ConstValue::Enum(b)) => a == b, (ConstValue::Enum(a), ConstValue::Enum(b)) => a == b, (ConstValue::Binary(a), ConstValue::Binary(b)) => a == b, (ConstValue::List(a), ConstValue::List(b)) => { if a.len() != b.len() { return false; } a.iter().zip(b.iter()).all(|(a, b)| a == b) } (ConstValue::Object(a), ConstValue::Object(b)) => { if a.len() != b.len() { return false; } for (a_key, a_value) in a.iter() { if let Some(b_value) = b.get(a_key.as_str()) { if b_value != a_value { return false; } } else { return false; } } true } _ => false, } } } impl From<()> for ConstValue { fn from((): ()) -> Self { ConstValue::Null } } macro_rules! from_integer { ($($ty:ident),*) => { $( impl From<$ty> for ConstValue { fn from(n: $ty) -> Self { ConstValue::Number(n.into()) } } )* }; } from_integer!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize); impl From for ConstValue { fn from(f: f32) -> Self { From::from(f as f64) } } impl From for ConstValue { fn from(f: f64) -> Self { Number::from_f64(f).map_or(ConstValue::Null, ConstValue::Number) } } impl From for ConstValue { fn from(value: bool) -> Self { ConstValue::Boolean(value) } } impl From for ConstValue { fn from(value: String) -> Self { ConstValue::String(value) } } impl<'a> From<&'a str> for ConstValue { fn from(value: &'a str) -> Self { ConstValue::String(value.into()) } } impl<'a> From> for ConstValue { fn from(f: Cow<'a, str>) -> Self { ConstValue::String(f.into_owned()) } } impl> FromIterator for ConstValue { fn from_iter>(iter: I) -> Self { ConstValue::List(iter.into_iter().map(Into::into).collect()) } } impl<'a, T: Clone + Into> From<&'a [T]> for ConstValue { fn from(f: &'a [T]) -> Self { ConstValue::List(f.iter().cloned().map(Into::into).collect()) } } impl> From> for ConstValue { fn from(f: Vec) -> Self { ConstValue::List(f.into_iter().map(Into::into).collect()) } } impl From> for ConstValue { fn from(f: IndexMap) -> Self { ConstValue::Object(f) } } impl ConstValue { /// Convert this `ConstValue` into a `Value`. #[must_use] pub fn into_value(self) -> Value { match self { Self::Null => Value::Null, Self::Number(num) => Value::Number(num), Self::String(s) => Value::String(s), Self::Boolean(b) => Value::Boolean(b), Self::Binary(bytes) => Value::Binary(bytes), Self::Enum(v) => Value::Enum(v), Self::List(items) => { Value::List(items.into_iter().map(ConstValue::into_value).collect()) } Self::Object(map) => Value::Object( map.into_iter() .map(|(key, value)| (key, value.into_value())) .collect(), ), } } /// Attempt to convert the value into JSON. This is equivalent to the `TryFrom` implementation. /// /// # Errors /// /// Fails if serialization fails (see enum docs for more info). pub fn into_json(self) -> serde_json::Result { self.try_into() } /// Attempt to convert JSON into a value. This is equivalent to the `TryFrom` implementation. /// /// # Errors /// /// Fails if deserialization fails (see enum docs for more info). pub fn from_json(json: serde_json::Value) -> serde_json::Result { json.try_into() } } impl Default for ConstValue { fn default() -> Self { Self::Null } } impl Display for ConstValue { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::Number(num) => write!(f, "{}", *num), Self::String(val) => write_quoted(val, f), Self::Boolean(true) => f.write_str("true"), Self::Boolean(false) => f.write_str("false"), Self::Binary(bytes) => write_binary(bytes, f), Self::Null => f.write_str("null"), Self::Enum(name) => f.write_str(name), Self::List(items) => write_list(items, f), Self::Object(map) => write_object(map, f), } } } impl TryFrom for ConstValue { type Error = serde_json::Error; fn try_from(value: serde_json::Value) -> Result { Self::deserialize(value) } } impl TryFrom for serde_json::Value { type Error = serde_json::Error; fn try_from(value: ConstValue) -> Result { serde_json::to_value(value) } } /// A GraphQL value, for example `1`, `$name` or `"Hello World!"`. This is /// [`ConstValue`](enum.ConstValue.html) with variables. /// /// It can be serialized and deserialized. Enums will be converted to strings. Attempting to /// serialize `Upload` or `Variable` will fail, and `Enum`, `Upload` and `Variable` cannot be /// deserialized. /// /// [Reference](https://spec.graphql.org/June2018/#Value). #[derive(Clone, Debug, PartialEq, Eq)] pub enum Value { /// A variable, without the `$`. Variable(Name), /// `null`. Null, /// A number. Number(Number), /// A string. String(String), /// A boolean. Boolean(bool), /// A binary. Binary(Bytes), /// An enum. These are typically in `SCREAMING_SNAKE_CASE`. Enum(Name), /// A list of values. List(Vec), /// An object. This is a map of keys to values. Object(IndexMap), } impl Value { /// Attempt to convert the value into a const value by using a function to get a variable. pub fn into_const_with( self, mut f: impl FnMut(Name) -> Result, ) -> Result { self.into_const_with_mut(&mut f) } fn into_const_with_mut( self, f: &mut impl FnMut(Name) -> Result, ) -> Result { Ok(match self { Self::Variable(name) => f(name)?, Self::Null => ConstValue::Null, Self::Number(num) => ConstValue::Number(num), Self::String(s) => ConstValue::String(s), Self::Boolean(b) => ConstValue::Boolean(b), Self::Binary(v) => ConstValue::Binary(v), Self::Enum(v) => ConstValue::Enum(v), Self::List(items) => ConstValue::List( items .into_iter() .map(|value| value.into_const_with_mut(f)) .collect::>()?, ), Self::Object(map) => ConstValue::Object( map.into_iter() .map(|(key, value)| Ok((key, value.into_const_with_mut(f)?))) .collect::>()?, ), }) } /// Attempt to convert the value into a const value. /// /// Will fail if the value contains variables. #[must_use] pub fn into_const(self) -> Option { self.into_const_with(|_| Err(())).ok() } /// Attempt to convert the value into JSON. This is equivalent to the `TryFrom` implementation. /// /// # Errors /// /// Fails if serialization fails (see enum docs for more info). pub fn into_json(self) -> serde_json::Result { self.try_into() } /// Attempt to convert JSON into a value. This is equivalent to the `TryFrom` implementation. /// /// # Errors /// /// Fails if deserialization fails (see enum docs for more info). pub fn from_json(json: serde_json::Value) -> serde_json::Result { json.try_into() } } impl Default for Value { fn default() -> Self { Self::Null } } impl Display for Value { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::Variable(name) => write!(f, "${}", name), Self::Number(num) => write!(f, "{}", *num), Self::String(val) => write_quoted(val, f), Self::Boolean(true) => f.write_str("true"), Self::Boolean(false) => f.write_str("false"), Self::Binary(bytes) => write_binary(bytes, f), Self::Null => f.write_str("null"), Self::Enum(name) => f.write_str(name), Self::List(items) => write_list(items, f), Self::Object(map) => write_object(map, f), } } } impl From for Value { fn from(value: ConstValue) -> Self { value.into_value() } } impl TryFrom for Value { type Error = serde_json::Error; fn try_from(value: serde_json::Value) -> Result { Self::deserialize(value) } } impl TryFrom for serde_json::Value { type Error = serde_json::Error; fn try_from(value: Value) -> Result { serde_json::to_value(value) } } fn write_quoted(s: &str, f: &mut Formatter<'_>) -> fmt::Result { f.write_char('"')?; for c in s.chars() { match c { '\r' => f.write_str("\\r"), '\n' => f.write_str("\\n"), '\t' => f.write_str("\\t"), '"' => f.write_str("\\\""), '\\' => f.write_str("\\\\"), c if c.is_control() => write!(f, "\\u{:04}", c as u32), c => f.write_char(c), }? } f.write_char('"') } fn write_binary(bytes: &[u8], f: &mut Formatter<'_>) -> fmt::Result { f.write_char('[')?; let mut iter = bytes.iter().copied(); if let Some(value) = iter.next() { value.fmt(f)?; } for value in iter { f.write_char(',')?; value.fmt(f)?; } f.write_char(']') } fn write_list(list: impl IntoIterator, f: &mut Formatter<'_>) -> fmt::Result { f.write_char('[')?; let mut iter = list.into_iter(); if let Some(item) = iter.next() { item.fmt(f)?; } for item in iter { f.write_char(',')?; item.fmt(f)?; } f.write_char(']') } fn write_object( object: impl IntoIterator, f: &mut Formatter<'_>, ) -> fmt::Result { f.write_char('{')?; let mut iter = object.into_iter(); if let Some((name, value)) = iter.next() { write!(f, "{}: {}", name, value)?; } for (name, value) in iter { f.write_char(',')?; write!(f, "{}: {}", name, value)?; } f.write_char('}') }