2014-04-02 23:31:00 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-29 19:06:37 -05:00
|
|
|
extern crate rbml;
|
2014-04-08 16:31:25 -05:00
|
|
|
extern crate serialize;
|
2014-04-02 23:31:00 -05:00
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
use std::io;
|
|
|
|
use std::io::{IoError, IoResult, SeekStyle};
|
|
|
|
use std::slice;
|
|
|
|
|
2014-04-08 16:31:25 -05:00
|
|
|
use serialize::{Encodable, Encoder};
|
|
|
|
use serialize::json;
|
2014-07-29 18:31:39 -05:00
|
|
|
|
2014-07-29 20:27:28 -05:00
|
|
|
use rbml::writer;
|
|
|
|
use rbml::io::SeekableMemWriter;
|
2014-04-02 23:31:00 -05:00
|
|
|
|
|
|
|
#[deriving(Encodable)]
|
|
|
|
struct Foo {
|
|
|
|
baz: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Encodable)]
|
|
|
|
struct Bar {
|
|
|
|
froboz: uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum WireProtocol {
|
|
|
|
JSON,
|
2014-07-29 19:06:37 -05:00
|
|
|
RBML,
|
2014-04-02 23:31:00 -05:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_json<'a,
|
|
|
|
T: Encodable<json::Encoder<'a>,
|
|
|
|
std::io::IoError>>(val: &T,
|
2014-07-29 18:31:39 -05:00
|
|
|
wr: &'a mut SeekableMemWriter) {
|
2014-04-02 23:31:00 -05:00
|
|
|
let mut encoder = json::Encoder::new(wr);
|
|
|
|
val.encode(&mut encoder);
|
|
|
|
}
|
2014-07-29 19:06:37 -05:00
|
|
|
fn encode_rbml<'a,
|
2014-07-29 18:31:39 -05:00
|
|
|
T: Encodable<writer::Encoder<'a, SeekableMemWriter>,
|
2014-04-02 23:31:00 -05:00
|
|
|
std::io::IoError>>(val: &T,
|
2014-07-29 18:31:39 -05:00
|
|
|
wr: &'a mut SeekableMemWriter) {
|
2014-05-25 20:32:04 -05:00
|
|
|
let mut encoder = writer::Encoder::new(wr);
|
2014-04-02 23:31:00 -05:00
|
|
|
val.encode(&mut encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let target = Foo{baz: false,};
|
2014-07-29 18:31:39 -05:00
|
|
|
let mut wr = SeekableMemWriter::new();
|
2014-04-02 23:31:00 -05:00
|
|
|
let proto = JSON;
|
|
|
|
match proto {
|
|
|
|
JSON => encode_json(&target, &mut wr),
|
2014-07-29 19:06:37 -05:00
|
|
|
RBML => encode_rbml(&target, &mut wr)
|
2014-04-02 23:31:00 -05:00
|
|
|
}
|
|
|
|
}
|