wip
This commit is contained in:
parent
f46d5a9973
commit
ee162975f9
955
bench_log.rs
Normal file
955
bench_log.rs
Normal file
@ -0,0 +1,955 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
extern crate serialize;
|
||||
extern crate test;
|
||||
extern crate time;
|
||||
|
||||
use std::io::MemWriter;
|
||||
use test::Bencher;
|
||||
|
||||
use json;
|
||||
use ser::Serializable;
|
||||
use ser;
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
struct Http {
|
||||
protocol: HttpProtocol,
|
||||
status: u32,
|
||||
host_status: u32,
|
||||
up_status: u32,
|
||||
method: HttpMethod,
|
||||
content_type: String,
|
||||
user_agent: String,
|
||||
referer: String,
|
||||
request_uri: String,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Http {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize_struct_start("Http", 9));
|
||||
|
||||
try!(s.serialize_struct_sep("protocol"));
|
||||
try!(self.protocol.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("status"));
|
||||
try!(self.status.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("host_status"));
|
||||
try!(self.host_status.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("up_status"));
|
||||
try!(self.up_status.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("method"));
|
||||
try!(self.method.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("content_type"));
|
||||
try!(self.content_type.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("user_agent"));
|
||||
try!(self.user_agent.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("referer"));
|
||||
try!(self.referer.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("request_uri"));
|
||||
try!(self.request_uri.serialize(s));
|
||||
|
||||
s.serialize_struct_end()
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
enum HttpProtocol {
|
||||
HTTP_PROTOCOL_UNKNOWN,
|
||||
HTTP10,
|
||||
HTTP11,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for HttpProtocol {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_uint(*self as uint)
|
||||
/*
|
||||
match *self {
|
||||
HTTP_PROTOCOL_UNKNOWN => {
|
||||
try!(s.serialize_EnumStart("HttpProtocol", "HTTP_PROTOCOL_UNKNOWN", 0)));
|
||||
}
|
||||
HTTP10 => {
|
||||
try!(s.serialize_EnumStart("HttpProtocol", "HTTP10", 0)));
|
||||
}
|
||||
HTTP11 => {
|
||||
try!(s.serialize_EnumStart("HttpProtocol", "HTTP11", 0)));
|
||||
}
|
||||
}
|
||||
|
||||
s.serialize_end()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
enum HttpMethod {
|
||||
METHOD_UNKNOWN,
|
||||
GET,
|
||||
POST,
|
||||
DELETE,
|
||||
PUT,
|
||||
HEAD,
|
||||
PURGE,
|
||||
OPTIONS,
|
||||
PROPFIND,
|
||||
MKCOL,
|
||||
PATCH,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for HttpMethod {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_uint(*self as uint)
|
||||
/*
|
||||
match *self {
|
||||
METHOD_UNKNOWN => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "METHOD_UNKNOWN", 0)));
|
||||
}
|
||||
GET => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "GET", 0)));
|
||||
}
|
||||
|
||||
POST => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "POST", 0)));
|
||||
}
|
||||
DELETE => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "DELETE", 0)));
|
||||
}
|
||||
PUT => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "PUT", 0)));
|
||||
}
|
||||
HEAD => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "HEAD", 0)));
|
||||
}
|
||||
PURGE => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "PURGE", 0)));
|
||||
}
|
||||
OPTIONS => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "OPTIONS", 0)));
|
||||
}
|
||||
PROPFIND => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "PROPFIND", 0)));
|
||||
}
|
||||
MKCOL => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "MKCOL", 0)));
|
||||
}
|
||||
PATCH => {
|
||||
try!(s.serialize_EnumStart("HttpMethod", "PATCH", 0)));
|
||||
}
|
||||
}
|
||||
|
||||
s.serialize_end()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
enum CacheStatus {
|
||||
CACHESTATUS_UNKNOWN,
|
||||
Miss,
|
||||
Expired,
|
||||
Hit,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for CacheStatus {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_uint(*self as uint)
|
||||
/*
|
||||
match *self {
|
||||
CACHESTATUS_UNKNOWN => {
|
||||
try!(s.serialize_EnumStart("CacheStatus", "CACHESTATUS_UNKNOWN", 0)));
|
||||
}
|
||||
Miss => {
|
||||
try!(s.serialize_EnumStart("CacheStatus", "Miss", 0)));
|
||||
}
|
||||
Expired => {
|
||||
try!(s.serialize_EnumStart("CacheStatus", "Expired", 0)));
|
||||
}
|
||||
Hit => {
|
||||
try!(s.serialize_EnumStart("CacheStatus", "Hit", 0)));
|
||||
}
|
||||
}
|
||||
|
||||
s.serialize_end()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
struct Origin {
|
||||
ip: String,
|
||||
port: u32,
|
||||
hostname: String,
|
||||
protocol: OriginProtocol,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Origin {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize_struct_start("Http", 4));
|
||||
|
||||
try!(s.serialize_struct_sep("ip"));
|
||||
try!(self.ip.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("port"));
|
||||
try!(self.port.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("hostname"));
|
||||
try!(self.hostname.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("protocol"));
|
||||
try!(self.protocol.serialize(s));
|
||||
|
||||
s.serialize_struct_end()
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
enum OriginProtocol {
|
||||
ORIGIN_PROTOCOL_UNKNOWN,
|
||||
HTTP,
|
||||
HTTPS,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for OriginProtocol {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_uint(*self as uint)
|
||||
/*
|
||||
match *self {
|
||||
ORIGIN_PROTOCOL_UNKNOWN => {
|
||||
try!(s.serialize_EnumStart("OriginProtocol", "ORIGIN_PROTOCOL_UNKNOWN", 0)));
|
||||
}
|
||||
HTTP => {
|
||||
try!(s.serialize_EnumStart("OriginProtocol", "HTTP", 0)));
|
||||
}
|
||||
HTTPS => {
|
||||
try!(s.serialize_EnumStart("OriginProtocol", "HTTPS", 0)));
|
||||
}
|
||||
}
|
||||
|
||||
s.serialize_end()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
enum ZonePlan {
|
||||
ZONEPLAN_UNKNOWN,
|
||||
FREE,
|
||||
PRO,
|
||||
BIZ,
|
||||
ENT,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for ZonePlan {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_uint(*self as uint)
|
||||
/*
|
||||
match *self {
|
||||
ZONEPLAN_UNKNOWN => {
|
||||
try!(s.serialize_EnumStart("ZonePlan", "ZONEPLAN_UNKNOWN", 0)));
|
||||
}
|
||||
FREE => {
|
||||
try!(s.serialize_EnumStart("ZonePlan", "FREE", 0)));
|
||||
}
|
||||
PRO => {
|
||||
try!(s.serialize_EnumStart("ZonePlan", "PRO", 0)));
|
||||
}
|
||||
BIZ => {
|
||||
try!(s.serialize_EnumStart("ZonePlan", "BIZ", 0)));
|
||||
}
|
||||
ENT => {
|
||||
try!(s.serialize_EnumStart("ZonePlan", "ENT", 0)));
|
||||
}
|
||||
}
|
||||
|
||||
s.serialize_end()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
enum Country {
|
||||
UNKNOWN,
|
||||
A1,
|
||||
A2,
|
||||
O1,
|
||||
AD,
|
||||
AE,
|
||||
AF,
|
||||
AG,
|
||||
AI,
|
||||
AL,
|
||||
AM,
|
||||
AO,
|
||||
AP,
|
||||
AQ,
|
||||
AR,
|
||||
AS,
|
||||
AT,
|
||||
AU,
|
||||
AW,
|
||||
AX,
|
||||
AZ,
|
||||
BA,
|
||||
BB,
|
||||
BD,
|
||||
BE,
|
||||
BF,
|
||||
BG,
|
||||
BH,
|
||||
BI,
|
||||
BJ,
|
||||
BL,
|
||||
BM,
|
||||
BN,
|
||||
BO,
|
||||
BQ,
|
||||
BR,
|
||||
BS,
|
||||
BT,
|
||||
BV,
|
||||
BW,
|
||||
BY,
|
||||
BZ,
|
||||
CA,
|
||||
CC,
|
||||
CD,
|
||||
CF,
|
||||
CG,
|
||||
CH,
|
||||
CI,
|
||||
CK,
|
||||
CL,
|
||||
CM,
|
||||
CN,
|
||||
CO,
|
||||
CR,
|
||||
CU,
|
||||
CV,
|
||||
CW,
|
||||
CX,
|
||||
CY,
|
||||
CZ,
|
||||
DE,
|
||||
DJ,
|
||||
DK,
|
||||
DM,
|
||||
DO,
|
||||
DZ,
|
||||
EC,
|
||||
EE,
|
||||
EG,
|
||||
EH,
|
||||
ER,
|
||||
ES,
|
||||
ET,
|
||||
EU,
|
||||
FI,
|
||||
FJ,
|
||||
FK,
|
||||
FM,
|
||||
FO,
|
||||
FR,
|
||||
GA,
|
||||
GB,
|
||||
GD,
|
||||
GE,
|
||||
GF,
|
||||
GG,
|
||||
GH,
|
||||
GI,
|
||||
GL,
|
||||
GM,
|
||||
GN,
|
||||
GP,
|
||||
GQ,
|
||||
GR,
|
||||
GS,
|
||||
GT,
|
||||
GU,
|
||||
GW,
|
||||
GY,
|
||||
HK,
|
||||
HM,
|
||||
HN,
|
||||
HR,
|
||||
HT,
|
||||
HU,
|
||||
ID,
|
||||
IE,
|
||||
IL,
|
||||
IM,
|
||||
IN,
|
||||
IO,
|
||||
IQ,
|
||||
IR,
|
||||
IS,
|
||||
IT,
|
||||
JE,
|
||||
JM,
|
||||
JO,
|
||||
JP,
|
||||
KE,
|
||||
KG,
|
||||
KH,
|
||||
KI,
|
||||
KM,
|
||||
KN,
|
||||
KP,
|
||||
KR,
|
||||
KW,
|
||||
KY,
|
||||
KZ,
|
||||
LA,
|
||||
LB,
|
||||
LC,
|
||||
LI,
|
||||
LK,
|
||||
LR,
|
||||
LS,
|
||||
LT,
|
||||
LU,
|
||||
LV,
|
||||
LY,
|
||||
MA,
|
||||
MC,
|
||||
MD,
|
||||
ME,
|
||||
MF,
|
||||
MG,
|
||||
MH,
|
||||
MK,
|
||||
ML,
|
||||
MM,
|
||||
MN,
|
||||
MO,
|
||||
MP,
|
||||
MQ,
|
||||
MR,
|
||||
MS,
|
||||
MT,
|
||||
MU,
|
||||
MV,
|
||||
MW,
|
||||
MX,
|
||||
MY,
|
||||
MZ,
|
||||
NA,
|
||||
NC,
|
||||
NE,
|
||||
NF,
|
||||
NG,
|
||||
NI,
|
||||
NL,
|
||||
NO,
|
||||
NP,
|
||||
NR,
|
||||
NU,
|
||||
NZ,
|
||||
OM,
|
||||
PA,
|
||||
PE,
|
||||
PF,
|
||||
PG,
|
||||
PH,
|
||||
PK,
|
||||
PL,
|
||||
PM,
|
||||
PN,
|
||||
PR,
|
||||
PS,
|
||||
PT,
|
||||
PW,
|
||||
PY,
|
||||
QA,
|
||||
RE,
|
||||
RO,
|
||||
RS,
|
||||
RU,
|
||||
RW,
|
||||
SA,
|
||||
SB,
|
||||
SC,
|
||||
SD,
|
||||
SE,
|
||||
SG,
|
||||
SH,
|
||||
SI,
|
||||
SJ,
|
||||
SK,
|
||||
SL,
|
||||
SM,
|
||||
SN,
|
||||
SO,
|
||||
SR,
|
||||
SS,
|
||||
ST,
|
||||
SV,
|
||||
SX,
|
||||
SY,
|
||||
SZ,
|
||||
TC,
|
||||
TD,
|
||||
TF,
|
||||
TG,
|
||||
TH,
|
||||
TJ,
|
||||
TK,
|
||||
TL,
|
||||
TM,
|
||||
TN,
|
||||
TO,
|
||||
TR,
|
||||
TT,
|
||||
TV,
|
||||
TW,
|
||||
TZ,
|
||||
UA,
|
||||
UG,
|
||||
UM,
|
||||
US,
|
||||
UY,
|
||||
UZ,
|
||||
VA,
|
||||
VC,
|
||||
VE,
|
||||
VG,
|
||||
VI,
|
||||
VN,
|
||||
VU,
|
||||
WF,
|
||||
WS,
|
||||
XX,
|
||||
YE,
|
||||
YT,
|
||||
ZA,
|
||||
ZM,
|
||||
ZW,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Country {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_uint(*self as uint)
|
||||
/*
|
||||
match *self {
|
||||
UNKNOWN => { try!(s.serialize_EnumStart("Country", "UNKNOWN", 0))); }
|
||||
A1 => { try!(s.serialize_EnumStart("Country", "A1", 0))); }
|
||||
A2 => { try!(s.serialize_EnumStart("Country", "A2", 0))); }
|
||||
O1 => { try!(s.serialize_EnumStart("Country", "O1", 0))); }
|
||||
AD => { try!(s.serialize_EnumStart("Country", "AD", 0))); }
|
||||
AE => { try!(s.serialize_EnumStart("Country", "AE", 0))); }
|
||||
AF => { try!(s.serialize_EnumStart("Country", "AF", 0))); }
|
||||
AG => { try!(s.serialize_EnumStart("Country", "AG", 0))); }
|
||||
AI => { try!(s.serialize_EnumStart("Country", "AI", 0))); }
|
||||
AL => { try!(s.serialize_EnumStart("Country", "AL", 0))); }
|
||||
AM => { try!(s.serialize_EnumStart("Country", "AM", 0))); }
|
||||
AO => { try!(s.serialize_EnumStart("Country", "AO", 0))); }
|
||||
AP => { try!(s.serialize_EnumStart("Country", "AP", 0))); }
|
||||
AQ => { try!(s.serialize_EnumStart("Country", "AQ", 0))); }
|
||||
AR => { try!(s.serialize_EnumStart("Country", "AR", 0))); }
|
||||
AS => { try!(s.serialize_EnumStart("Country", "AS", 0))); }
|
||||
AT => { try!(s.serialize_EnumStart("Country", "AT", 0))); }
|
||||
AU => { try!(s.serialize_EnumStart("Country", "AU", 0))); }
|
||||
AW => { try!(s.serialize_EnumStart("Country", "AW", 0))); }
|
||||
AX => { try!(s.serialize_EnumStart("Country", "AX", 0))); }
|
||||
AZ => { try!(s.serialize_EnumStart("Country", "AZ", 0))); }
|
||||
BA => { try!(s.serialize_EnumStart("Country", "BA", 0))); }
|
||||
BB => { try!(s.serialize_EnumStart("Country", "BB", 0))); }
|
||||
BD => { try!(s.serialize_EnumStart("Country", "BD", 0))); }
|
||||
BE => { try!(s.serialize_EnumStart("Country", "BE", 0))); }
|
||||
BF => { try!(s.serialize_EnumStart("Country", "BF", 0))); }
|
||||
BG => { try!(s.serialize_EnumStart("Country", "BG", 0))); }
|
||||
BH => { try!(s.serialize_EnumStart("Country", "BH", 0))); }
|
||||
BI => { try!(s.serialize_EnumStart("Country", "BI", 0))); }
|
||||
BJ => { try!(s.serialize_EnumStart("Country", "BJ", 0))); }
|
||||
BL => { try!(s.serialize_EnumStart("Country", "BL", 0))); }
|
||||
BM => { try!(s.serialize_EnumStart("Country", "BM", 0))); }
|
||||
BN => { try!(s.serialize_EnumStart("Country", "BN", 0))); }
|
||||
BO => { try!(s.serialize_EnumStart("Country", "BO", 0))); }
|
||||
BQ => { try!(s.serialize_EnumStart("Country", "BQ", 0))); }
|
||||
BR => { try!(s.serialize_EnumStart("Country", "BR", 0))); }
|
||||
BS => { try!(s.serialize_EnumStart("Country", "BS", 0))); }
|
||||
BT => { try!(s.serialize_EnumStart("Country", "BT", 0))); }
|
||||
BV => { try!(s.serialize_EnumStart("Country", "BV", 0))); }
|
||||
BW => { try!(s.serialize_EnumStart("Country", "BW", 0))); }
|
||||
BY => { try!(s.serialize_EnumStart("Country", "BY", 0))); }
|
||||
BZ => { try!(s.serialize_EnumStart("Country", "BZ", 0))); }
|
||||
CA => { try!(s.serialize_EnumStart("Country", "CA", 0))); }
|
||||
CC => { try!(s.serialize_EnumStart("Country", "CC", 0))); }
|
||||
CD => { try!(s.serialize_EnumStart("Country", "CD", 0))); }
|
||||
CF => { try!(s.serialize_EnumStart("Country", "CF", 0))); }
|
||||
CG => { try!(s.serialize_EnumStart("Country", "CG", 0))); }
|
||||
CH => { try!(s.serialize_EnumStart("Country", "CH", 0))); }
|
||||
CI => { try!(s.serialize_EnumStart("Country", "CI", 0))); }
|
||||
CK => { try!(s.serialize_EnumStart("Country", "CK", 0))); }
|
||||
CL => { try!(s.serialize_EnumStart("Country", "CL", 0))); }
|
||||
CM => { try!(s.serialize_EnumStart("Country", "CM", 0))); }
|
||||
CN => { try!(s.serialize_EnumStart("Country", "CN", 0))); }
|
||||
CO => { try!(s.serialize_EnumStart("Country", "CO", 0))); }
|
||||
CR => { try!(s.serialize_EnumStart("Country", "CR", 0))); }
|
||||
CU => { try!(s.serialize_EnumStart("Country", "CU", 0))); }
|
||||
CV => { try!(s.serialize_EnumStart("Country", "CV", 0))); }
|
||||
CW => { try!(s.serialize_EnumStart("Country", "CW", 0))); }
|
||||
CX => { try!(s.serialize_EnumStart("Country", "CX", 0))); }
|
||||
CY => { try!(s.serialize_EnumStart("Country", "CY", 0))); }
|
||||
CZ => { try!(s.serialize_EnumStart("Country", "CZ", 0))); }
|
||||
DE => { try!(s.serialize_EnumStart("Country", "DE", 0))); }
|
||||
DJ => { try!(s.serialize_EnumStart("Country", "DJ", 0))); }
|
||||
DK => { try!(s.serialize_EnumStart("Country", "DK", 0))); }
|
||||
DM => { try!(s.serialize_EnumStart("Country", "DM", 0))); }
|
||||
DO => { try!(s.serialize_EnumStart("Country", "DO", 0))); }
|
||||
DZ => { try!(s.serialize_EnumStart("Country", "DZ", 0))); }
|
||||
EC => { try!(s.serialize_EnumStart("Country", "EC", 0))); }
|
||||
EE => { try!(s.serialize_EnumStart("Country", "EE", 0))); }
|
||||
EG => { try!(s.serialize_EnumStart("Country", "EG", 0))); }
|
||||
EH => { try!(s.serialize_EnumStart("Country", "EH", 0))); }
|
||||
ER => { try!(s.serialize_EnumStart("Country", "ER", 0))); }
|
||||
ES => { try!(s.serialize_EnumStart("Country", "ES", 0))); }
|
||||
ET => { try!(s.serialize_EnumStart("Country", "ET", 0))); }
|
||||
EU => { try!(s.serialize_EnumStart("Country", "EU", 0))); }
|
||||
FI => { try!(s.serialize_EnumStart("Country", "FI", 0))); }
|
||||
FJ => { try!(s.serialize_EnumStart("Country", "FJ", 0))); }
|
||||
FK => { try!(s.serialize_EnumStart("Country", "FK", 0))); }
|
||||
FM => { try!(s.serialize_EnumStart("Country", "FM", 0))); }
|
||||
FO => { try!(s.serialize_EnumStart("Country", "FO", 0))); }
|
||||
FR => { try!(s.serialize_EnumStart("Country", "FR", 0))); }
|
||||
GA => { try!(s.serialize_EnumStart("Country", "GA", 0))); }
|
||||
GB => { try!(s.serialize_EnumStart("Country", "GB", 0))); }
|
||||
GD => { try!(s.serialize_EnumStart("Country", "GD", 0))); }
|
||||
GE => { try!(s.serialize_EnumStart("Country", "GE", 0))); }
|
||||
GF => { try!(s.serialize_EnumStart("Country", "GF", 0))); }
|
||||
GG => { try!(s.serialize_EnumStart("Country", "GG", 0))); }
|
||||
GH => { try!(s.serialize_EnumStart("Country", "GH", 0))); }
|
||||
GI => { try!(s.serialize_EnumStart("Country", "GI", 0))); }
|
||||
GL => { try!(s.serialize_EnumStart("Country", "GL", 0))); }
|
||||
GM => { try!(s.serialize_EnumStart("Country", "GM", 0))); }
|
||||
GN => { try!(s.serialize_EnumStart("Country", "GN", 0))); }
|
||||
GP => { try!(s.serialize_EnumStart("Country", "GP", 0))); }
|
||||
GQ => { try!(s.serialize_EnumStart("Country", "GQ", 0))); }
|
||||
GR => { try!(s.serialize_EnumStart("Country", "GR", 0))); }
|
||||
GS => { try!(s.serialize_EnumStart("Country", "GS", 0))); }
|
||||
GT => { try!(s.serialize_EnumStart("Country", "GT", 0))); }
|
||||
GU => { try!(s.serialize_EnumStart("Country", "GU", 0))); }
|
||||
GW => { try!(s.serialize_EnumStart("Country", "GW", 0))); }
|
||||
GY => { try!(s.serialize_EnumStart("Country", "GY", 0))); }
|
||||
HK => { try!(s.serialize_EnumStart("Country", "HK", 0))); }
|
||||
HM => { try!(s.serialize_EnumStart("Country", "HM", 0))); }
|
||||
HN => { try!(s.serialize_EnumStart("Country", "HN", 0))); }
|
||||
HR => { try!(s.serialize_EnumStart("Country", "HR", 0))); }
|
||||
HT => { try!(s.serialize_EnumStart("Country", "HT", 0))); }
|
||||
HU => { try!(s.serialize_EnumStart("Country", "HU", 0))); }
|
||||
ID => { try!(s.serialize_EnumStart("Country", "ID", 0))); }
|
||||
IE => { try!(s.serialize_EnumStart("Country", "IE", 0))); }
|
||||
IL => { try!(s.serialize_EnumStart("Country", "IL", 0))); }
|
||||
IM => { try!(s.serialize_EnumStart("Country", "IM", 0))); }
|
||||
IN => { try!(s.serialize_EnumStart("Country", "IN", 0))); }
|
||||
IO => { try!(s.serialize_EnumStart("Country", "IO", 0))); }
|
||||
IQ => { try!(s.serialize_EnumStart("Country", "IQ", 0))); }
|
||||
IR => { try!(s.serialize_EnumStart("Country", "IR", 0))); }
|
||||
IS => { try!(s.serialize_EnumStart("Country", "IS", 0))); }
|
||||
IT => { try!(s.serialize_EnumStart("Country", "IT", 0))); }
|
||||
JE => { try!(s.serialize_EnumStart("Country", "JE", 0))); }
|
||||
JM => { try!(s.serialize_EnumStart("Country", "JM", 0))); }
|
||||
JO => { try!(s.serialize_EnumStart("Country", "JO", 0))); }
|
||||
JP => { try!(s.serialize_EnumStart("Country", "JP", 0))); }
|
||||
KE => { try!(s.serialize_EnumStart("Country", "KE", 0))); }
|
||||
KG => { try!(s.serialize_EnumStart("Country", "KG", 0))); }
|
||||
KH => { try!(s.serialize_EnumStart("Country", "KH", 0))); }
|
||||
KI => { try!(s.serialize_EnumStart("Country", "KI", 0))); }
|
||||
KM => { try!(s.serialize_EnumStart("Country", "KM", 0))); }
|
||||
KN => { try!(s.serialize_EnumStart("Country", "KN", 0))); }
|
||||
KP => { try!(s.serialize_EnumStart("Country", "KP", 0))); }
|
||||
KR => { try!(s.serialize_EnumStart("Country", "KR", 0))); }
|
||||
KW => { try!(s.serialize_EnumStart("Country", "KW", 0))); }
|
||||
KY => { try!(s.serialize_EnumStart("Country", "KY", 0))); }
|
||||
KZ => { try!(s.serialize_EnumStart("Country", "KZ", 0))); }
|
||||
LA => { try!(s.serialize_EnumStart("Country", "LA", 0))); }
|
||||
LB => { try!(s.serialize_EnumStart("Country", "LB", 0))); }
|
||||
LC => { try!(s.serialize_EnumStart("Country", "LC", 0))); }
|
||||
LI => { try!(s.serialize_EnumStart("Country", "LI", 0))); }
|
||||
LK => { try!(s.serialize_EnumStart("Country", "LK", 0))); }
|
||||
LR => { try!(s.serialize_EnumStart("Country", "LR", 0))); }
|
||||
LS => { try!(s.serialize_EnumStart("Country", "LS", 0))); }
|
||||
LT => { try!(s.serialize_EnumStart("Country", "LT", 0))); }
|
||||
LU => { try!(s.serialize_EnumStart("Country", "LU", 0))); }
|
||||
LV => { try!(s.serialize_EnumStart("Country", "LV", 0))); }
|
||||
LY => { try!(s.serialize_EnumStart("Country", "LY", 0))); }
|
||||
MA => { try!(s.serialize_EnumStart("Country", "MA", 0))); }
|
||||
MC => { try!(s.serialize_EnumStart("Country", "MC", 0))); }
|
||||
MD => { try!(s.serialize_EnumStart("Country", "MD", 0))); }
|
||||
ME => { try!(s.serialize_EnumStart("Country", "ME", 0))); }
|
||||
MF => { try!(s.serialize_EnumStart("Country", "MF", 0))); }
|
||||
MG => { try!(s.serialize_EnumStart("Country", "MG", 0))); }
|
||||
MH => { try!(s.serialize_EnumStart("Country", "MH", 0))); }
|
||||
MK => { try!(s.serialize_EnumStart("Country", "MK", 0))); }
|
||||
ML => { try!(s.serialize_EnumStart("Country", "ML", 0))); }
|
||||
MM => { try!(s.serialize_EnumStart("Country", "MM", 0))); }
|
||||
MN => { try!(s.serialize_EnumStart("Country", "MN", 0))); }
|
||||
MO => { try!(s.serialize_EnumStart("Country", "MO", 0))); }
|
||||
MP => { try!(s.serialize_EnumStart("Country", "MP", 0))); }
|
||||
MQ => { try!(s.serialize_EnumStart("Country", "MQ", 0))); }
|
||||
MR => { try!(s.serialize_EnumStart("Country", "MR", 0))); }
|
||||
MS => { try!(s.serialize_EnumStart("Country", "MS", 0))); }
|
||||
MT => { try!(s.serialize_EnumStart("Country", "MT", 0))); }
|
||||
MU => { try!(s.serialize_EnumStart("Country", "MU", 0))); }
|
||||
MV => { try!(s.serialize_EnumStart("Country", "MV", 0))); }
|
||||
MW => { try!(s.serialize_EnumStart("Country", "MW", 0))); }
|
||||
MX => { try!(s.serialize_EnumStart("Country", "MX", 0))); }
|
||||
MY => { try!(s.serialize_EnumStart("Country", "MY", 0))); }
|
||||
MZ => { try!(s.serialize_EnumStart("Country", "MZ", 0))); }
|
||||
NA => { try!(s.serialize_EnumStart("Country", "NA", 0))); }
|
||||
NC => { try!(s.serialize_EnumStart("Country", "NC", 0))); }
|
||||
NE => { try!(s.serialize_EnumStart("Country", "NE", 0))); }
|
||||
NF => { try!(s.serialize_EnumStart("Country", "NF", 0))); }
|
||||
NG => { try!(s.serialize_EnumStart("Country", "NG", 0))); }
|
||||
NI => { try!(s.serialize_EnumStart("Country", "NI", 0))); }
|
||||
NL => { try!(s.serialize_EnumStart("Country", "NL", 0))); }
|
||||
NO => { try!(s.serialize_EnumStart("Country", "NO", 0))); }
|
||||
NP => { try!(s.serialize_EnumStart("Country", "NP", 0))); }
|
||||
NR => { try!(s.serialize_EnumStart("Country", "NR", 0))); }
|
||||
NU => { try!(s.serialize_EnumStart("Country", "NU", 0))); }
|
||||
NZ => { try!(s.serialize_EnumStart("Country", "NZ", 0))); }
|
||||
OM => { try!(s.serialize_EnumStart("Country", "OM", 0))); }
|
||||
PA => { try!(s.serialize_EnumStart("Country", "PA", 0))); }
|
||||
PE => { try!(s.serialize_EnumStart("Country", "PE", 0))); }
|
||||
PF => { try!(s.serialize_EnumStart("Country", "PF", 0))); }
|
||||
PG => { try!(s.serialize_EnumStart("Country", "PG", 0))); }
|
||||
PH => { try!(s.serialize_EnumStart("Country", "PH", 0))); }
|
||||
PK => { try!(s.serialize_EnumStart("Country", "PK", 0))); }
|
||||
PL => { try!(s.serialize_EnumStart("Country", "PL", 0))); }
|
||||
PM => { try!(s.serialize_EnumStart("Country", "PM", 0))); }
|
||||
PN => { try!(s.serialize_EnumStart("Country", "PN", 0))); }
|
||||
PR => { try!(s.serialize_EnumStart("Country", "PR", 0))); }
|
||||
PS => { try!(s.serialize_EnumStart("Country", "PS", 0))); }
|
||||
PT => { try!(s.serialize_EnumStart("Country", "PT", 0))); }
|
||||
PW => { try!(s.serialize_EnumStart("Country", "PW", 0))); }
|
||||
PY => { try!(s.serialize_EnumStart("Country", "PY", 0))); }
|
||||
QA => { try!(s.serialize_EnumStart("Country", "QA", 0))); }
|
||||
RE => { try!(s.serialize_EnumStart("Country", "RE", 0))); }
|
||||
RO => { try!(s.serialize_EnumStart("Country", "RO", 0))); }
|
||||
RS => { try!(s.serialize_EnumStart("Country", "RS", 0))); }
|
||||
RU => { try!(s.serialize_EnumStart("Country", "RU", 0))); }
|
||||
RW => { try!(s.serialize_EnumStart("Country", "RW", 0))); }
|
||||
SA => { try!(s.serialize_EnumStart("Country", "SA", 0))); }
|
||||
SB => { try!(s.serialize_EnumStart("Country", "SB", 0))); }
|
||||
SC => { try!(s.serialize_EnumStart("Country", "SC", 0))); }
|
||||
SD => { try!(s.serialize_EnumStart("Country", "SD", 0))); }
|
||||
SE => { try!(s.serialize_EnumStart("Country", "SE", 0))); }
|
||||
SG => { try!(s.serialize_EnumStart("Country", "SG", 0))); }
|
||||
SH => { try!(s.serialize_EnumStart("Country", "SH", 0))); }
|
||||
SI => { try!(s.serialize_EnumStart("Country", "SI", 0))); }
|
||||
SJ => { try!(s.serialize_EnumStart("Country", "SJ", 0))); }
|
||||
SK => { try!(s.serialize_EnumStart("Country", "SK", 0))); }
|
||||
SL => { try!(s.serialize_EnumStart("Country", "SL", 0))); }
|
||||
SM => { try!(s.serialize_EnumStart("Country", "SM", 0))); }
|
||||
SN => { try!(s.serialize_EnumStart("Country", "SN", 0))); }
|
||||
SO => { try!(s.serialize_EnumStart("Country", "SO", 0))); }
|
||||
SR => { try!(s.serialize_EnumStart("Country", "SR", 0))); }
|
||||
SS => { try!(s.serialize_EnumStart("Country", "SS", 0))); }
|
||||
ST => { try!(s.serialize_EnumStart("Country", "ST", 0))); }
|
||||
SV => { try!(s.serialize_EnumStart("Country", "SV", 0))); }
|
||||
SX => { try!(s.serialize_EnumStart("Country", "SX", 0))); }
|
||||
SY => { try!(s.serialize_EnumStart("Country", "SY", 0))); }
|
||||
SZ => { try!(s.serialize_EnumStart("Country", "SZ", 0))); }
|
||||
TC => { try!(s.serialize_EnumStart("Country", "TC", 0))); }
|
||||
TD => { try!(s.serialize_EnumStart("Country", "TD", 0))); }
|
||||
TF => { try!(s.serialize_EnumStart("Country", "TF", 0))); }
|
||||
TG => { try!(s.serialize_EnumStart("Country", "TG", 0))); }
|
||||
TH => { try!(s.serialize_EnumStart("Country", "TH", 0))); }
|
||||
TJ => { try!(s.serialize_EnumStart("Country", "TJ", 0))); }
|
||||
TK => { try!(s.serialize_EnumStart("Country", "TK", 0))); }
|
||||
TL => { try!(s.serialize_EnumStart("Country", "TL", 0))); }
|
||||
TM => { try!(s.serialize_EnumStart("Country", "TM", 0))); }
|
||||
TN => { try!(s.serialize_EnumStart("Country", "TN", 0))); }
|
||||
TO => { try!(s.serialize_EnumStart("Country", "TO", 0))); }
|
||||
TR => { try!(s.serialize_EnumStart("Country", "TR", 0))); }
|
||||
TT => { try!(s.serialize_EnumStart("Country", "TT", 0))); }
|
||||
TV => { try!(s.serialize_EnumStart("Country", "TV", 0))); }
|
||||
TW => { try!(s.serialize_EnumStart("Country", "TW", 0))); }
|
||||
TZ => { try!(s.serialize_EnumStart("Country", "TZ", 0))); }
|
||||
UA => { try!(s.serialize_EnumStart("Country", "UA", 0))); }
|
||||
UG => { try!(s.serialize_EnumStart("Country", "UG", 0))); }
|
||||
UM => { try!(s.serialize_EnumStart("Country", "UM", 0))); }
|
||||
US => { try!(s.serialize_EnumStart("Country", "US", 0))); }
|
||||
UY => { try!(s.serialize_EnumStart("Country", "UY", 0))); }
|
||||
UZ => { try!(s.serialize_EnumStart("Country", "UZ", 0))); }
|
||||
VA => { try!(s.serialize_EnumStart("Country", "VA", 0))); }
|
||||
VC => { try!(s.serialize_EnumStart("Country", "VC", 0))); }
|
||||
VE => { try!(s.serialize_EnumStart("Country", "VE", 0))); }
|
||||
VG => { try!(s.serialize_EnumStart("Country", "VG", 0))); }
|
||||
VI => { try!(s.serialize_EnumStart("Country", "VI", 0))); }
|
||||
VN => { try!(s.serialize_EnumStart("Country", "VN", 0))); }
|
||||
VU => { try!(s.serialize_EnumStart("Country", "VU", 0))); }
|
||||
WF => { try!(s.serialize_EnumStart("Country", "WF", 0))); }
|
||||
WS => { try!(s.serialize_EnumStart("Country", "WS", 0))); }
|
||||
XX => { try!(s.serialize_EnumStart("Country", "XX", 0))); }
|
||||
YE => { try!(s.serialize_EnumStart("Country", "YE", 0))); }
|
||||
YT => { try!(s.serialize_EnumStart("Country", "YT", 0))); }
|
||||
ZA => { try!(s.serialize_EnumStart("Country", "ZA", 0))); }
|
||||
ZM => { try!(s.serialize_EnumStart("Country", "ZM", 0))); }
|
||||
ZW => { try!(s.serialize_EnumStart("Country", "ZW", 0))); }
|
||||
}
|
||||
|
||||
s.serialize_end()
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#[deriving(Encodable, Decodable)]
|
||||
struct Log {
|
||||
timestamp: i64,
|
||||
zone_id: u32,
|
||||
zone_plan: ZonePlan,
|
||||
http: Http,
|
||||
origin: Origin,
|
||||
country: Country,
|
||||
cache_status: CacheStatus,
|
||||
server_ip: String,
|
||||
server_name: String,
|
||||
remote_ip: String,
|
||||
bytes_dlv: u64,
|
||||
ray_id: String,
|
||||
}
|
||||
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Log {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize_struct_start("Log", 12));
|
||||
|
||||
try!(s.serialize_struct_sep("timestamp"));
|
||||
try!(self.timestamp.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("zone_id"));
|
||||
try!(self.zone_id.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("zone_plan"));
|
||||
try!(self.zone_plan.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("http"));
|
||||
try!(self.http.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("origin"));
|
||||
try!(self.origin.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("country"));
|
||||
try!(self.country.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("cache_status"));
|
||||
try!(self.cache_status.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("server_ip"));
|
||||
try!(self.server_ip.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("server_name"));
|
||||
try!(self.server_name.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("remote_ip"));
|
||||
try!(self.remote_ip.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("bytes_dlv"));
|
||||
try!(self.bytes_dlv.serialize(s));
|
||||
|
||||
try!(s.serialize_struct_sep("ray_id"));
|
||||
try!(self.ray_id.serialize(s));
|
||||
|
||||
s.serialize_struct_end()
|
||||
}
|
||||
}
|
||||
|
||||
impl Log {
|
||||
fn new() -> Log {
|
||||
Log {
|
||||
timestamp: time::precise_time_ns() as i64,
|
||||
zone_id: 123456,
|
||||
zone_plan: FREE,
|
||||
http: Http {
|
||||
protocol: HTTP11,
|
||||
status: 200,
|
||||
host_status: 503,
|
||||
up_status: 520,
|
||||
method: GET,
|
||||
content_type: "text/html".to_string(),
|
||||
user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36".to_string(),
|
||||
referer: "https://www.cloudflare.com/".to_string(),
|
||||
request_uri: "/cdn-cgi/trace".to_string(),
|
||||
},
|
||||
origin: Origin {
|
||||
ip: "1.2.3.4".to_string(),
|
||||
port: 8000,
|
||||
hostname: "www.example.com".to_string(),
|
||||
protocol: HTTPS,
|
||||
},
|
||||
country: US,
|
||||
cache_status: Hit,
|
||||
server_ip: "192.168.1.1".to_string(),
|
||||
server_name: "metal.cloudflare.com".to_string(),
|
||||
remote_ip: "10.1.2.3".to_string(),
|
||||
bytes_dlv: 123456,
|
||||
ray_id: "10c73629cce30078-LAX".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_encoder(b: &mut Bencher) {
|
||||
let log = Log::new();
|
||||
let json = serialize::json::Encoder::str_encode(&log);
|
||||
//println!("json: {}", json);
|
||||
let _len = json.len();
|
||||
|
||||
b.iter(|| {
|
||||
let _ = serialize::json::Encoder::str_encode(&log);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_serializer(b: &mut Bencher) {
|
||||
let log = Log::new();
|
||||
|
||||
let mut wr = MemWriter::with_capacity(700);
|
||||
{
|
||||
let mut serializer = json::Serializer::new(&mut wr);
|
||||
log.serialize(&mut serializer).unwrap();
|
||||
}
|
||||
let json = String::from_utf8(wr.unwrap()).unwrap();
|
||||
//println!("json: {}", json);
|
||||
let _len = json.len();
|
||||
|
||||
b.iter(|| {
|
||||
let mut wr = MemWriter::with_capacity(700);
|
||||
{
|
||||
let mut serializer = json::Serializer::new(&mut wr);
|
||||
log.serialize(&mut serializer).unwrap();
|
||||
}
|
||||
//let _ = String::from_utf8(wr.unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decooder(b: &mut Bencher) {
|
||||
let s = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":"FREE","http":{"protocol":"HTTP11","status":200,"host_status":503,"up_status":520,"method":"GET","content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":"HTTPS"},"country":"US","cache_status":"Hit","server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#;
|
||||
|
||||
|
||||
//"{"timestamp":3444218605346,"zone_id":123456,"zone_plan":1,"http":{"protocol":2,"status":200,"host_status":503,"up_status":520,"method":1,"content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":2},"country":238,"cache_status":3,"server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#;
|
||||
|
||||
b.iter(|| {
|
||||
let json = serialize::json::from_str(s).unwrap();
|
||||
//println!("json: {}", json);
|
||||
let mut decoder = serialize::json::Decoder::new(json);
|
||||
let _log: Log = serialize::Decodable::decode(&mut decoder).unwrap();
|
||||
});
|
||||
|
||||
}
|
544
json.rs
544
json.rs
@ -234,6 +234,7 @@ use std::f64;
|
||||
use std::fmt;
|
||||
use std::io::MemWriter;
|
||||
use std::io;
|
||||
use std::mem::transmute;
|
||||
use std::num;
|
||||
use std::str::ScalarValue;
|
||||
use std::str;
|
||||
@ -266,6 +267,7 @@ impl Json {
|
||||
self.serialize(&mut serializer)
|
||||
}
|
||||
|
||||
/*
|
||||
/// Encodes a json value into an io::writer.
|
||||
/// Pretty-prints in a more readable format.
|
||||
pub fn to_pretty_writer(&self, wr: &mut Writer) -> EncodeResult {
|
||||
@ -279,6 +281,7 @@ impl Json {
|
||||
self.to_pretty_writer(&mut s as &mut Writer).unwrap();
|
||||
str::from_utf8(s.unwrap().as_slice()).unwrap().to_string()
|
||||
}
|
||||
*/
|
||||
|
||||
/// If the Json value is an Object, returns the value associated with the provided key.
|
||||
/// Otherwise, returns None.
|
||||
@ -412,12 +415,12 @@ impl Json {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, S: ser::Serializer<E>> ser::Serializable<E, S> for Json {
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Json {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
match *self {
|
||||
Null => {
|
||||
s.serialize(ser::Null)
|
||||
().serialize(s)
|
||||
}
|
||||
Boolean(v) => {
|
||||
v.serialize(s)
|
||||
@ -750,15 +753,15 @@ fn spaces(n: uint) -> String {
|
||||
#[deriving(Show)]
|
||||
enum SerializerState {
|
||||
ValueState,
|
||||
SeqState(bool),
|
||||
MapState(bool),
|
||||
EnumState(bool),
|
||||
TupleState,
|
||||
StructState,
|
||||
EnumState,
|
||||
}
|
||||
|
||||
/// A structure for implementing serialization to JSON.
|
||||
pub struct Serializer<'a> {
|
||||
wr: &'a mut Writer,
|
||||
state_stack: Vec<SerializerState>,
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<'a> Serializer<'a> {
|
||||
@ -767,19 +770,22 @@ impl<'a> Serializer<'a> {
|
||||
pub fn new(wr: &'a mut Writer) -> Serializer<'a> {
|
||||
Serializer {
|
||||
wr: wr,
|
||||
state_stack: vec!(ValueState),
|
||||
first: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json [u8]
|
||||
pub fn buf_encode<
|
||||
'a,
|
||||
T: ser::Serializable<io::IoError, Serializer<'a>>
|
||||
T: ser::Serializable<Serializer<'a>, io::IoError>
|
||||
>(value: &T) -> Vec<u8> {
|
||||
//Serialize the object in a string using a writer
|
||||
let mut wr = MemWriter::new();
|
||||
{
|
||||
// FIXME(14302) remove the transmute and unsafe block.
|
||||
unsafe {
|
||||
let mut serializer = Serializer::new(&mut wr);
|
||||
value.serialize(&mut serializer).unwrap();
|
||||
// MemWriter never Errs
|
||||
let _ = value.serialize(transmute(&mut serializer));
|
||||
}
|
||||
wr.unwrap()
|
||||
}
|
||||
@ -787,54 +793,13 @@ impl<'a> Serializer<'a> {
|
||||
/// Encode the specified struct into a json str
|
||||
pub fn str_encode<
|
||||
'a,
|
||||
T: ser::Serializable<io::IoError, Serializer<'a>>
|
||||
T: ser::Serializable<Serializer<'a>, io::IoError>
|
||||
>(value: &T) -> Result<String, Vec<u8>> {
|
||||
let buf = Serializer::buf_encode(value);
|
||||
String::from_utf8(buf)
|
||||
}
|
||||
|
||||
fn serialize_value<'a>(&mut self, token: ser::Token<'a>) -> Result<(), io::IoError> {
|
||||
match token {
|
||||
ser::Null => { write!(self.wr, "null") }
|
||||
ser::Bool(true) => { write!(self.wr, "true") }
|
||||
ser::Bool(false) => { write!(self.wr, "false") }
|
||||
ser::Int(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::I8(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::I16(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::I32(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::I64(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::Uint(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::U8(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::U16(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::U32(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::U64(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::F32(v) => { write!(self.wr, "{}", f64::to_str_digits(v as f64, 6)) }
|
||||
ser::F64(v) => { write!(self.wr, "{}", f64::to_str_digits(v, 6)) }
|
||||
ser::Char(v) => { write!(self.wr, "{}", escape_str(str::from_char(v).as_slice())) }
|
||||
ser::Str(v) => { write!(self.wr, "{}", escape_str(v)) }
|
||||
ser::Option(false) => { write!(self.wr, "null") }
|
||||
ser::Option(true) => {
|
||||
self.state_stack.push(ValueState);
|
||||
Ok(())
|
||||
}
|
||||
ser::TupleStart(_)
|
||||
| ser::SeqStart(_) => {
|
||||
self.state_stack.push(SeqState(true));
|
||||
write!(self.wr, "[")
|
||||
}
|
||||
ser::StructStart(_, _)
|
||||
| ser::MapStart(_) => {
|
||||
self.state_stack.push(MapState(true));
|
||||
write!(self.wr, "{{")
|
||||
}
|
||||
ser::EnumStart(_, variant, _) => {
|
||||
self.state_stack.push(EnumState(true));
|
||||
write!(self.wr, "{{{}:[", escape_str(variant))
|
||||
}
|
||||
ser::End => { fail!("not implemented") }
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
fn serialize_seq<'a>(&mut self, token: ser::Token<'a>, first: bool) -> Result<(), io::IoError> {
|
||||
match token {
|
||||
ser::End => {
|
||||
@ -885,20 +850,250 @@ impl<'a> Serializer<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl<'a> ser::Serializer<io::IoError> for Serializer<'a> {
|
||||
fn serialize<'a>(&mut self, token: ser::Token<'a>) -> Result<(), io::IoError> {
|
||||
match self.state_stack.pop() {
|
||||
Some(ValueState) => self.serialize_value(token),
|
||||
Some(SeqState(v)) => self.serialize_seq(token, v),
|
||||
Some(MapState(v)) => self.serialize_map(token, v),
|
||||
Some(EnumState(v)) => self.serialize_enum(token, v),
|
||||
None => { fail!("internal state machine error") }
|
||||
#[inline]
|
||||
fn serialize_null(&mut self) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "null")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), io::IoError> {
|
||||
if v {
|
||||
write!(self.wr, "true")
|
||||
} else {
|
||||
write!(self.wr, "false")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "{}", f64::to_str_digits(v as f64, 6))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "{}", escape_str(str::from_char(v).as_slice()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "{}", escape_str(v))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_start(&mut self, _len: uint) -> Result<(), io::IoError> {
|
||||
//self.state_stack.push(TupleState);
|
||||
self.first = true;
|
||||
write!(self.wr, "[")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_sep(&mut self) -> Result<(), io::IoError> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
Ok(())
|
||||
} else {
|
||||
write!(self.wr, ",")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "]")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_start(&mut self, _name: &str, _len: uint) -> Result<(), io::IoError> {
|
||||
self.first = true;
|
||||
write!(self.wr, "{{")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_sep(&mut self, name: &str) -> Result<(), io::IoError> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
try!(write!(self.wr, ","));
|
||||
}
|
||||
try!(name.serialize(self));
|
||||
write!(self.wr, ":")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_end(&mut self) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "}}")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: uint) -> Result<(), io::IoError> {
|
||||
self.first = true;
|
||||
write!(self.wr, "{{{}:[", escape_str(variant))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_sep(&mut self) -> Result<(), io::IoError> {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
Ok(())
|
||||
} else {
|
||||
write!(self.wr, ",")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_end(&mut self) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "]}}")
|
||||
}
|
||||
|
||||
/*
|
||||
#[inline]
|
||||
fn serialize_sep(&mut self) -> Result<(), io::IoError> {
|
||||
match self.state_stack.last() {
|
||||
Some(&TupleState) | Some(&StructState) | Some(&EnumState) => {
|
||||
write!(self.wr, ",")
|
||||
}
|
||||
Some(&ValueState) | None => {
|
||||
fail!()
|
||||
//Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), io::IoError> {
|
||||
match self.state_stack.pop() {
|
||||
Some(TupleState) => {
|
||||
write!(self.wr, "]")
|
||||
}
|
||||
Some(StructState) => {
|
||||
write!(self.wr, "}}")
|
||||
}
|
||||
Some(EnumState) => {
|
||||
write!(self.wr, "]}}")
|
||||
}
|
||||
Some(ValueState) | None => {
|
||||
fail!()
|
||||
//Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#[inline]
|
||||
fn serialize_option<
|
||||
T: Serializable<Serializer<'a>, io::IoError>
|
||||
>(&mut self, v: &Option<T>) -> Result<(), io::IoError> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
v.serialize(self)
|
||||
}
|
||||
None => {
|
||||
self.serialize_null()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq<
|
||||
T: Serializable<Serializer<'a>, io::IoError>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> Result<(), io::IoError> {
|
||||
try!(write!(self.wr, "["));
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
try!(write!(self.wr, ","));
|
||||
}
|
||||
try!(elt.serialize(self));
|
||||
|
||||
}
|
||||
write!(self.wr, "]")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map<
|
||||
K: Serializable<Serializer<'a>, io::IoError>,
|
||||
V: Serializable<Serializer<'a>, io::IoError>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> Result<(), io::IoError> {
|
||||
try!(write!(self.wr, "{{"));
|
||||
let mut first = true;
|
||||
for (key, value) in iter {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
try!(write!(self.wr, ","));
|
||||
}
|
||||
try!(key.serialize(self));
|
||||
try!(write!(self.wr, ":"));
|
||||
try!(value.serialize(self));
|
||||
|
||||
}
|
||||
write!(self.wr, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// Another serializer for JSON, but prints out human-readable JSON instead of
|
||||
/// compact data
|
||||
pub struct PrettySerializer<'a> {
|
||||
@ -917,10 +1112,10 @@ impl<'a> PrettySerializer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// Encode the specified struct into a json [u8]
|
||||
pub fn buf_encode<
|
||||
'a,
|
||||
T: ser::Serializable<io::IoError, PrettySerializer<'a>>
|
||||
T: ser::Serializable<PrettySerializer<'a>, io::IoError>
|
||||
>(value: &T) -> Vec<u8> {
|
||||
let mut wr = MemWriter::new();
|
||||
{
|
||||
@ -932,13 +1127,14 @@ impl<'a> PrettySerializer<'a> {
|
||||
|
||||
/// Encode the specified struct into a json str
|
||||
pub fn str_encode<
|
||||
'a,
|
||||
T: ser::Serializable<io::IoError, PrettySerializer<'a>>
|
||||
T: ser::Serializable<PrettySerializer<'a>, io::IoError>
|
||||
>(value: &T) -> Result<String, Vec<u8>> {
|
||||
let buf = PrettySerializer::buf_encode(value);
|
||||
String::from_utf8(buf)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
fn serialize_value<'a>(&mut self, token: ser::Token<'a>) -> Result<(), io::IoError> {
|
||||
match token {
|
||||
ser::Null => { write!(self.wr, "null") }
|
||||
@ -1063,20 +1259,200 @@ impl<'a> PrettySerializer<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl<'a> ser::Serializer<io::IoError> for PrettySerializer<'a> {
|
||||
fn serialize<'a>(&mut self, token: ser::Token<'a>) -> Result<(), io::IoError> {
|
||||
match self.state_stack.pop() {
|
||||
Some(ValueState) => self.serialize_value(token),
|
||||
Some(SeqState(v)) => self.serialize_seq(token, v),
|
||||
Some(MapState(v)) => self.serialize_map(token, v),
|
||||
Some(EnumState(v)) => self.serialize_enum(token, v),
|
||||
None => { fail!("internal state machine error") }
|
||||
#[inline]
|
||||
fn serialize_null(&mut self) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "null")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), io::IoError> {
|
||||
if v {
|
||||
write!(self.wr, "true")
|
||||
} else {
|
||||
write!(self.wr, "false")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), io::IoError> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "{}", f64::to_str_digits(v as f64, 6))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "{}", escape_str(str::from_char(v).as_slice()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), io::IoError> {
|
||||
write!(self.wr, "{}", escape_str(v))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_tuple_start(&mut self, _len: uint) -> Result<(), io::IoError> {
|
||||
self.state_stack.push(TupleState);
|
||||
write!(self.wr, "[")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_struct_start(&mut self, _name: &str, _len: uint) -> Result<(), io::IoError> {
|
||||
self.state_stack.push(StructState);
|
||||
write!(self.wr, "{{")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_enum_start(&mut self, _name: &str, variant: &str, _len: uint) -> Result<(), io::IoError> {
|
||||
self.state_stack.push(EnumState);
|
||||
write!(self.wr, "{{{}:[", escape_str(variant))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_sep(&mut self) -> Result<(), io::IoError> {
|
||||
match self.state_stack.last() {
|
||||
Some(&TupleState) | Some(&StructState) | Some(&EnumState) => {
|
||||
write!(self.wr, ",")
|
||||
}
|
||||
Some(&ValueState) | None => {
|
||||
fail!()
|
||||
//Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_end(&mut self) -> Result<(), io::IoError> {
|
||||
match self.state_stack.pop() {
|
||||
Some(TupleState) => {
|
||||
write!(self.wr, "]")
|
||||
}
|
||||
Some(StructState) => {
|
||||
write!(self.wr, "}}")
|
||||
}
|
||||
Some(EnumState) => {
|
||||
write!(self.wr, "]}}")
|
||||
}
|
||||
Some(ValueState) | None => {
|
||||
fail!()
|
||||
//Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_option<
|
||||
T: Serializable<PrettySerializer<'a>, io::IoError>
|
||||
>(&mut self, v: &Option<T>) -> Result<(), io::IoError> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
v.serialize(self)
|
||||
}
|
||||
None => {
|
||||
self.serialize_null()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_seq<
|
||||
T: Serializable<PrettySerializer<'a>, io::IoError>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, mut iter: Iter) -> Result<(), io::IoError> {
|
||||
try!(write!(self.wr, "["));
|
||||
let mut first = true;
|
||||
for elt in iter {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
try!(write!(self.wr, ","));
|
||||
}
|
||||
try!(elt.serialize(self));
|
||||
|
||||
}
|
||||
write!(self.wr, "]")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_map<
|
||||
K: Serializable<PrettySerializer<'a>, io::IoError>,
|
||||
V: Serializable<PrettySerializer<'a>, io::IoError>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: Iter) -> Result<(), io::IoError> {
|
||||
try!(write!(self.wr, "{{"));
|
||||
let mut first = true;
|
||||
for (key, value) in iter {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
try!(write!(self.wr, ","));
|
||||
}
|
||||
try!(key.serialize(self));
|
||||
try!(write!(self.wr, ":"));
|
||||
try!(value.serialize(self));
|
||||
|
||||
}
|
||||
write!(self.wr, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
/// The output of the streaming parser.
|
||||
@ -2004,6 +2380,7 @@ impl fmt::Show for Json {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io;
|
||||
@ -2045,19 +2422,19 @@ mod tests {
|
||||
Frog(String, int)
|
||||
}
|
||||
|
||||
impl<E, S: ser::Serializer<E>> ser::Serializable<E, S> for Animal {
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Animal {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
match *self {
|
||||
Dog => {
|
||||
try!(s.serialize(ser::EnumStart("Animal", "Dog", 0)));
|
||||
s.serialize(ser::End)
|
||||
try!(s.serialize_enum_start("Animal", "Dog", 0));
|
||||
s.serialize_end()
|
||||
}
|
||||
Frog(ref x0, x1) => {
|
||||
try!(s.serialize(ser::EnumStart("Animal", "Frog", 2)));
|
||||
try!(s.serialize_enum_start("Animal", "Frog", 2));
|
||||
try!(x0.serialize(s));
|
||||
try!(x1.serialize(s));
|
||||
s.serialize(ser::End)
|
||||
s.serialize_end()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2112,14 +2489,14 @@ mod tests {
|
||||
c: Vec<String>,
|
||||
}
|
||||
|
||||
impl<E, S: ser::Serializer<E>> ser::Serializable<E, S> for Inner {
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Inner {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(ser::StructStart("Inner", 3)));
|
||||
try!(s.serialize_struct_start("Inner", 3));
|
||||
try!(self.a.serialize(s));
|
||||
try!(self.b.serialize(s));
|
||||
try!(self.c.serialize(s));
|
||||
s.serialize(ser::End)
|
||||
s.serialize_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -2197,12 +2574,12 @@ mod tests {
|
||||
inner: Vec<Inner>,
|
||||
}
|
||||
|
||||
impl<E, S: ser::Serializer<E>> ser::Serializable<E, S> for Outer {
|
||||
impl<S: ser::Serializer<E>, E> ser::Serializable<S, E> for Outer {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(ser::StructStart("Outer", 1)));
|
||||
try!(s.serialize_struct_start("Outer", 1));
|
||||
try!(self.inner.serialize(s));
|
||||
s.serialize(ser::End)
|
||||
s.serialize_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -3685,3 +4062,4 @@ mod bench {
|
||||
bench_deserializer_streaming(b, 500)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
520
ser.rs
520
ser.rs
@ -11,95 +11,143 @@
|
||||
use std::collections::{HashMap, TreeMap};
|
||||
use std::hash::Hash;
|
||||
|
||||
#[deriving(Clone, PartialEq, Show)]
|
||||
pub enum Token<'a> {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int(int),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
Uint(uint),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'a str),
|
||||
Option(bool),
|
||||
|
||||
TupleStart(uint),
|
||||
StructStart(&'a str, uint),
|
||||
EnumStart(&'a str, &'a str, uint),
|
||||
SeqStart(uint),
|
||||
MapStart(uint),
|
||||
|
||||
End,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializer<E> {
|
||||
fn serialize<'a>(&mut self, token: Token<'a>) -> Result<(), E>;
|
||||
fn serialize_null(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), E>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), E>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), E>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), E> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), E>;
|
||||
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), E>;
|
||||
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), E>;
|
||||
|
||||
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), E>;
|
||||
fn serialize_tuple_sep(&mut self) -> Result<(), E>;
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), E>;
|
||||
fn serialize_struct_sep(&mut self, name: &str) -> Result<(), E>;
|
||||
fn serialize_struct_end(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), E>;
|
||||
fn serialize_enum_sep(&mut self) -> Result<(), E>;
|
||||
fn serialize_enum_end(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_option<
|
||||
T: Serializable<Self, E>
|
||||
>(&mut self, v: &Option<T>) -> Result<(), E>;
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serializable<Self, E>,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, iter: Iter) -> Result<(), E>;
|
||||
|
||||
fn serialize_map<
|
||||
K: Serializable<Self, E>,
|
||||
V: Serializable<Self, E>,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, iter: Iter) -> Result<(), E>;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializable<E, S: Serializer<E>> {
|
||||
pub trait Serializable<S: Serializer<E>, E> {
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E>;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! impl_serializable {
|
||||
($ty:ty, $variant:expr) => {
|
||||
impl<'a, E, S: Serializer<E>> Serializable<E, S> for $ty {
|
||||
($ty:ty, $method:ident, $variant:expr) => {
|
||||
impl<'a, S: Serializer<E>, E> Serializable<S, E> for $ty {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize($variant)
|
||||
s.$method($variant)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_serializable!(bool, Bool(*self))
|
||||
impl_serializable!(int, Int(*self))
|
||||
impl_serializable!(i8, I8(*self))
|
||||
impl_serializable!(i16, I16(*self))
|
||||
impl_serializable!(i32, I32(*self))
|
||||
impl_serializable!(i64, I64(*self))
|
||||
impl_serializable!(uint, Uint(*self))
|
||||
impl_serializable!(u8, U8(*self))
|
||||
impl_serializable!(u16, U16(*self))
|
||||
impl_serializable!(u32, U32(*self))
|
||||
impl_serializable!(u64, U64(*self))
|
||||
impl_serializable!(f32, F32(*self))
|
||||
impl_serializable!(f64, F64(*self))
|
||||
impl_serializable!(char, Char(*self))
|
||||
impl_serializable!(&'a str, Str(*self))
|
||||
impl_serializable!(String, Str(self.as_slice()))
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
impl_serializable!(bool, serialize_bool, *self)
|
||||
impl_serializable!(int, serialize_int, *self)
|
||||
impl_serializable!(i8, serialize_i8, *self)
|
||||
impl_serializable!(i16, serialize_i16, *self)
|
||||
impl_serializable!(i32, serialize_i32, *self)
|
||||
impl_serializable!(i64, serialize_i64, *self)
|
||||
impl_serializable!(uint, serialize_uint, *self)
|
||||
impl_serializable!(u8, serialize_u8, *self)
|
||||
impl_serializable!(u16, serialize_u16, *self)
|
||||
impl_serializable!(u32, serialize_u32, *self)
|
||||
impl_serializable!(u64, serialize_u64, *self)
|
||||
impl_serializable!(f32, serialize_f32, *self)
|
||||
impl_serializable!(f64, serialize_f64, *self)
|
||||
impl_serializable!(char, serialize_char, *self)
|
||||
impl_serializable!(&'a str, serialize_str, *self)
|
||||
impl_serializable!(String, serialize_str, self.as_slice())
|
||||
|
||||
impl<
|
||||
'a,
|
||||
E,
|
||||
S: Serializer<E>,
|
||||
T: Serializable<E, S>
|
||||
> Serializable<E, S> for Option<T> {
|
||||
T: Serializable<S, E>
|
||||
> Serializable<S, E> for &'a T {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
match *self {
|
||||
Some(ref value) => {
|
||||
try!(s.serialize(Option(true)));
|
||||
value.serialize(s)
|
||||
}
|
||||
None => {
|
||||
s.serialize(Option(false))
|
||||
}
|
||||
}
|
||||
(*self).serialize(s)
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,15 +156,11 @@ impl<
|
||||
impl<
|
||||
E,
|
||||
S: Serializer<E>,
|
||||
T: Serializable<E, S>
|
||||
> Serializable<E, S> for Vec<T> {
|
||||
T: Serializable<S, E>
|
||||
> Serializable<S, E> for Option<T> {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(SeqStart(self.len())));
|
||||
for elt in self.iter() {
|
||||
try!(elt.serialize(s));
|
||||
}
|
||||
s.serialize(End)
|
||||
s.serialize_option(self)
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,34 +169,37 @@ impl<
|
||||
impl<
|
||||
E,
|
||||
S: Serializer<E>,
|
||||
K: Serializable<E, S> + Eq + Hash,
|
||||
V: Serializable<E, S>
|
||||
> Serializable<E, S> for HashMap<K, V> {
|
||||
T: Serializable<S, E>
|
||||
> Serializable<S, E> for Vec<T> {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(MapStart(self.len())));
|
||||
for (key, value) in self.iter() {
|
||||
try!(key.serialize(s));
|
||||
try!(value.serialize(s));
|
||||
s.serialize_seq(self.iter())
|
||||
}
|
||||
s.serialize(End)
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
E,
|
||||
S: Serializer<E>,
|
||||
K: Serializable<S, E> + Eq + Hash,
|
||||
V: Serializable<S, E>
|
||||
> Serializable<S, E> for HashMap<K, V> {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_map(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
E,
|
||||
S: Serializer<E>,
|
||||
K: Serializable<E, S> + Ord,
|
||||
V: Serializable<E, S>
|
||||
> Serializable<E, S> for TreeMap<K, V> {
|
||||
K: Serializable<S, E> + Ord,
|
||||
V: Serializable<S, E>
|
||||
> Serializable<S, E> for TreeMap<K, V> {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(MapStart(self.len())));
|
||||
for (key, value) in self.iter() {
|
||||
try!(key.serialize(s));
|
||||
try!(value.serialize(s));
|
||||
}
|
||||
s.serialize(End)
|
||||
s.serialize_map(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,10 +214,10 @@ macro_rules! impl_serialize_tuple {
|
||||
impl<
|
||||
E,
|
||||
S: Serializer<E>
|
||||
> Serializable<E, S> for () {
|
||||
> Serializable<S, E> for () {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize(Null)
|
||||
s.serialize_null()
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -178,8 +225,8 @@ macro_rules! impl_serialize_tuple {
|
||||
impl<
|
||||
E,
|
||||
S: Serializer<E>,
|
||||
$($name:Serializable<E, S>),*
|
||||
> Serializable<E, S> for ($($name,)*) {
|
||||
$($name:Serializable<S, E>),*
|
||||
> Serializable<S, E> for ($($name,)*) {
|
||||
#[inline]
|
||||
#[allow(uppercase_variables)]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
@ -189,9 +236,9 @@ macro_rules! impl_serialize_tuple {
|
||||
|
||||
let ($(ref $name,)*) = *self;
|
||||
|
||||
try!(s.serialize(TupleStart(len)));
|
||||
try!(s.serialize_tuple_start(len));
|
||||
$(try!($name.serialize(s));)*
|
||||
s.serialize(End)
|
||||
s.serialize_tuple_end()
|
||||
}
|
||||
}
|
||||
peel!($($name,)*)
|
||||
@ -208,9 +255,6 @@ mod tests {
|
||||
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Token, Null, Int, Uint, Str, Char, Option};
|
||||
use super::{TupleStart, StructStart, EnumStart};
|
||||
use super::{SeqStart, MapStart, End};
|
||||
use super::{Serializer, Serializable};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@ -222,17 +266,21 @@ mod tests {
|
||||
c: HashMap<String, Option<char>>,
|
||||
}
|
||||
|
||||
impl<E, S: Serializer<E>> Serializable<E, S> for Inner {
|
||||
impl<S: Serializer<E>, E> Serializable<S, E> for Inner {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(StructStart("Inner", 3)));
|
||||
try!(s.serialize(Str("a")));
|
||||
try!(s.serialize_struct_start("Inner", 3));
|
||||
|
||||
try!(s.serialize_struct_sep("a"));
|
||||
try!(self.a.serialize(s));
|
||||
try!(s.serialize(Str("b")));
|
||||
|
||||
try!(s.serialize_struct_sep("b"));
|
||||
try!(self.b.serialize(s));
|
||||
try!(s.serialize(Str("c")));
|
||||
|
||||
try!(s.serialize_struct_sep("c"));
|
||||
try!(self.c.serialize(s));
|
||||
s.serialize(End)
|
||||
|
||||
s.serialize_struct_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,13 +291,15 @@ mod tests {
|
||||
inner: Vec<Inner>,
|
||||
}
|
||||
|
||||
impl<E, S: Serializer<E>> Serializable<E, S> for Outer {
|
||||
impl<S: Serializer<E>, E> Serializable<S, E> for Outer {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
try!(s.serialize(StructStart("Outer", 1)));
|
||||
try!(s.serialize(Str("inner")));
|
||||
try!(s.serialize_struct_start("Outer", 1));
|
||||
|
||||
try!(s.serialize_struct_sep("inner"));
|
||||
try!(self.inner.serialize(s));
|
||||
s.serialize(End)
|
||||
|
||||
s.serialize_enum_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -261,25 +311,69 @@ mod tests {
|
||||
Frog(String, int)
|
||||
}
|
||||
|
||||
impl<E, S: Serializer<E>> Serializable<E, S> for Animal {
|
||||
impl<S: Serializer<E>, E> Serializable<S, E> for Animal {
|
||||
#[inline]
|
||||
fn serialize(&self, s: &mut S) -> Result<(), E> {
|
||||
match *self {
|
||||
Dog => {
|
||||
try!(s.serialize(EnumStart("Animal", "Dog", 0)));
|
||||
try!(s.serialize_enum_start("Animal", "Dog", 0));
|
||||
}
|
||||
Frog(ref x, y) => {
|
||||
try!(s.serialize(EnumStart("Animal", "Frog", 2)));
|
||||
try!(s.serialize_enum_start("Animal", "Frog", 2));
|
||||
|
||||
try!(s.serialize_enum_sep());
|
||||
try!(x.serialize(s));
|
||||
|
||||
try!(s.serialize_enum_sep());
|
||||
try!(y.serialize(s));
|
||||
}
|
||||
}
|
||||
s.serialize(End)
|
||||
s.serialize_enum_end()
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show)]
|
||||
pub enum Token<'a> {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int(int),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
Uint(uint),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'a str),
|
||||
|
||||
TupleStart(uint),
|
||||
TupleSep,
|
||||
TupleEnd,
|
||||
|
||||
StructStart(&'a str, uint),
|
||||
StructSep(&'a str),
|
||||
StructEnd,
|
||||
|
||||
EnumStart(&'a str, &'a str, uint),
|
||||
EnumSep,
|
||||
EnumEnd,
|
||||
|
||||
Option(bool),
|
||||
|
||||
SeqStart(uint),
|
||||
SeqEnd,
|
||||
|
||||
MapStart(uint),
|
||||
MapEnd,
|
||||
}
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
@ -293,16 +387,12 @@ mod tests {
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Token<'a>>> AssertSerializer<Iter> {
|
||||
#[inline]
|
||||
fn new(iter: Iter) -> AssertSerializer<Iter> {
|
||||
AssertSerializer {
|
||||
iter: iter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Token<'a>>> Serializer<Error> for AssertSerializer<Iter> {
|
||||
#[inline]
|
||||
fn serialize<'b>(&mut self, token: Token<'b>) -> Result<(), Error> {
|
||||
let t = match self.iter.next() {
|
||||
Some(t) => t,
|
||||
@ -315,6 +405,146 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Token<'a>>> Serializer<Error> for AssertSerializer<Iter> {
|
||||
fn serialize_null(&mut self) -> Result<(), Error> {
|
||||
self.serialize(Null)
|
||||
}
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||
self.serialize(Bool(v))
|
||||
}
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), Error> {
|
||||
self.serialize(Int(v))
|
||||
}
|
||||
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
|
||||
self.serialize(I8(v))
|
||||
}
|
||||
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
|
||||
self.serialize(I16(v))
|
||||
}
|
||||
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
|
||||
self.serialize(I32(v))
|
||||
}
|
||||
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
|
||||
self.serialize(I64(v))
|
||||
}
|
||||
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), Error> {
|
||||
self.serialize(Uint(v))
|
||||
}
|
||||
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
|
||||
self.serialize(U8(v))
|
||||
}
|
||||
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
|
||||
self.serialize(U16(v))
|
||||
}
|
||||
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
|
||||
self.serialize(U32(v))
|
||||
}
|
||||
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
||||
self.serialize(U64(v))
|
||||
}
|
||||
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
|
||||
self.serialize(F32(v))
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
||||
self.serialize(F64(v))
|
||||
}
|
||||
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), Error> {
|
||||
self.serialize(Char(v))
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), Error> {
|
||||
self.serialize(Str(v))
|
||||
}
|
||||
|
||||
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), Error> {
|
||||
self.serialize(TupleStart(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_sep(&mut self) -> Result<(), Error> {
|
||||
self.serialize(TupleSep)
|
||||
}
|
||||
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(TupleEnd)
|
||||
}
|
||||
|
||||
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), Error> {
|
||||
self.serialize(StructStart(name, len))
|
||||
}
|
||||
|
||||
fn serialize_struct_sep(&mut self, name: &str) -> Result<(), Error> {
|
||||
self.serialize(StructSep(name))
|
||||
}
|
||||
|
||||
fn serialize_struct_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(StructEnd)
|
||||
}
|
||||
|
||||
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), Error> {
|
||||
self.serialize(EnumStart(name, variant, len))
|
||||
}
|
||||
|
||||
fn serialize_enum_sep(&mut self) -> Result<(), Error> {
|
||||
self.serialize(EnumSep)
|
||||
}
|
||||
|
||||
fn serialize_enum_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(EnumEnd)
|
||||
}
|
||||
|
||||
fn serialize_option<
|
||||
T: Serializable<AssertSerializer<Iter>, Error>
|
||||
>(&mut self, v: &Option<T>) -> Result<(), Error> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
try!(self.serialize(Option(true)));
|
||||
v.serialize(self)
|
||||
}
|
||||
None => {
|
||||
self.serialize(Option(false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serializable<AssertSerializer<Iter>, Error>,
|
||||
SeqIter: Iterator<T>
|
||||
>(&mut self, mut iter: SeqIter) -> Result<(), Error> {
|
||||
let (len, _) = iter.size_hint();
|
||||
try!(self.serialize(SeqStart(len)));
|
||||
for elt in iter {
|
||||
elt.serialize(self).unwrap();
|
||||
}
|
||||
self.serialize(SeqEnd)
|
||||
}
|
||||
|
||||
fn serialize_map<
|
||||
K: Serializable<AssertSerializer<Iter>, Error>,
|
||||
V: Serializable<AssertSerializer<Iter>, Error>,
|
||||
MapIter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: MapIter) -> Result<(), Error> {
|
||||
let (len, _) = iter.size_hint();
|
||||
try!(self.serialize(MapStart(len)));
|
||||
for (key, value) in iter {
|
||||
try!(key.serialize(self));
|
||||
try!(value.serialize(self));
|
||||
}
|
||||
self.serialize(MapEnd)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[test]
|
||||
@ -376,9 +606,12 @@ mod tests {
|
||||
fn test_tokens_tuple() {
|
||||
let tokens = vec!(
|
||||
TupleStart(2),
|
||||
TupleSep,
|
||||
Int(5),
|
||||
|
||||
TupleSep,
|
||||
Str("a"),
|
||||
End,
|
||||
TupleEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -390,14 +623,21 @@ mod tests {
|
||||
fn test_tokens_tuple_compound() {
|
||||
let tokens = vec!(
|
||||
TupleStart(3),
|
||||
Null,
|
||||
TupleSep,
|
||||
Null,
|
||||
|
||||
TupleSep,
|
||||
Null,
|
||||
|
||||
TupleSep,
|
||||
TupleStart(2),
|
||||
TupleSep,
|
||||
Int(5),
|
||||
|
||||
TupleSep,
|
||||
Str("a"),
|
||||
End,
|
||||
End,
|
||||
TupleEnd,
|
||||
TupleEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -409,10 +649,10 @@ mod tests {
|
||||
fn test_tokens_struct_empty() {
|
||||
let tokens = vec!(
|
||||
StructStart("Outer", 1),
|
||||
Str("inner"),
|
||||
StructSep("inner"),
|
||||
SeqStart(0),
|
||||
End,
|
||||
End,
|
||||
SeqEnd,
|
||||
StructEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -424,25 +664,24 @@ mod tests {
|
||||
fn test_tokens_struct() {
|
||||
let tokens = vec!(
|
||||
StructStart("Outer", 1),
|
||||
Str("inner"),
|
||||
StructSep("inner"),
|
||||
SeqStart(1),
|
||||
StructStart("Inner", 3),
|
||||
Str("a"),
|
||||
StructSep("a"),
|
||||
Null,
|
||||
|
||||
Str("b"),
|
||||
StructSep("b"),
|
||||
Uint(5),
|
||||
|
||||
Str("c"),
|
||||
StructSep("c"),
|
||||
MapStart(1),
|
||||
Str("abc"),
|
||||
|
||||
Option(true),
|
||||
Char('c'),
|
||||
End,
|
||||
End,
|
||||
End,
|
||||
End,
|
||||
MapEnd,
|
||||
StructEnd,
|
||||
StructEnd,
|
||||
StructEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -466,7 +705,7 @@ mod tests {
|
||||
fn test_tokens_enum() {
|
||||
let tokens = vec!(
|
||||
EnumStart("Animal", "Dog", 0),
|
||||
End,
|
||||
EnumEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -475,9 +714,12 @@ mod tests {
|
||||
|
||||
let tokens = vec!(
|
||||
EnumStart("Animal", "Frog", 2),
|
||||
EnumSep,
|
||||
Str("Henry"),
|
||||
|
||||
EnumSep,
|
||||
Int(349),
|
||||
End,
|
||||
EnumEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -489,7 +731,7 @@ mod tests {
|
||||
fn test_tokens_vec_empty() {
|
||||
let tokens = vec!(
|
||||
SeqStart(0),
|
||||
End,
|
||||
SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -505,7 +747,7 @@ mod tests {
|
||||
Int(5),
|
||||
Int(6),
|
||||
Int(7),
|
||||
End,
|
||||
SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -519,19 +761,19 @@ mod tests {
|
||||
SeqStart(3),
|
||||
SeqStart(1),
|
||||
Int(1),
|
||||
End,
|
||||
SeqEnd,
|
||||
|
||||
SeqStart(2),
|
||||
Int(2),
|
||||
Int(3),
|
||||
End,
|
||||
SeqEnd,
|
||||
|
||||
SeqStart(3),
|
||||
Int(4),
|
||||
Int(5),
|
||||
Int(6),
|
||||
End,
|
||||
End,
|
||||
SeqEnd,
|
||||
SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
@ -548,7 +790,7 @@ mod tests {
|
||||
|
||||
Int(6),
|
||||
Str("b"),
|
||||
End,
|
||||
MapEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
|
Loading…
x
Reference in New Issue
Block a user