From f66d8bcf33530c858a502bfa170f2383a8cbc204 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 29 Jan 2017 16:53:20 -0800 Subject: Rewrite crate with serde support from ground up This commit completely rewrites this crate from the ground up, supporting serde at the lowest levels as I believe serde support was intended to do. This is a major change from the previous versions of this crate, with a summary of changes being: * Serialization directly to TOML is now supported without going through a `Value` first. * Deserialization directly from TOML is now supported without going through a `Value`. Note that due to the TOML format some values still are buffered in intermediate memory, but overall this should be at a minimum now. * The API of `Value` was overhauled to match the API of `serde_json::Value`. The changes here were to: * Add `is_*` accessors * Add `get` and `get_mut` for one-field lookups. * Implement panicking lookups through `Index` The old `index` methods are now gone in favor of `get` and `Index` implementations. * A `Datetime` type has been added to represent a TOML datetime in a first-class fashion. Currently this type provides no accessors other than a `Display` implementation, but the idea is that this will grow support over time for decomposing the date. * Support for the `rustc-serialize` crate has been dropped, that'll stay on the 0.2 and 0.1 release trains. * This crate no longer supports the detection of unused fields, for that though you can use the `serde_ignored` crate on crates.io --- src/encoder/serde.rs | 339 --------------------------------------------------- 1 file changed, 339 deletions(-) delete mode 100644 src/encoder/serde.rs (limited to 'src/encoder/serde.rs') diff --git a/src/encoder/serde.rs b/src/encoder/serde.rs deleted file mode 100644 index 997bc37..0000000 --- a/src/encoder/serde.rs +++ /dev/null @@ -1,339 +0,0 @@ -use std::mem; - -use serde::ser; -use Value; -use super::{Encoder, Error, EncoderState, State}; - -impl Encoder { - fn table_begin(&mut self) -> Result { - match self.state { - State::NextMapKey => Err(Error::InvalidMapKeyLocation), - _ => Ok(mem::replace(self, Encoder::new())) - } - } - - fn table_end(&mut self, mut state: Self) -> Result<(), Error> { - match state.state { - State::NextKey(key) => { - mem::swap(&mut self.toml, &mut state.toml); - self.toml.insert(key, Value::Table(state.toml)); - }, - State::NextArray(mut arr) => { - mem::swap(&mut self.toml, &mut state.toml); - arr.push(Value::Table(state.toml)); - self.state = State::NextArray(arr); - }, - State::Start => {}, - State::NextMapKey => unreachable!(), - } - Ok(()) - } -} - -impl ser::Serializer for Encoder { - type Error = Error; - type MapState = Self; - type StructState = Self; - type StructVariantState = Self; - type SeqState = EncoderState; - type TupleState = EncoderState; - type TupleStructState = EncoderState; - type TupleVariantState = EncoderState; - - fn serialize_bool(&mut self, v: bool) -> Result<(), Error> { - self.emit_value(Value::Boolean(v)) - } - - fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { - self.emit_value(Value::Integer(v)) - } - - // TODO: checked casts - - fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { - self.serialize_i64(v as i64) - } - - fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { - self.serialize_f64(v as f64) - } - - fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { - self.emit_value(Value::Float(v)) - } - - fn serialize_str(&mut self, value: &str) -> Result<(), Error> { - self.emit_value(Value::String(value.to_string())) - } - - fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<(), Error> { - Ok(()) - } - - fn serialize_unit(&mut self) -> Result<(), Error> { - Ok(()) - } - - fn serialize_none(&mut self) -> Result<(), Error> { - self.emit_none() - } - - fn serialize_char(&mut self, c: char) -> Result<(), Error> { - self.serialize_str(&c.to_string()) - } - - fn serialize_some(&mut self, value: V) -> Result<(), Error> - where V: ser::Serialize - { - value.serialize(self) - } - - fn serialize_bytes(&mut self, v: &[u8]) -> Result<(), Error> { - let mut state = try!(self.serialize_seq(Some(v.len()))); - for c in v { - try!(self.serialize_seq_elt(&mut state, c)); - } - self.serialize_seq_end(state) - } - - fn serialize_seq_fixed_size(&mut self, len: usize) - -> Result { - self.serialize_seq(Some(len)) - } - - fn serialize_seq(&mut self, _len: Option) - -> Result { - self.seq_begin().map(|s| EncoderState { inner: s }) - } - - fn serialize_seq_elt(&mut self, - _state: &mut EncoderState, - value: T) -> Result<(), Error> - where T: ser::Serialize - { - value.serialize(self) - } - - fn serialize_seq_end(&mut self, state: EncoderState) -> Result<(), Error> { - self.seq_end(state.inner) - } - - fn serialize_tuple(&mut self, len: usize) - -> Result { - self.serialize_seq(Some(len)) - } - - fn serialize_tuple_elt(&mut self, - state: &mut EncoderState, - value: T) -> Result<(), Error> - where T: ser::Serialize - { - self.serialize_seq_elt(state, value) - } - - fn serialize_tuple_end(&mut self, state: EncoderState) -> Result<(), Error> { - self.serialize_seq_end(state) - } - - fn serialize_tuple_struct(&mut self, - _name: &'static str, - len: usize) -> Result { - self.serialize_seq(Some(len)) - } - - fn serialize_tuple_struct_elt(&mut self, - state: &mut EncoderState, - value: T) -> Result<(), Error> - where T: ser::Serialize - { - self.serialize_seq_elt(state, value) - } - - fn serialize_tuple_struct_end(&mut self, state: EncoderState) - -> Result<(), Error> { - self.serialize_seq_end(state) - } - - fn serialize_tuple_variant(&mut self, - _name: &'static str, - _id: usize, - _variant: &'static str, - len: usize) -> Result { - self.serialize_seq(Some(len)) - } - - fn serialize_tuple_variant_elt(&mut self, - state: &mut EncoderState, - value: T) -> Result<(), Error> - where T: ser::Serialize - { - self.serialize_seq_elt(state, value) - } - - fn serialize_tuple_variant_end(&mut self, state: EncoderState) - -> Result<(), Error> { - self.serialize_seq_end(state) - } - - fn serialize_map(&mut self, _len: Option) -> Result { - self.table_begin() - } - - fn serialize_map_key(&mut self, - _state: &mut Encoder, - key: K) -> Result<(), Error> - where K: ser::Serialize - { - self.table_key(|me| key.serialize(me)) - } - - fn serialize_map_value(&mut self, - _state: &mut Encoder, - value: V) -> Result<(), Error> - where V: ser::Serialize - { - value.serialize(self) - } - - fn serialize_map_end(&mut self, state: Self) -> Result<(), Error> { - self.table_end(state) - } - - fn serialize_struct(&mut self, - _name: &'static str, - len: usize) -> Result { - self.serialize_map(Some(len)) - } - - fn serialize_struct_elt(&mut self, - state: &mut Encoder, - key: &'static str, - value: V) -> Result<(), Error> - where V: ser::Serialize - { - try!(self.serialize_map_key(state, key)); - self.serialize_map_value(state, value) - } - - fn serialize_struct_end(&mut self, state: Self) -> Result<(), Error> { - self.serialize_map_end(state) - } - - fn serialize_struct_variant(&mut self, - _name: &'static str, - _id: usize, - _variant: &'static str, - len: usize) -> Result { - self.serialize_map(Some(len)) - } - - fn serialize_struct_variant_elt(&mut self, - state: &mut Encoder, - key: &'static str, - value: V) -> Result<(), Error> - where V: ser::Serialize - { - try!(self.serialize_map_key(state, key)); - self.serialize_map_value(state, value) - } - - fn serialize_struct_variant_end(&mut self, state: Self) -> Result<(), Error> { - self.serialize_map_end(state) - } - - fn serialize_newtype_struct(&mut self, - _name: &'static str, - value: T) -> Result<(), Self::Error> - where T: ser::Serialize, - { - // Don't serialize the newtype struct in a tuple. - value.serialize(self) - } - - fn serialize_newtype_variant(&mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - value: T) -> Result<(), Self::Error> - where T: ser::Serialize, - { - // Don't serialize the newtype struct variant in a tuple. - value.serialize(self) - } - - fn serialize_unit_variant(&mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - ) -> Result<(), Self::Error> - { - Ok(()) - } -} - -impl ser::Serialize for Value { - fn serialize(&self, e: &mut E) -> Result<(), E::Error> - where E: ser::Serializer - { - match *self { - Value::String(ref s) => e.serialize_str(s), - Value::Integer(i) => e.serialize_i64(i), - Value::Float(f) => e.serialize_f64(f), - Value::Boolean(b) => e.serialize_bool(b), - Value::Datetime(ref s) => e.serialize_str(s), - Value::Array(ref a) => { - let mut state = try!(e.serialize_seq(Some(a.len()))); - for el in a.iter() { - try!(e.serialize_seq_elt(&mut state, el)); - } - e.serialize_seq_end(state) - } - Value::Table(ref t) => { - let mut state = try!(e.serialize_map(Some(t.len()))); - for (k, v) in t.iter() { - try!(e.serialize_map_key(&mut state, k)); - try!(e.serialize_map_value(&mut state, v)); - } - e.serialize_map_end(state) - } - } - } -} - -impl ser::Error for Error { - fn custom>(msg: T) -> Error { - Error::Custom(msg.into()) - } -} -- cgit v1.2.3