checkpoint
This commit is contained in:
parent
01716fbdcf
commit
3fb863475b
@ -14,7 +14,7 @@ struct Foo {
|
||||
|
||||
impl<S: serde2::VisitorState<R>, R> serde2::Serialize<S, R> for Foo {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.serialize_struct("Foo", FooSerialize {
|
||||
state.visit_struct("Foo", FooSerialize {
|
||||
value: self,
|
||||
state: 0,
|
||||
})
|
||||
@ -31,15 +31,15 @@ impl<'a, S: serde2::VisitorState<R>, R> serde2::Visitor<S, R> for FooSerialize<'
|
||||
match self.state {
|
||||
0 => {
|
||||
self.state += 1;
|
||||
Some(state.serialize_map_elt(true, "x", &self.value.x))
|
||||
Some(state.visit_map_elt(true, "x", &self.value.x))
|
||||
}
|
||||
1 => {
|
||||
self.state += 1;
|
||||
Some(state.serialize_map_elt(false, "y", &self.value.y))
|
||||
Some(state.visit_map_elt(false, "y", &self.value.y))
|
||||
}
|
||||
2 => {
|
||||
self.state += 1;
|
||||
Some(state.serialize_map_elt(false, "z", &self.value.z))
|
||||
Some(state.visit_map_elt(false, "z", &self.value.z))
|
||||
}
|
||||
_ => {
|
||||
None
|
||||
|
3
serde2/src/json/mod.rs
Normal file
3
serde2/src/json/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub use ser::Serializer;
|
||||
|
||||
pub mod ser;
|
208
serde2/src/json/ser.rs
Normal file
208
serde2/src/json/ser.rs
Normal file
@ -0,0 +1,208 @@
|
||||
use std::io;
|
||||
use std::num::{FPNaN, FPInfinite};
|
||||
use std::f64;
|
||||
|
||||
use super::super::ser as ser;
|
||||
|
||||
/// A structure for implementing serialization to JSON.
|
||||
pub struct Serializer<W> {
|
||||
writer: W,
|
||||
}
|
||||
|
||||
impl<W: Writer> Serializer<W> {
|
||||
/// Creates a new JSON serializer whose output will be written to the writer
|
||||
/// specified.
|
||||
pub fn new(writer: W) -> Serializer<W> {
|
||||
Serializer {
|
||||
writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap the Writer from the Serializer.
|
||||
pub fn unwrap(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Writer> ser::VisitorState<io::IoResult<()>> for Serializer<W> {
|
||||
#[inline]
|
||||
fn visit_null(&mut self) -> io::IoResult<()> {
|
||||
self.writer.write_str("null")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_bool(&mut self, value: bool) -> io::IoResult<()> {
|
||||
if value {
|
||||
self.writer.write_str("true")
|
||||
} else {
|
||||
self.writer.write_str("false")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, value: i64) -> io::IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, value: u64) -> io::IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64(&mut self, value: f64) -> io::IoResult<()> {
|
||||
fmt_f64_or_null(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char(&mut self, v: char) -> io::IoResult<()> {
|
||||
escape_char(&mut self.writer, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &'static str) -> io::IoResult<()> {
|
||||
escape_str(&mut self.writer, value)
|
||||
}
|
||||
|
||||
fn visit_seq<
|
||||
T: ser::Serialize<Serializer<W>, io::IoResult<()>>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> io::IoResult<()> {
|
||||
try!(write!(self.writer, "["));
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
try!(self.visit_seq_elt(first, elt));
|
||||
first = false;
|
||||
|
||||
}
|
||||
write!(self.writer, "]")
|
||||
}
|
||||
|
||||
fn visit_seq_elt<
|
||||
T: ser::Serialize<Serializer<W>, io::IoResult<()>>
|
||||
>(&mut self, first: bool, value: T) -> io::IoResult<()> {
|
||||
if !first {
|
||||
try!(write!(self.writer, ", "));
|
||||
}
|
||||
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_tuple<
|
||||
V: ser::Visitor<Serializer<W>, io::IoResult<()>>
|
||||
>(&mut self, mut visitor: V) -> io::IoResult<()> {
|
||||
try!(write!(self.writer, "["));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(Ok(())) => { }
|
||||
Some(Err(err)) => { return Err(err); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
write!(self.writer, "]")
|
||||
}
|
||||
|
||||
fn visit_tuple_struct<
|
||||
V: ser::Visitor<Serializer<W>, io::IoResult<()>>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> io::IoResult<()> {
|
||||
self.visit_tuple(visitor)
|
||||
}
|
||||
|
||||
|
||||
fn visit_enum<
|
||||
V: ser::Visitor<Serializer<W>, io::IoResult<()>>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> io::IoResult<()> {
|
||||
self.visit_tuple(visitor)
|
||||
}
|
||||
|
||||
fn visit_map<
|
||||
K: ser::Serialize<Serializer<W>, io::IoResult<()>>,
|
||||
V: ser::Serialize<Serializer<W>, io::IoResult<()>>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> io::IoResult<()> {
|
||||
try!(write!(self.writer, "{{"));
|
||||
let mut first = true;
|
||||
for (key, value) in iter {
|
||||
try!(self.visit_map_elt(first, &key, &value))
|
||||
first = false;
|
||||
}
|
||||
write!(self.writer, "}}")
|
||||
}
|
||||
|
||||
fn visit_map_elt<
|
||||
K: ser::Serialize<Serializer<W>, io::IoResult<()>>,
|
||||
V: ser::Serialize<Serializer<W>, io::IoResult<()>>
|
||||
>(&mut self, first: bool, key: K, value: V) -> io::IoResult<()> {
|
||||
if !first {
|
||||
try!(write!(self.writer, ", "));
|
||||
}
|
||||
|
||||
try!(key.serialize(self));
|
||||
try!(write!(self.writer, ": "));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_struct<
|
||||
V: ser::Visitor<Serializer<W>, io::IoResult<()>>
|
||||
>(&mut self, _name: &'static str, mut visitor: V) -> io::IoResult<()> {
|
||||
try!(write!(self.writer, "{{"));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(Ok(())) => { }
|
||||
Some(Err(err)) => { return Err(err); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
write!(self.writer, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn escape_bytes<W: Writer>(wr: &mut W, bytes: &[u8]) -> io::IoResult<()> {
|
||||
try!(wr.write_str("\""));
|
||||
|
||||
let mut start = 0;
|
||||
|
||||
for (i, byte) in bytes.iter().enumerate() {
|
||||
let escaped = match *byte {
|
||||
b'"' => "\\\"",
|
||||
b'\\' => "\\\\",
|
||||
b'\x08' => "\\b",
|
||||
b'\x0c' => "\\f",
|
||||
b'\n' => "\\n",
|
||||
b'\r' => "\\r",
|
||||
b'\t' => "\\t",
|
||||
_ => { continue; }
|
||||
};
|
||||
|
||||
if start < i {
|
||||
try!(wr.write(bytes.slice(start, i)));
|
||||
}
|
||||
|
||||
try!(wr.write_str(escaped));
|
||||
|
||||
start = i + 1;
|
||||
}
|
||||
|
||||
if start != bytes.len() {
|
||||
try!(wr.write(bytes.slice_from(start)));
|
||||
}
|
||||
|
||||
wr.write_str("\"")
|
||||
}
|
||||
|
||||
pub fn escape_str<W: Writer>(wr: &mut W, value: &str) -> io::IoResult<()> {
|
||||
escape_bytes(wr, value.as_bytes())
|
||||
}
|
||||
|
||||
pub fn escape_char<W: Writer>(wr: &mut W, value: char) -> io::IoResult<()> {
|
||||
let mut buf = [0, .. 4];
|
||||
value.encode_utf8(buf);
|
||||
escape_bytes(wr, buf)
|
||||
}
|
||||
|
||||
fn fmt_f64_or_null<W: Writer>(wr: &mut W, value: f64) -> io::IoResult<()> {
|
||||
match value.classify() {
|
||||
FPNaN | FPInfinite => wr.write_str("null"),
|
||||
_ => wr.write_str(f64::to_str_digits(value, 6).as_slice()),
|
||||
}
|
||||
}
|
122
serde2/src/json/value.rs
Normal file
122
serde2/src/json/value.rs
Normal file
@ -0,0 +1,122 @@
|
||||
#[deriving(PartialEq)]
|
||||
pub enum Json {
|
||||
Integer(int),
|
||||
String(String),
|
||||
Array(Vec<Json>),
|
||||
Object(TreeMap<String, Json>),
|
||||
}
|
||||
|
||||
pub struct JsonSerializer {
|
||||
key: Option<String>
|
||||
}
|
||||
|
||||
impl JsonSerializer {
|
||||
pub fn new() -> JsonSerializer {
|
||||
JsonSerializer {
|
||||
key: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitorState<Json> for JsonSerializer {
|
||||
fn visit_int(&mut self, value: int) -> Json {
|
||||
Integer(value)
|
||||
}
|
||||
|
||||
fn visit_str(&mut self, value: &'static str) -> Json {
|
||||
String(value.to_string())
|
||||
}
|
||||
|
||||
fn visit_seq<
|
||||
T: Serialize<JsonSerializer, Json>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> Json {
|
||||
let (len, _) = iter.size_hint();
|
||||
let mut v = Vec::with_capacity(len);
|
||||
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
v.push(self.visit_seq_elt(first, elt));
|
||||
first = false;
|
||||
}
|
||||
|
||||
Array(v)
|
||||
}
|
||||
|
||||
fn visit_seq_elt<
|
||||
T: Serialize<JsonSerializer, Json>
|
||||
>(&mut self, _first: bool, value: T) -> Json {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_tuple<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, mut visitor: V) -> Json {
|
||||
let (len, _) = visitor.size_hint();
|
||||
let mut v = Vec::with_capacity(len);
|
||||
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(value) => { v.push(value); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
Array(v)
|
||||
}
|
||||
|
||||
fn visit_tuple_struct<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> Json {
|
||||
self.visit_tuple(visitor)
|
||||
}
|
||||
|
||||
fn visit_enum<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> Json {
|
||||
self.visit_tuple(visitor)
|
||||
}
|
||||
|
||||
fn visit_map<
|
||||
K: Serialize<JsonSerializer, Json>,
|
||||
V: Serialize<JsonSerializer, Json>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> Json {
|
||||
let mut v = TreeMap::new();
|
||||
let mut first = true;
|
||||
|
||||
for (key, value) in iter {
|
||||
let value = self.visit_map_elt(first, key, value);
|
||||
v.insert(self.key.take().unwrap(), value);
|
||||
first = false;
|
||||
}
|
||||
|
||||
Object(v)
|
||||
}
|
||||
|
||||
fn visit_map_elt<
|
||||
K: Serialize<JsonSerializer, Json>,
|
||||
V: Serialize<JsonSerializer, Json>
|
||||
>(&mut self, _first: bool, key: K, value: V) -> Json {
|
||||
match key.serialize(self) {
|
||||
String(key) => { self.key = Some(key); }
|
||||
_ => { fail!() }
|
||||
}
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_struct<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, _name: &'static str, mut visitor: V) -> Json {
|
||||
let mut v = TreeMap::new();
|
||||
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(value) => { v.insert(self.key.take().unwrap(), value); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
Object(v)
|
||||
}
|
||||
}
|
@ -1,560 +1,7 @@
|
||||
use std::io::IoResult;
|
||||
use std::io;
|
||||
use std::collections::TreeMap;
|
||||
pub use ser::{Serialize, Serializer};
|
||||
pub use ser::{Visitor, VisitorState};
|
||||
pub use ser::FormatState;
|
||||
pub use ser::GatherTokens;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serialize<S, R> {
|
||||
fn serialize(&self, state: &mut S) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializer<S, R> {
|
||||
fn hash<T: Serialize<S, R>>(&self, value: &T) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Visitor<S, R> {
|
||||
fn visit(&mut self, state: &mut S) -> Option<R>;
|
||||
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VisitorState<R> {
|
||||
fn serialize_int(&mut self, value: int) -> R;
|
||||
|
||||
fn serialize_str(&mut self, value: &'static str) -> R;
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serialize<Self, R>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, iter: Iter) -> R;
|
||||
|
||||
fn serialize_seq_elt<
|
||||
T: Serialize<Self, R>
|
||||
>(&mut self, first: bool, value: T) -> R;
|
||||
|
||||
fn serialize_tuple<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, visitor: V) -> R;
|
||||
|
||||
fn serialize_tuple_struct<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, name: &'static str, mut visitor: V) -> R;
|
||||
|
||||
fn serialize_enum<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, name: &'static str, variant: &'static str, visitor: V) -> R;
|
||||
|
||||
fn serialize_map<
|
||||
K: Serialize<Self, R>,
|
||||
V: Serialize<Self, R>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, iter: Iter) -> R;
|
||||
|
||||
fn serialize_map_elt<
|
||||
K: Serialize<Self, R>,
|
||||
V: Serialize<Self, R>
|
||||
>(&mut self, first: bool, key: K, value: V) -> R;
|
||||
|
||||
fn serialize_struct<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, name: &'static str, visitor: V) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<S: VisitorState<R>, R> Serialize<S, R> for int {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.serialize_int(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: VisitorState<R>, R> Serialize<S, R> for &'static str {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.serialize_str(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T: Serialize<S, R>
|
||||
> Serialize<S, R> for Vec<T> {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.serialize_seq(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
K: Serialize<S, R> + Ord,
|
||||
V: Serialize<S, R>
|
||||
> Serialize<S, R> for TreeMap<K, V> {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.serialize_map(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T0: Serialize<S, R>,
|
||||
T1: Serialize<S, R>
|
||||
> Serialize<S, R> for (T0, T1) {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.serialize_tuple(Tuple2Serialize { value: self, state: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
struct Tuple2Serialize<'a, T0, T1> {
|
||||
value: &'a (T0, T1),
|
||||
state: uint,
|
||||
}
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T0: Serialize<S, R>,
|
||||
T1: Serialize<S, R>
|
||||
> Visitor<S, R> for Tuple2Serialize<'a, T0, T1> {
|
||||
fn visit(&mut self, state: &mut S) -> Option<R> {
|
||||
match self.state {
|
||||
0 => {
|
||||
self.state += 1;
|
||||
let (ref value, _) = *self.value;
|
||||
Some(state.serialize_seq_elt(true, value))
|
||||
}
|
||||
1 => {
|
||||
self.state += 1;
|
||||
let (_, ref value) = *self.value;
|
||||
Some(state.serialize_seq_elt(false, value))
|
||||
}
|
||||
_ => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
let size = 2 - self.state;
|
||||
(size, Some(size))
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T: Serialize<S, R>
|
||||
> Serialize<S, R> for &'a T {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
(**self).serialize(state)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
pub enum Token {
|
||||
Int(int),
|
||||
Str(&'static str),
|
||||
SeqStart(uint),
|
||||
MapStart(uint),
|
||||
StructStart(&'static str, uint),
|
||||
End,
|
||||
}
|
||||
|
||||
pub trait TokenState<R>: VisitorState<R> {
|
||||
fn serialize(&mut self, token: Token) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct GatherTokens {
|
||||
tokens: Vec<Token>,
|
||||
}
|
||||
|
||||
impl GatherTokens {
|
||||
pub fn new() -> GatherTokens {
|
||||
GatherTokens {
|
||||
tokens: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Vec<Token> {
|
||||
self.tokens
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenState<()> for GatherTokens {
|
||||
fn serialize(&mut self, token: Token) -> () {
|
||||
self.tokens.push(token);
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitorState<()> for GatherTokens {
|
||||
fn serialize_int(&mut self, value: int) -> () {
|
||||
self.serialize(Int(value))
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, value: &'static str) -> () {
|
||||
self.serialize(Str(value))
|
||||
}
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serialize<GatherTokens, ()>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> () {
|
||||
let (len, _) = iter.size_hint();
|
||||
self.serialize(SeqStart(len));
|
||||
let mut first = false;
|
||||
for elt in iter {
|
||||
self.serialize_seq_elt(first, elt);
|
||||
|
||||
if first {
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
self.serialize(End)
|
||||
}
|
||||
|
||||
fn serialize_seq_elt<
|
||||
T: Serialize<GatherTokens, ()>
|
||||
>(&mut self, _first: bool, value: T) -> () {
|
||||
value.serialize(self);
|
||||
}
|
||||
|
||||
fn serialize_tuple<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, mut visitor: V) -> () {
|
||||
let (len, _) = visitor.size_hint();
|
||||
self.tokens.push(SeqStart(len));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(()) => { }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
self.tokens.push(End)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> () {
|
||||
self.serialize_tuple(visitor)
|
||||
}
|
||||
|
||||
fn serialize_enum<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> () {
|
||||
self.serialize_tuple(visitor)
|
||||
}
|
||||
|
||||
fn serialize_map<
|
||||
K: Serialize<GatherTokens, ()>,
|
||||
V: Serialize<GatherTokens, ()>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> () {
|
||||
let (len, _) = iter.size_hint();
|
||||
self.serialize(MapStart(len));
|
||||
let mut first = true;
|
||||
for (key, value) in iter {
|
||||
self.serialize_map_elt(first, key, value);
|
||||
first = false;
|
||||
}
|
||||
self.serialize(End)
|
||||
}
|
||||
|
||||
fn serialize_map_elt<
|
||||
K: Serialize<GatherTokens, ()>,
|
||||
V: Serialize<GatherTokens, ()>
|
||||
>(&mut self, _first: bool, key: K, value: V) -> () {
|
||||
key.serialize(self);
|
||||
value.serialize(self);
|
||||
}
|
||||
|
||||
fn serialize_struct<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, name: &'static str, mut visitor: V) -> () {
|
||||
let (len, _) = visitor.size_hint();
|
||||
self.serialize(StructStart(name, len));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(()) => { }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
self.serialize(End)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct FormatState<W: Writer> {
|
||||
writer: W,
|
||||
}
|
||||
|
||||
impl<W: Writer> FormatState<W> {
|
||||
pub fn new(writer: W) -> FormatState<W> {
|
||||
FormatState {
|
||||
writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Writer> VisitorState<IoResult<()>> for FormatState<W> {
|
||||
fn serialize_int(&mut self, value: int) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, value: &'static str) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serialize<FormatState<W>, IoResult<()>>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> IoResult<()> {
|
||||
try!(write!(self.writer, "["));
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
try!(self.serialize_seq_elt(first, elt));
|
||||
first = false;
|
||||
|
||||
}
|
||||
write!(self.writer, "]")
|
||||
}
|
||||
|
||||
fn serialize_seq_elt<
|
||||
T: Serialize<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, first: bool, value: T) -> IoResult<()> {
|
||||
if !first {
|
||||
try!(write!(self.writer, ", "));
|
||||
}
|
||||
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, mut visitor: V) -> IoResult<()> {
|
||||
try!(write!(self.writer, "["));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(Ok(())) => { }
|
||||
Some(Err(err)) => { return Err(err); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
write!(self.writer, "]")
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> IoResult<()> {
|
||||
self.serialize_tuple(visitor)
|
||||
}
|
||||
|
||||
|
||||
fn serialize_enum<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> IoResult<()> {
|
||||
self.serialize_tuple(visitor)
|
||||
}
|
||||
|
||||
fn serialize_map<
|
||||
K: Serialize<FormatState<W>, IoResult<()>>,
|
||||
V: Serialize<FormatState<W>, IoResult<()>>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> IoResult<()> {
|
||||
try!(write!(self.writer, "{{"));
|
||||
let mut first = true;
|
||||
for (key, value) in iter {
|
||||
try!(self.serialize_map_elt(first, &key, &value))
|
||||
first = false;
|
||||
}
|
||||
write!(self.writer, "}}")
|
||||
}
|
||||
|
||||
fn serialize_map_elt<
|
||||
K: Serialize<FormatState<W>, IoResult<()>>,
|
||||
V: Serialize<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, first: bool, key: K, value: V) -> IoResult<()> {
|
||||
if !first {
|
||||
try!(write!(self.writer, ", "));
|
||||
}
|
||||
|
||||
try!(key.serialize(self));
|
||||
try!(write!(self.writer, ": "));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_struct<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, _name: &'static str, mut visitor: V) -> IoResult<()> {
|
||||
try!(write!(self.writer, "{{"));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(Ok(())) => { }
|
||||
Some(Err(err)) => { return Err(err); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
write!(self.writer, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(PartialEq)]
|
||||
pub enum Json {
|
||||
Integer(int),
|
||||
String(String),
|
||||
Array(Vec<Json>),
|
||||
Object(TreeMap<String, Json>),
|
||||
}
|
||||
|
||||
pub struct JsonSerializer {
|
||||
key: Option<String>
|
||||
}
|
||||
|
||||
impl JsonSerializer {
|
||||
pub fn new() -> JsonSerializer {
|
||||
JsonSerializer {
|
||||
key: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitorState<Json> for JsonSerializer {
|
||||
fn serialize_int(&mut self, value: int) -> Json {
|
||||
Integer(value)
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, value: &'static str) -> Json {
|
||||
String(value.to_string())
|
||||
}
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serialize<JsonSerializer, Json>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> Json {
|
||||
let (len, _) = iter.size_hint();
|
||||
let mut v = Vec::with_capacity(len);
|
||||
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
v.push(self.serialize_seq_elt(first, elt));
|
||||
first = false;
|
||||
}
|
||||
|
||||
Array(v)
|
||||
}
|
||||
|
||||
fn serialize_seq_elt<
|
||||
T: Serialize<JsonSerializer, Json>
|
||||
>(&mut self, _first: bool, value: T) -> Json {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, mut visitor: V) -> Json {
|
||||
let (len, _) = visitor.size_hint();
|
||||
let mut v = Vec::with_capacity(len);
|
||||
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(value) => { v.push(value); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
Array(v)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> Json {
|
||||
self.serialize_tuple(visitor)
|
||||
}
|
||||
|
||||
fn serialize_enum<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> Json {
|
||||
self.serialize_tuple(visitor)
|
||||
}
|
||||
|
||||
fn serialize_map<
|
||||
K: Serialize<JsonSerializer, Json>,
|
||||
V: Serialize<JsonSerializer, Json>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> Json {
|
||||
let mut v = TreeMap::new();
|
||||
let mut first = true;
|
||||
|
||||
for (key, value) in iter {
|
||||
let value = self.serialize_map_elt(first, key, value);
|
||||
v.insert(self.key.take().unwrap(), value);
|
||||
first = false;
|
||||
}
|
||||
|
||||
Object(v)
|
||||
}
|
||||
|
||||
fn serialize_map_elt<
|
||||
K: Serialize<JsonSerializer, Json>,
|
||||
V: Serialize<JsonSerializer, Json>
|
||||
>(&mut self, _first: bool, key: K, value: V) -> Json {
|
||||
match key.serialize(self) {
|
||||
String(key) => { self.key = Some(key); }
|
||||
_ => { fail!() }
|
||||
}
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_struct<
|
||||
V: Visitor<JsonSerializer, Json>
|
||||
>(&mut self, _name: &'static str, mut visitor: V) -> Json {
|
||||
let mut v = TreeMap::new();
|
||||
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(value) => { v.insert(self.key.take().unwrap(), value); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
Object(v)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub fn to_format_vec<
|
||||
T: Serialize<FormatState<io::MemWriter>, IoResult<()>>
|
||||
>(value: &T) -> IoResult<Vec<u8>> {
|
||||
let writer = io::MemWriter::new();
|
||||
let mut state = FormatState::new(writer);
|
||||
try!(value.serialize(&mut state));
|
||||
Ok(state.unwrap().unwrap())
|
||||
}
|
||||
|
||||
pub fn to_format_string<
|
||||
T: Serialize<FormatState<io::MemWriter>, IoResult<()>>
|
||||
>(value: &T) -> IoResult<Result<String, Vec<u8>>> {
|
||||
let vec = try!(to_format_vec(value));
|
||||
Ok(String::from_utf8(vec))
|
||||
}
|
||||
pub mod ser;
|
||||
//pub mod json;
|
||||
|
490
serde2/src/ser.rs
Normal file
490
serde2/src/ser.rs
Normal file
@ -0,0 +1,490 @@
|
||||
use std::io::IoResult;
|
||||
use std::io;
|
||||
use std::collections::TreeMap;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serialize<S, R> {
|
||||
fn serialize(&self, state: &mut S) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializer<S, R> {
|
||||
fn hash<T: Serialize<S, R>>(&self, value: &T) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Visitor<S, R> {
|
||||
fn visit(&mut self, state: &mut S) -> Option<R>;
|
||||
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
(0, None)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VisitorState<R> {
|
||||
fn visit_null(&mut self) -> R;
|
||||
|
||||
fn visit_bool(&mut self, v: bool) -> R;
|
||||
|
||||
#[inline]
|
||||
fn visit_int(&mut self, v: int) -> R {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i8(&mut self, v: i8) -> R {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i16(&mut self, v: i16) -> R {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i32(&mut self, v: i32) -> R {
|
||||
self.visit_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, v: i64) -> R;
|
||||
|
||||
#[inline]
|
||||
fn visit_uint(&mut self, v: uint) -> R {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u8(&mut self, v: u8) -> R {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u16(&mut self, v: u16) -> R {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u32(&mut self, v: u32) -> R {
|
||||
self.visit_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, v: u64) -> R;
|
||||
|
||||
#[inline]
|
||||
fn visit_f32(&mut self, v: f32) -> R {
|
||||
self.visit_f64(v as f64)
|
||||
}
|
||||
|
||||
fn visit_f64(&mut self, v: f64) -> R;
|
||||
|
||||
fn visit_char(&mut self, value: char) -> R;
|
||||
|
||||
fn visit_str(&mut self, value: &'static str) -> R;
|
||||
|
||||
fn visit_seq<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, visitor: V) -> R;
|
||||
|
||||
fn visit_named_seq<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> R {
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
fn visit_enum<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> R {
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
fn visit_seq_elt<
|
||||
T: Serialize<Self, R>
|
||||
>(&mut self, value: T) -> R;
|
||||
|
||||
fn visit_map<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, visitor: V) -> R;
|
||||
|
||||
fn visit_named_map<
|
||||
V: Visitor<Self, R>
|
||||
>(&mut self, _name: &'static str, visitor: V) -> R {
|
||||
self.visit_map(visitor)
|
||||
}
|
||||
|
||||
fn visit_map_elt<
|
||||
K: Serialize<Self, R>,
|
||||
V: Serialize<Self, R>
|
||||
>(&mut self, key: K, value: V) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<S: VisitorState<R>, R> Serialize<S, R> for int {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.visit_int(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: VisitorState<R>, R> Serialize<S, R> for &'static str {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.visit_str(*self)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct IteratorVisitor<Iter> {
|
||||
iter: Iter,
|
||||
}
|
||||
|
||||
impl<T, Iter: Iterator<T>> IteratorVisitor<Iter> {
|
||||
pub fn new(iter: Iter) -> IteratorVisitor<Iter> {
|
||||
IteratorVisitor {
|
||||
iter: iter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
T: Serialize<S, R>,
|
||||
Iter: Iterator<T>,
|
||||
S: VisitorState<R>,
|
||||
R
|
||||
> Visitor<S, R> for IteratorVisitor<Iter> {
|
||||
fn visit(&mut self, state: &mut S) -> Option<R> {
|
||||
match self.iter.next() {
|
||||
Some(value) => Some(state.visit_seq_elt(value)),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T: Serialize<S, R>
|
||||
> Serialize<S, R> for Vec<T> {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.visit_seq(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
K: Serialize<S, R> + Ord,
|
||||
V: Serialize<S, R>
|
||||
> Serialize<S, R> for TreeMap<K, V> {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.visit_map(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T0: Serialize<S, R>,
|
||||
T1: Serialize<S, R>
|
||||
> Serialize<S, R> for (T0, T1) {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
state.visit_seq(Tuple2Serialize { value: self, state: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
struct Tuple2Serialize<'a, T0, T1> {
|
||||
value: &'a (T0, T1),
|
||||
state: uint,
|
||||
}
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T0: Serialize<S, R>,
|
||||
T1: Serialize<S, R>
|
||||
> Visitor<S, R> for Tuple2Serialize<'a, T0, T1> {
|
||||
fn visit(&mut self, state: &mut S) -> Option<R> {
|
||||
match self.state {
|
||||
0 => {
|
||||
self.state += 1;
|
||||
let (ref value, _) = *self.value;
|
||||
Some(state.visit_seq_elt(true, value))
|
||||
}
|
||||
1 => {
|
||||
self.state += 1;
|
||||
let (_, ref value) = *self.value;
|
||||
Some(state.visit_seq_elt(false, value))
|
||||
}
|
||||
_ => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
let size = 2 - self.state;
|
||||
(size, Some(size))
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: VisitorState<R>,
|
||||
R,
|
||||
T: Serialize<S, R>
|
||||
> Serialize<S, R> for &'a T {
|
||||
fn serialize(&self, state: &mut S) -> R {
|
||||
(**self).serialize(state)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
pub enum Token {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int(int),
|
||||
I64(i64),
|
||||
U64(u64),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'static str),
|
||||
SeqStart(uint),
|
||||
MapStart(uint),
|
||||
StructStart(&'static str, uint),
|
||||
End,
|
||||
}
|
||||
|
||||
pub trait TokenState<R>: VisitorState<R> {
|
||||
fn serialize(&mut self, token: Token) -> R;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct GatherTokens {
|
||||
tokens: Vec<Token>,
|
||||
}
|
||||
|
||||
impl GatherTokens {
|
||||
pub fn new() -> GatherTokens {
|
||||
GatherTokens {
|
||||
tokens: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Vec<Token> {
|
||||
self.tokens
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenState<()> for GatherTokens {
|
||||
fn serialize(&mut self, token: Token) -> () {
|
||||
self.tokens.push(token);
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitorState<()> for GatherTokens {
|
||||
fn visit_null(&mut self) -> () {
|
||||
self.serialize(Null)
|
||||
}
|
||||
|
||||
fn visit_bool(&mut self, value: bool) -> () {
|
||||
self.serialize(Bool(value))
|
||||
}
|
||||
|
||||
fn visit_i64(&mut self, value: i64) -> () {
|
||||
self.serialize(I64(value))
|
||||
}
|
||||
|
||||
fn visit_u64(&mut self, value: u64) -> () {
|
||||
self.serialize(U64(value))
|
||||
}
|
||||
|
||||
fn visit_f64(&mut self, value: f64) -> () {
|
||||
self.serialize(F64(value))
|
||||
}
|
||||
|
||||
fn visit_char(&mut self, value: char) -> () {
|
||||
self.serialize(Char(value))
|
||||
}
|
||||
|
||||
fn visit_str(&mut self, value: &'static str) -> () {
|
||||
self.serialize(Str(value))
|
||||
}
|
||||
|
||||
fn visit_seq<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, mut visitor: V) -> () {
|
||||
let (len, _) = visitor.size_hint();
|
||||
self.tokens.push(SeqStart(len));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(()) => { }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
self.tokens.push(End)
|
||||
}
|
||||
|
||||
fn visit_enum<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> () {
|
||||
self.visit_seq(visitor)
|
||||
}
|
||||
|
||||
fn visit_map<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, mut visitor: V) -> () {
|
||||
let (len, _) = visitor.size_hint();
|
||||
self.serialize(MapStart(len));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(()) => { }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
self.serialize(End)
|
||||
}
|
||||
|
||||
fn visit_named_map<
|
||||
V: Visitor<GatherTokens, ()>
|
||||
>(&mut self, name: &'static str, mut visitor: V) -> () {
|
||||
let (len, _) = visitor.size_hint();
|
||||
self.serialize(StructStart(name, len));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(()) => { }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
self.serialize(End)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub struct FormatState<W: Writer> {
|
||||
writer: W,
|
||||
}
|
||||
|
||||
impl<W: Writer> FormatState<W> {
|
||||
pub fn new(writer: W) -> FormatState<W> {
|
||||
FormatState {
|
||||
writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Writer> VisitorState<IoResult<()>> for FormatState<W> {
|
||||
fn visit_null(&mut self) -> IoResult<()> {
|
||||
write!(self.writer, "()")
|
||||
}
|
||||
|
||||
fn visit_bool(&mut self, value: bool) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn visit_i64(&mut self, value: i64) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn visit_u64(&mut self, value: u64) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn visit_f64(&mut self, value: f64) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn visit_char(&mut self, value: char) -> io::IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn visit_str(&mut self, value: &'static str) -> IoResult<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
}
|
||||
|
||||
fn visit_seq<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, mut visitor: V) -> IoResult<()> {
|
||||
try!(write!(self.writer, "["));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(Ok(())) => { }
|
||||
Some(Err(err)) => { return Err(err); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
write!(self.writer, "]")
|
||||
}
|
||||
|
||||
fn visit_enum<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, _name: &'static str, _variant: &'static str, visitor: V) -> IoResult<()> {
|
||||
self.visit_tuple(visitor)
|
||||
}
|
||||
|
||||
fn visit_map_elt<
|
||||
K: Serialize<FormatState<W>, IoResult<()>>,
|
||||
V: Serialize<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, first: bool, key: K, value: V) -> IoResult<()> {
|
||||
if !first {
|
||||
try!(write!(self.writer, ", "));
|
||||
}
|
||||
|
||||
try!(key.serialize(self));
|
||||
try!(write!(self.writer, ": "));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn visit_map<
|
||||
V: Visitor<FormatState<W>, IoResult<()>>
|
||||
>(&mut self, mut visitor: V) -> IoResult<()> {
|
||||
try!(write!(self.writer, "{{"));
|
||||
loop {
|
||||
match visitor.visit(self) {
|
||||
Some(Ok(())) => { }
|
||||
Some(Err(err)) => { return Err(err); }
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
write!(self.writer, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub fn to_format_vec<
|
||||
T: Serialize<FormatState<io::MemWriter>, IoResult<()>>
|
||||
>(value: &T) -> IoResult<Vec<u8>> {
|
||||
let writer = io::MemWriter::new();
|
||||
let mut state = FormatState::new(writer);
|
||||
try!(value.serialize(&mut state));
|
||||
Ok(state.unwrap().unwrap())
|
||||
}
|
||||
|
||||
pub fn to_format_string<
|
||||
T: Serialize<FormatState<io::MemWriter>, IoResult<()>>
|
||||
>(value: &T) -> IoResult<Result<String, Vec<u8>>> {
|
||||
let vec = try!(to_format_vec(value));
|
||||
Ok(String::from_utf8(vec))
|
||||
}
|
Loading…
Reference in New Issue
Block a user