next iteration
This commit is contained in:
parent
183646b118
commit
c7d96b191e
|
@ -9,7 +9,6 @@ serde = "0.7"
|
||||||
toml = { path = "..", features = ["serde"] }
|
toml = { path = "..", features = ["serde"] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
syntex = "0.33"
|
|
||||||
serde_codegen = "0.7"
|
serde_codegen = "0.7"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
extern crate syntex;
|
|
||||||
extern crate serde_codegen;
|
extern crate serde_codegen;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
@ -10,8 +9,5 @@ fn main() {
|
||||||
let src = Path::new("test.rs.in");
|
let src = Path::new("test.rs.in");
|
||||||
let dst = Path::new(&out_dir).join("test.rs");
|
let dst = Path::new(&out_dir).join("test.rs");
|
||||||
|
|
||||||
let mut registry = syntex::Registry::new();
|
serde_codegen::expand(&src, &dst).unwrap();
|
||||||
|
|
||||||
serde_codegen::register(&mut registry);
|
|
||||||
registry.expand("", &src, &dst).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,12 @@ use super::{Encoder, Error, State};
|
||||||
impl ser::Serializer for Encoder {
|
impl ser::Serializer for Encoder {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type MapState = Self;
|
type MapState = Self;
|
||||||
|
type StructState = Self;
|
||||||
|
type StructVariantState = Self;
|
||||||
type SeqState = State;
|
type SeqState = State;
|
||||||
|
type TupleState = State;
|
||||||
|
type TupleStructState = State;
|
||||||
|
type TupleVariantState = State;
|
||||||
|
|
||||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||||
self.emit_value(Value::Boolean(v))
|
self.emit_value(Value::Boolean(v))
|
||||||
|
@ -16,12 +21,42 @@ impl ser::Serializer for Encoder {
|
||||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
||||||
self.serialize_i64(v as i64)
|
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> {
|
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
||||||
self.emit_value(Value::Float(v))
|
self.emit_value(Value::Float(v))
|
||||||
}
|
}
|
||||||
fn serialize_str(&mut self, value: &str) -> Result<(), Error> {
|
fn serialize_str(&mut self, value: &str) -> Result<(), Error> {
|
||||||
self.emit_value(Value::String(value.to_string()))
|
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> {
|
fn serialize_unit(&mut self) -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -36,30 +71,95 @@ impl ser::Serializer for Encoder {
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
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<State, Error> {
|
||||||
|
self.serialize_seq(Some(len))
|
||||||
|
}
|
||||||
fn serialize_seq(&mut self, _len: Option<usize>) -> Result<State, Error> {
|
fn serialize_seq(&mut self, _len: Option<usize>) -> Result<State, Error> {
|
||||||
self.seq_begin()
|
self.seq_begin()
|
||||||
}
|
}
|
||||||
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
|
fn serialize_seq_elt<T>(&mut self, _state: &mut State, value: T) -> Result<(), Error>
|
||||||
where T: ser::Serialize
|
where T: ser::Serialize
|
||||||
{
|
{
|
||||||
value.serialize(self)
|
value.serialize(self)
|
||||||
}
|
}
|
||||||
fn serialize_seq_end(&mut self, _len: Option<usize>, state: State) -> Result<(), Error> {
|
fn serialize_seq_end(&mut self, state: State) -> Result<(), Error> {
|
||||||
self.seq_end(state)
|
self.seq_end(state)
|
||||||
}
|
}
|
||||||
|
fn serialize_tuple(&mut self, len: usize) -> Result<State, Error> {
|
||||||
|
self.serialize_seq(Some(len))
|
||||||
|
}
|
||||||
|
fn serialize_tuple_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
|
||||||
|
where T: ser::Serialize
|
||||||
|
{
|
||||||
|
self.serialize_seq_elt(state, value)
|
||||||
|
}
|
||||||
|
fn serialize_tuple_end(&mut self, state: State) -> Result<(), Error> {
|
||||||
|
self.serialize_seq_end(state)
|
||||||
|
}
|
||||||
|
fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<State, Error> {
|
||||||
|
self.serialize_seq(Some(len))
|
||||||
|
}
|
||||||
|
fn serialize_tuple_struct_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
|
||||||
|
where T: ser::Serialize
|
||||||
|
{
|
||||||
|
self.serialize_seq_elt(state, value)
|
||||||
|
}
|
||||||
|
fn serialize_tuple_struct_end(&mut self, state: State) -> Result<(), Error> {
|
||||||
|
self.serialize_seq_end(state)
|
||||||
|
}
|
||||||
|
fn serialize_tuple_variant(&mut self, _name: &'static str, _id: usize, _variant: &'static str, len: usize) -> Result<State, Error> {
|
||||||
|
self.serialize_seq(Some(len))
|
||||||
|
}
|
||||||
|
fn serialize_tuple_variant_elt<T>(&mut self, state: &mut State, value: T) -> Result<(), Error>
|
||||||
|
where T: ser::Serialize
|
||||||
|
{
|
||||||
|
self.serialize_seq_elt(state, value)
|
||||||
|
}
|
||||||
|
fn serialize_tuple_variant_end(&mut self, state: State) -> Result<(), Error> {
|
||||||
|
self.serialize_seq_end(state)
|
||||||
|
}
|
||||||
fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> {
|
fn serialize_map(&mut self, _len: Option<usize>) -> Result<Self, Error> {
|
||||||
self.table_begin()
|
self.table_begin()
|
||||||
}
|
}
|
||||||
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Error>
|
fn serialize_map_elt<K, V>(&mut self, _state: &mut Encoder, key: K, value: V) -> Result<(), Error>
|
||||||
where K: ser::Serialize, V: ser::Serialize
|
where K: ser::Serialize, V: ser::Serialize
|
||||||
{
|
{
|
||||||
try!(self.table_key(|me| key.serialize(me)));
|
try!(self.table_key(|me| key.serialize(me)));
|
||||||
try!(value.serialize(self));
|
try!(value.serialize(self));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn serialize_map_end(&mut self, _len: Option<usize>, state: Self) -> Result<(), Error> {
|
fn serialize_map_end(&mut self, state: Self) -> Result<(), Error> {
|
||||||
self.table_end(state)
|
self.table_end(state)
|
||||||
}
|
}
|
||||||
|
fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<Self, Error> {
|
||||||
|
self.serialize_map(Some(len))
|
||||||
|
}
|
||||||
|
fn serialize_struct_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
|
||||||
|
where V: ser::Serialize
|
||||||
|
{
|
||||||
|
self.serialize_map_elt(state, key, 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, Error> {
|
||||||
|
self.serialize_map(Some(len))
|
||||||
|
}
|
||||||
|
fn serialize_struct_variant_elt<V>(&mut self, state: &mut Encoder, key: &'static str, value: V) -> Result<(), Error>
|
||||||
|
where V: ser::Serialize
|
||||||
|
{
|
||||||
|
self.serialize_map_elt(state, key, value)
|
||||||
|
}
|
||||||
|
fn serialize_struct_variant_end(&mut self, state: Self) -> Result<(), Error> {
|
||||||
|
self.serialize_map_end(state)
|
||||||
|
}
|
||||||
fn serialize_newtype_struct<T>(&mut self,
|
fn serialize_newtype_struct<T>(&mut self,
|
||||||
_name: &'static str,
|
_name: &'static str,
|
||||||
value: T) -> Result<(), Self::Error>
|
value: T) -> Result<(), Self::Error>
|
||||||
|
@ -78,6 +178,14 @@ impl ser::Serializer for Encoder {
|
||||||
// Don't serialize the newtype struct variant in a tuple.
|
// Don't serialize the newtype struct variant in a tuple.
|
||||||
value.serialize(self)
|
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 {
|
impl ser::Serialize for Value {
|
||||||
|
@ -91,18 +199,18 @@ impl ser::Serialize for Value {
|
||||||
Value::Boolean(b) => e.serialize_bool(b),
|
Value::Boolean(b) => e.serialize_bool(b),
|
||||||
Value::Datetime(ref s) => e.serialize_str(s),
|
Value::Datetime(ref s) => e.serialize_str(s),
|
||||||
Value::Array(ref a) => {
|
Value::Array(ref a) => {
|
||||||
let state = try!(e.serialize_seq(Some(a.len())));
|
let mut state = try!(e.serialize_seq(Some(a.len())));
|
||||||
for el in a.iter() {
|
for el in a.iter() {
|
||||||
try!(e.serialize_seq_elt(el));
|
try!(e.serialize_seq_elt(&mut state, el));
|
||||||
}
|
}
|
||||||
e.serialize_seq_end(Some(a.len()), state)
|
e.serialize_seq_end(state)
|
||||||
}
|
}
|
||||||
Value::Table(ref t) => {
|
Value::Table(ref t) => {
|
||||||
let state = try!(e.serialize_map(Some(t.len())));
|
let mut state = try!(e.serialize_map(Some(t.len())));
|
||||||
for (k, v) in t.iter() {
|
for (k, v) in t.iter() {
|
||||||
try!(e.serialize_map_elt(k, v));
|
try!(e.serialize_map_elt(&mut state, k, v));
|
||||||
}
|
}
|
||||||
e.serialize_map_end(Some(t.len()), state)
|
e.serialize_map_end(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue