diff --git a/serde2/.gitignore b/serde2/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/serde2/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/serde2/Cargo.lock b/serde2/Cargo.lock new file mode 100644 index 00000000..386763db --- /dev/null +++ b/serde2/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "serde2" +version = "0.1.0" + diff --git a/serde2/Cargo.toml b/serde2/Cargo.toml new file mode 100644 index 00000000..32c309b4 --- /dev/null +++ b/serde2/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "serde2" +version = "0.1.0" +authors = ["Erick Tryzelaar "] + +[lib] +name = "serde2" + +[[bin]] +name = "serde2" +path = "src/bin.rs" diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs new file mode 100644 index 00000000..b15594fa --- /dev/null +++ b/serde2/src/bin.rs @@ -0,0 +1,119 @@ +extern crate serde2; + +use std::io; +use std::collections::TreeMap; +use serde2::{Serialize, FormatState, GatherTokens}; + +/////////////////////////////////////////////////////////////////////////////// + +struct Foo { + x: int, + y: int, + z: &'static str, +} + +impl, R> serde2::Serialize for Foo { + fn serialize(&self, state: &mut S) -> R { + state.serialize_struct("Foo", FooSerialize { + value: self, + state: 0, + }) + } +} + +struct FooSerialize<'a> { + value: &'a Foo, + state: uint, +} + +impl<'a, S: serde2::SerializeState, R> serde2::Visitor for FooSerialize<'a> { + fn visit(&mut self, state: &mut S) -> Option { + match self.state { + 0 => { + self.state += 1; + Some(state.serialize_map_elt(true, "x", &self.value.x)) + } + 1 => { + self.state += 1; + Some(state.serialize_map_elt(false, "y", &self.value.y)) + } + 2 => { + self.state += 1; + Some(state.serialize_map_elt(false, "z", &self.value.z)) + } + _ => { + None + } + } + } + + fn size_hint(&self) -> (uint, Option) { + let size = 3 - self.state; + (size, Some(size)) + } +} + +/////////////////////////////////////////////////////////////////////////////// + +fn main() { + let value = 5i; + + let mut s = GatherTokens::new(); + value.serialize(&mut s); + println!("tokens: {}", s.unwrap()); + + value.serialize(&mut FormatState::new(io::stdout())).unwrap(); + println!(""); + + //// + + let value = vec!(1i, 2, 3); + + let mut s = GatherTokens::new(); + value.serialize(&mut s); + println!("tokens: {}", s.unwrap()); + + value.serialize(&mut FormatState::new(io::stdout())).unwrap(); + println!(""); + + //// + + let mut value = TreeMap::new(); + value.insert("a", 1i); + value.insert("b", 2); + value.insert("c", 3); + + let mut s = GatherTokens::new(); + value.serialize(&mut s); + println!("tokens: {}", s.unwrap()); + + value.serialize(&mut FormatState::new(io::stdout())).unwrap(); + println!(""); + + //// + + /* + println!("{}", to_format_vec(&5i)); + println!("{}", to_format_string(&5i)); + */ + + let value = Foo { x: 1, y: 2, z: "abc" }; + + let mut s = GatherTokens::new(); + value.serialize(&mut s); + println!("tokens: {}", s.unwrap()); + + value.serialize(&mut FormatState::new(io::stdout())).unwrap(); + println!(""); + + //// + + let value = (1i, "abc"); + + let mut s = GatherTokens::new(); + value.serialize(&mut s); + println!("tokens: {}", s.unwrap()); + + value.serialize(&mut FormatState::new(io::stdout())).unwrap(); + println!(""); +} diff --git a/src/serde2.rs b/serde2/src/lib.rs similarity index 80% rename from src/serde2.rs rename to serde2/src/lib.rs index f0df9e84..b94bfbaa 100644 --- a/src/serde2.rs +++ b/serde2/src/lib.rs @@ -4,19 +4,19 @@ use std::collections::TreeMap; /////////////////////////////////////////////////////////////////////////////// -trait Serialize { +pub trait Serialize { fn serialize(&self, state: &mut S) -> R; } /////////////////////////////////////////////////////////////////////////////// -trait Serializer { +pub trait Serializer { fn hash>(&self, value: &T) -> R; } /////////////////////////////////////////////////////////////////////////////// -trait Visitor { +pub trait Visitor { fn visit(&mut self, state: &mut S) -> Option; fn size_hint(&self) -> (uint, Option) { @@ -24,7 +24,7 @@ trait Visitor { } } -trait SerializeState { +pub trait SerializeState { fn serialize_int(&mut self, value: int) -> R; fn serialize_str(&mut self, value: &'static str) -> R; @@ -163,7 +163,7 @@ impl< /////////////////////////////////////////////////////////////////////////////// #[deriving(Show)] -enum Token { +pub enum Token { Int(int), Str(&'static str), SeqStart(uint), @@ -172,24 +172,24 @@ enum Token { End, } -trait TokenState: SerializeState { +pub trait TokenState: SerializeState { fn serialize(&mut self, token: Token) -> R; } /////////////////////////////////////////////////////////////////////////////// -struct GatherTokens { +pub struct GatherTokens { tokens: Vec, } impl GatherTokens { - fn new() -> GatherTokens { + pub fn new() -> GatherTokens { GatherTokens { tokens: Vec::new(), } } - fn unwrap(self) -> Vec { + pub fn unwrap(self) -> Vec { self.tokens } } @@ -298,16 +298,20 @@ impl SerializeState<()> for GatherTokens { /////////////////////////////////////////////////////////////////////////////// -struct FormatState { +pub struct FormatState { writer: W, } impl FormatState { - fn new(writer: W) -> FormatState { + pub fn new(writer: W) -> FormatState { FormatState { writer: writer, } } + + pub fn unwrap(self) -> W { + self.writer + } } impl SerializeState> for FormatState { @@ -539,17 +543,13 @@ impl SerializeState for JsonSerializer { /////////////////////////////////////////////////////////////////////////////// -/* pub fn to_format_vec< - W: Writer, - T: Serialize, IoResult<()>> + T: Serialize, IoResult<()>> >(value: &T) -> IoResult> { - let mut writer = io::MemWriter::new(); - { - let mut w = FormatState::new(writer.by_ref()); - try!(value.serialize(&mut w)); - } - Ok(writer.unwrap()) + let writer = io::MemWriter::new(); + let mut state = FormatState::new(writer); + try!(value.serialize(&mut state)); + Ok(state.unwrap().unwrap()) } pub fn to_format_string< @@ -558,127 +558,3 @@ pub fn to_format_string< let vec = try!(to_format_vec(value)); Ok(String::from_utf8(vec)) } -*/ - -/////////////////////////////////////////////////////////////////////////////// - -struct Foo { - x: int, - y: int, - z: &'static str, -} - -impl, R> Serialize for Foo { - fn serialize(&self, state: &mut S) -> R { - let mut x = FooSerialize { - value: self, - state: 0, - foo_state: state, - }; - x.foo_state.serialize_struct("Foo", &mut x) - /* - state.serialize_struct("Foo", FooSerialize { - value: self, - state: 0, - foo_state: state, - }) - */ - } -} - -struct FooSerialize<'a, S> { - value: &'a Foo, - state: uint, - foo_state: &'a mut S, -} - -impl<'a, S: SerializeState, R> Visitor for FooSerialize<'a, S> { - fn visit(&mut self, state: &mut S) -> Option { - match self.state { - 0 => { - self.state += 1; - Some(state.serialize_map_elt(true, "x", &self.value.x)) - } - 1 => { - self.state += 1; - Some(state.serialize_map_elt(false, "y", &self.value.y)) - } - 2 => { - self.state += 1; - Some(state.serialize_map_elt(false, "z", &self.value.z)) - } - _ => { - None - } - } - } - - fn size_hint(&self) -> (uint, Option) { - let size = 3 - self.state; - (size, Some(size)) - } -} - -fn main() { - - let value = 5i; - - let mut s = GatherTokens::new(); - value.serialize(&mut s); - println!("tokens: {}", s.unwrap()); - - value.serialize(&mut FormatState::new(io::stdout())).unwrap(); - println!(""); - - //// - - let value = vec!(1i, 2, 3); - - let mut s = GatherTokens::new(); - value.serialize(&mut s); - println!("tokens: {}", s.unwrap()); - - value.serialize(&mut FormatState::new(io::stdout())).unwrap(); - println!(""); - - //// - - let mut value = TreeMap::new(); - value.insert("a", 1i); - value.insert("b", 2); - value.insert("c", 3); - - let mut s = GatherTokens::new(); - value.serialize(&mut s); - println!("tokens: {}", s.unwrap()); - - value.serialize(&mut FormatState::new(io::stdout())).unwrap(); - println!(""); - - //// - - /* - println!("{}", to_format_vec(&5i)); - println!("{}", to_format_string(&5i)); - */ - - let value = Foo { x: 1, y: 2, z: "abc" }; - - let mut s = GatherTokens::new(); - value.serialize(&mut s); - println!("tokens: {}", s.unwrap()); - - value.serialize(&mut FormatState::new(io::stdout())).unwrap(); - println!(""); - - //// - - let value = (1i, "abc"); - - let mut s = GatherTokens::new(); - value.serialize(&mut s); - println!("tokens: {}", s.unwrap()); - - value.serialize(&mut FormatState::new(io::stdout())).unwrap(); - println!(""); -}