mirror of https://git.sr.ht/~stygianentity/bincode
renamed project to binary_encode
This commit is contained in:
parent
e13a2a42b1
commit
9bd822ecf8
|
|
@ -1,5 +1,4 @@
|
||||||
[package]
|
[package]
|
||||||
|
name = "binary_encode"
|
||||||
name = "writer_encoder"
|
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["Ty Overby <ty@pre-alpha.com>"]
|
authors = ["Ty Overby <ty@pre-alpha.com>"]
|
||||||
|
|
|
||||||
14
src/lib.rs
14
src/lib.rs
|
|
@ -7,8 +7,6 @@ use std::io::MemReader;
|
||||||
use std::io::IoError;
|
use std::io::IoError;
|
||||||
use serialize::Encodable;
|
use serialize::Encodable;
|
||||||
use serialize::Decodable;
|
use serialize::Decodable;
|
||||||
use serialize::Encoder;
|
|
||||||
use serialize::Decoder;
|
|
||||||
|
|
||||||
pub use writer::EncoderWriter;
|
pub use writer::EncoderWriter;
|
||||||
pub use reader::DecoderReader;
|
pub use reader::DecoderReader;
|
||||||
|
|
@ -16,17 +14,18 @@ pub use reader::DecoderReader;
|
||||||
mod writer;
|
mod writer;
|
||||||
mod reader;
|
mod reader;
|
||||||
|
|
||||||
pub fn encode<T: Encodable<EncoderWriter<MemWriter>, IoError>>(t: &T) -> Result<Vec<u8>, IoError> {
|
pub fn encode<T: Encodable<EncoderWriter<MemWriter>, IoError>>(t: &T) ->
|
||||||
|
Result<Vec<u8>, IoError> {
|
||||||
match encode_into(t, MemWriter::new()) {
|
match encode_into(t, MemWriter::new()) {
|
||||||
Ok(w) => Ok(w.unwrap()),
|
Ok(w) => Ok(w.unwrap()),
|
||||||
Err((w, e)) => Err(e)
|
Err((_, e)) => Err(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode<T: Decodable<DecoderReader<MemReader>, IoError>>(b: Vec<u8>) ->
|
pub fn decode<T: Decodable<DecoderReader<MemReader>, IoError>>(b: Vec<u8>) ->
|
||||||
Result<(T, Vec<u8>), (IoError, Vec<u8>)> {
|
Result<T, (IoError, Vec<u8>)> {
|
||||||
match decode_from(MemReader::new(b)) {
|
match decode_from(MemReader::new(b)) {
|
||||||
Ok((t, r)) => Ok((t, r.unwrap())),
|
Ok((t, _)) => Ok(t),
|
||||||
Err((e, r)) => Err((e, r.unwrap()))
|
Err((e, r)) => Err((e, r.unwrap()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -41,7 +40,8 @@ pub fn encode_into<W: Writer, T: Encodable<EncoderWriter<W>, IoError>>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode_from<R: Reader, T: Decodable<DecoderReader<R>, IoError>>(r: R) -> Result<(T, R), (IoError, R)> {
|
pub fn decode_from<R: Reader, T: Decodable<DecoderReader<R>, IoError>>(r: R) ->
|
||||||
|
Result<(T, R), (IoError, R)> {
|
||||||
let mut reader = reader::DecoderReader::new(r);
|
let mut reader = reader::DecoderReader::new(r);
|
||||||
let x: Result<T, IoError> = Decodable::decode(&mut reader);
|
let x: Result<T, IoError> = Decodable::decode(&mut reader);
|
||||||
let mem = reader.unwrap();
|
let mem = reader.unwrap();
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ use std::io::Reader;
|
||||||
use std::io::BufferedReader;
|
use std::io::BufferedReader;
|
||||||
use std::io::IoError;
|
use std::io::IoError;
|
||||||
use std::io::OtherIoError;
|
use std::io::OtherIoError;
|
||||||
use std::io::RefReader;
|
|
||||||
use serialize::Decoder;
|
use serialize::Decoder;
|
||||||
|
|
||||||
type EwResult = Result<(), IoError>;
|
type EwResult = Result<(), IoError>;
|
||||||
|
|
@ -79,64 +78,84 @@ impl <R: Reader> Decoder<IoError> for DecoderReader<R> {
|
||||||
}
|
}
|
||||||
Ok(string)
|
Ok(string)
|
||||||
}
|
}
|
||||||
fn read_enum<T>(&mut self, name: &str, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_enum<T>(&mut self, _: &str,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_enum_variant<T>(&mut self, names: &[&str], f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_enum_variant<T>(&mut self, _: &[&str],
|
||||||
|
f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
let id = try!(self.reader.read_be_uint());
|
let id = try!(self.reader.read_be_uint());
|
||||||
//let len = try!(self.reader.read_be_uint());
|
|
||||||
f(self, id)
|
f(self, id)
|
||||||
}
|
}
|
||||||
fn read_enum_variant_arg<T>(&mut self, a_idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_enum_variant_arg<T>(&mut self, _: uint,
|
||||||
let idx = try!(self.reader.read_be_uint());
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_enum_struct_variant<T>(&mut self, names: &[&str], f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_enum_struct_variant<T>(&mut self, names: &[&str],
|
||||||
|
f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
self.read_enum_variant(names, f)
|
self.read_enum_variant(names, f)
|
||||||
}
|
}
|
||||||
fn read_enum_struct_variant_field<T>(&mut self, f_name: &str, f_idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_enum_struct_variant_field<T>(&mut self, _: &str, f_idx: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
self.read_enum_variant_arg(f_idx, f)
|
self.read_enum_variant_arg(f_idx, f)
|
||||||
}
|
}
|
||||||
fn read_struct<T>(&mut self, s_name: &str, len: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_struct<T>(&mut self, _: &str, _: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_struct_field<T>(&mut self, f_name: &str, f_idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_struct_field<T>(&mut self, _: &str, _: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_tuple<T>(&mut self, f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_tuple<T>(&mut self,
|
||||||
|
f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) ->
|
||||||
|
Result<T, IoError> {
|
||||||
let len = try!(self.reader.read_be_uint());
|
let len = try!(self.reader.read_be_uint());
|
||||||
f(self, len)
|
f(self, len)
|
||||||
}
|
}
|
||||||
fn read_tuple_arg<T>(&mut self, a_idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_tuple_arg<T>(&mut self, _: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_tuple_struct<T>(&mut self, s_name: &str, f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_tuple_struct<T>(&mut self, _: &str,
|
||||||
|
f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) ->
|
||||||
|
Result<T, IoError> {
|
||||||
self.read_tuple(f)
|
self.read_tuple(f)
|
||||||
}
|
}
|
||||||
fn read_tuple_struct_arg<T>(&mut self, a_idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_tuple_struct_arg<T>(&mut self, a_idx: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
self.read_tuple_arg(a_idx, f)
|
self.read_tuple_arg(a_idx, f)
|
||||||
}
|
}
|
||||||
fn read_option<T>(&mut self, f: |&mut DecoderReader<R>, bool| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_option<T>(&mut self,
|
||||||
|
f: |&mut DecoderReader<R>, bool| -> Result<T, IoError>) ->
|
||||||
|
Result<T, IoError> {
|
||||||
match try!(self.reader.read_u8()) {
|
match try!(self.reader.read_u8()) {
|
||||||
1 => f(self, true),
|
1 => f(self, true),
|
||||||
_ => f(self, false)
|
_ => f(self, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn read_seq<T>(&mut self, f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_seq<T>(&mut self,
|
||||||
|
f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) ->
|
||||||
|
Result<T, IoError> {
|
||||||
let len = try!(self.reader.read_be_uint());
|
let len = try!(self.reader.read_be_uint());
|
||||||
f(self, len)
|
f(self, len)
|
||||||
}
|
}
|
||||||
fn read_seq_elt<T>(&mut self, idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_seq_elt<T>(&mut self, _: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_map<T>(&mut self, f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_map<T>(&mut self,
|
||||||
|
f: |&mut DecoderReader<R>, uint| -> Result<T, IoError>) ->
|
||||||
|
Result<T, IoError> {
|
||||||
let len = try!(self.reader.read_be_uint());
|
let len = try!(self.reader.read_be_uint());
|
||||||
f(self, len)
|
f(self, len)
|
||||||
}
|
}
|
||||||
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_map_elt_key<T>(&mut self, _: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
fn read_map_elt_val<T>(&mut self, _: uint,
|
||||||
|
f: |&mut DecoderReader<R>| -> Result<T, IoError>) -> Result<T, IoError> {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn error(&mut self, err: &str) -> IoError {
|
fn error(&mut self, err: &str) -> IoError {
|
||||||
|
|
|
||||||
14
src/test.rs
14
src/test.rs
|
|
@ -13,22 +13,14 @@ use serialize::{
|
||||||
|
|
||||||
use super::EncoderWriter;
|
use super::EncoderWriter;
|
||||||
use super::DecoderReader;
|
use super::DecoderReader;
|
||||||
|
use super::encode;
|
||||||
|
use super::decode;
|
||||||
|
|
||||||
fn the_same<
|
fn the_same<
|
||||||
V: Encodable<EncoderWriter<MemWriter>, IoError> +
|
V: Encodable<EncoderWriter<MemWriter>, IoError> +
|
||||||
Decodable<DecoderReader<MemReader>, IoError> +
|
Decodable<DecoderReader<MemReader>, IoError> +
|
||||||
PartialEq + Show>(element: V) {
|
PartialEq + Show>(element: V) {
|
||||||
let mem_w = MemWriter::new();
|
assert!(element == decode(encode(&element).unwrap()).unwrap());
|
||||||
let mut writer = EncoderWriter::new(mem_w);
|
|
||||||
element.encode(&mut writer);
|
|
||||||
|
|
||||||
let bytes = writer.unwrap().unwrap();
|
|
||||||
let mem_r = MemReader::new(bytes);
|
|
||||||
let mut reader = DecoderReader::new(mem_r);
|
|
||||||
|
|
||||||
let decoded = Decodable::decode(&mut reader).unwrap();
|
|
||||||
println!("{}, -> {}", element, decoded);
|
|
||||||
assert!(element == decoded);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -66,91 +66,85 @@ impl <W: Writer> Encoder<IoError> for EncoderWriter<W> {
|
||||||
try!(self.writer.write_be_uint(v.len()));
|
try!(self.writer.write_be_uint(v.len()));
|
||||||
self.writer.write_str(v)
|
self.writer.write_str(v)
|
||||||
}
|
}
|
||||||
fn emit_enum(&mut self, name: &str,
|
fn emit_enum(&mut self, _: &str,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_enum_variant(&mut self,
|
fn emit_enum_variant(&mut self,
|
||||||
v_name: &str, v_id: uint, len: uint,
|
_: &str, v_id: uint, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(v_id));
|
try!(self.writer.write_be_uint(v_id));
|
||||||
//try!(self.writer.write_be_uint(len));
|
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_enum_variant_arg(&mut self, a_idx: uint,
|
fn emit_enum_variant_arg(&mut self, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(a_idx));
|
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_enum_struct_variant(&mut self, v_name: &str, v_id: uint,
|
fn emit_enum_struct_variant(&mut self, _: &str, _: uint,
|
||||||
len: uint, f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
_: uint, f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(v_id));
|
|
||||||
try!(self.writer.write_str(v_name));
|
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_enum_struct_variant_field(&mut self, f_name: &str,
|
fn emit_enum_struct_variant_field(&mut self, _: &str,
|
||||||
f_idx: uint, f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
_: uint, f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(f_idx));
|
|
||||||
try!(self.writer.write_str(f_name));
|
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_struct(&mut self, name: &str, len: uint,
|
fn emit_struct(&mut self, _: &str, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_struct_field(&mut self, f_name: &str, f_idx: uint,
|
fn emit_struct_field(&mut self, _: &str, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_tuple(&mut self, len: uint,
|
fn emit_tuple(&mut self, len: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(len));
|
try!(self.writer.write_be_uint(len));
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_tuple_arg(&mut self, idx: uint,
|
fn emit_tuple_arg(&mut self, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_tuple_struct(&mut self, name: &str, len: uint,
|
fn emit_tuple_struct(&mut self, _: &str, len: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
self.emit_tuple(len, f)
|
self.emit_tuple(len, f)
|
||||||
}
|
}
|
||||||
fn emit_tuple_struct_arg(&mut self, f_idx: uint,
|
fn emit_tuple_struct_arg(&mut self, f_idx: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
self.emit_tuple_arg(f_idx, f)
|
self.emit_tuple_arg(f_idx, f)
|
||||||
}
|
}
|
||||||
fn emit_option(&mut self,
|
fn emit_option(&mut self,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_option_none(&mut self) -> EwResult {
|
fn emit_option_none(&mut self) -> EwResult {
|
||||||
self.writer.write_u8(0)
|
self.writer.write_u8(0)
|
||||||
}
|
}
|
||||||
fn emit_option_some(&mut self,
|
fn emit_option_some(&mut self,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_u8(1));
|
try!(self.writer.write_u8(1));
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_seq(&mut self, len: uint,
|
fn emit_seq(&mut self, len: uint,
|
||||||
f: |this: &mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |this: &mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(len));
|
try!(self.writer.write_be_uint(len));
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_seq_elt(&mut self, idx: uint,
|
fn emit_seq_elt(&mut self, _: uint,
|
||||||
f: |this: &mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |this: &mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_map(&mut self, len: uint,
|
fn emit_map(&mut self, len: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
try!(self.writer.write_be_uint(len));
|
try!(self.writer.write_be_uint(len));
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_map_elt_key(&mut self, idx: uint,
|
fn emit_map_elt_key(&mut self, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
fn emit_map_elt_val(&mut self, idx: uint,
|
fn emit_map_elt_val(&mut self, _: uint,
|
||||||
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
f: |&mut EncoderWriter<W>| -> EwResult) -> EwResult {
|
||||||
f(self)
|
f(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue