diff --git a/Cargo.toml b/Cargo.toml index b7388e89..ea97f5df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ expose-test-schema = [] [dependencies] rustc-serialize = { version = "^0.3.19", optional = true } iron = { version = "^0.4.0", optional = true } -serde = { version = "^0.8.21", optional = true } +serde = { version = "^0.9.1", optional = true } [dev-dependencies] iron = "^0.4.0" diff --git a/src/integrations/serde.rs b/src/integrations/serde.rs index 002d7d9b..e8662949 100644 --- a/src/integrations/serde.rs +++ b/src/integrations/serde.rs @@ -1,5 +1,7 @@ use serde::{de, ser}; +use serde::ser::SerializeMap; use std::collections::HashMap; +use std::fmt; use ::{GraphQLError, Value}; use ast::InputValue; @@ -8,26 +10,27 @@ use parser::{ParseError, Spanning, SourcePosition}; use validation::RuleError; impl ser::Serialize for ExecutionError { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { - let mut state = try!(serializer.serialize_map(Some(3))); + let mut map = try!(serializer.serialize_map(Some(3))); - try!(serializer.serialize_map_key(&mut state, "message")); - try!(serializer.serialize_map_value(&mut state, self.message())); + try!(map.serialize_key("message")); + try!(map.serialize_value(self.message())); - try!(serializer.serialize_map_key(&mut state, "locations")); - try!(serializer.serialize_map_value(&mut state, vec![self.location()])); + let locations = vec![self.location()]; + try!(map.serialize_key("locations")); + try!(map.serialize_value(&locations)); - try!(serializer.serialize_map_key(&mut state, "path")); - try!(serializer.serialize_map_value(&mut state, self.path())); + try!(map.serialize_key("path")); + try!(map.serialize_value(self.path())); - serializer.serialize_map_end(state) + map.end() } } impl<'a> ser::Serialize for GraphQLError<'a> { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { match *self { @@ -47,7 +50,7 @@ impl<'a> ser::Serialize for GraphQLError<'a> { } impl de::Deserialize for InputValue { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: D) -> Result where D: de::Deserializer, { struct InputValueVisitor; @@ -55,50 +58,54 @@ impl de::Deserialize for InputValue { impl de::Visitor for InputValueVisitor { type Value = InputValue; - fn visit_bool(&mut self, value: bool) -> Result { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("a valid input value") + } + + fn visit_bool(self, value: bool) -> Result { Ok(InputValue::boolean(value)) } - fn visit_i64(&mut self, value: i64) -> Result { + fn visit_i64(self, value: i64) -> Result { Ok(InputValue::int(value)) } - fn visit_u64(&mut self, value: u64) -> Result + fn visit_u64(self, value: u64) -> Result where E: de::Error, { self.visit_f64(value as f64) } - fn visit_f64(&mut self, value: f64) -> Result { + fn visit_f64(self, value: f64) -> Result { Ok(InputValue::float(value)) } - fn visit_str(&mut self, value: &str) -> Result + fn visit_str(self, value: &str) -> Result where E: de::Error, { self.visit_string(value.into()) } - fn visit_string(&mut self, value: String) -> Result { + fn visit_string(self, value: String) -> Result { Ok(InputValue::string(value)) } - fn visit_none(&mut self) -> Result { + fn visit_none(self) -> Result { Ok(InputValue::null()) } - fn visit_unit(&mut self) -> Result { + fn visit_unit(self) -> Result { Ok(InputValue::null()) } - fn visit_seq(&mut self, visitor: V) -> Result + fn visit_seq(self, visitor: V) -> Result where V: de::SeqVisitor, { let values = try!(de::impls::VecVisitor::new().visit_seq(visitor)); Ok(InputValue::list(values)) } - fn visit_map(&mut self, visitor: V) -> Result + fn visit_map(self, visitor: V) -> Result where V: de::MapVisitor, { let values = try!(de::impls::HashMapVisitor::::new() @@ -112,7 +119,7 @@ impl de::Deserialize for InputValue { } impl ser::Serialize for InputValue { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { match *self { @@ -135,46 +142,48 @@ impl ser::Serialize for InputValue { } impl ser::Serialize for RuleError { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { - let mut state = try!(serializer.serialize_map(Some(2))); + let mut map = try!(serializer.serialize_map(Some(2))); - try!(serializer.serialize_map_key(&mut state, "message")); - try!(serializer.serialize_map_value(&mut state, self.message())); + try!(map.serialize_key("message")); + try!(map.serialize_value(self.message())); - try!(serializer.serialize_map_key(&mut state, "locations")); - try!(serializer.serialize_map_value(&mut state, self.locations())); + try!(map.serialize_key("locations")); + try!(map.serialize_value(self.locations())); - serializer.serialize_map_end(state) + map.end() } } impl ser::Serialize for SourcePosition { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { - let mut state = try!(serializer.serialize_map(Some(2))); + let mut map = try!(serializer.serialize_map(Some(2))); - try!(serializer.serialize_map_key(&mut state, "line")); - try!(serializer.serialize_map_value(&mut state, self.line() + 1)); + let line = self.line() + 1; + try!(map.serialize_key("line")); + try!(map.serialize_value(&line)); - try!(serializer.serialize_map_key(&mut state, "column")); - try!(serializer.serialize_map_value(&mut state, self.column() + 1)); + let column = self.column() + 1; + try!(map.serialize_key("column")); + try!(map.serialize_value(&column)); - serializer.serialize_map_end(state) + map.end() } } impl<'a> ser::Serialize for Spanning> { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { - let mut state = try!(serializer.serialize_map(Some(2))); + let mut map = try!(serializer.serialize_map(Some(2))); let message = format!("{}", self.item); - try!(serializer.serialize_map_key(&mut state, "message")); - try!(serializer.serialize_map_value(&mut state, message)); + try!(map.serialize_key("message")); + try!(map.serialize_value(&message)); let mut location = HashMap::new(); location.insert("line".to_owned(), self.start.line() + 1); @@ -182,15 +191,15 @@ impl<'a> ser::Serialize for Spanning> { let locations = vec![location]; - try!(serializer.serialize_map_key(&mut state, "locations")); - try!(serializer.serialize_map_value(&mut state, locations)); + try!(map.serialize_key("locations")); + try!(map.serialize_value(&locations)); - serializer.serialize_map_end(state) + map.end() } } impl ser::Serialize for Value { - fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + fn serialize(&self, serializer: S) -> Result where S: ser::Serializer, { match *self {