Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Update for map serialization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Jul 28, 2016
1 parent 41e0cde commit 588225a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
3 changes: 3 additions & 0 deletions yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ preserve_order = ["yaml-rust/preserve_order"]
clippy = { version = "^0.*", optional = true }
serde = "0.8.0-rc3"
yaml-rust = "^0.3.3"

[replace]
"serde:0.8.0-rc3" = { git = "https://github.com/serde-rs/serde" }
61 changes: 39 additions & 22 deletions yaml/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ impl ser::Serializer for Serializer {
type TupleState = yaml::Array;
type TupleStructState = yaml::Array;
type TupleVariantState = (&'static str, yaml::Array);
type MapState = yaml::Hash;
type StructState = yaml::Hash;
type StructVariantState = (&'static str, yaml::Hash);
type MapState = (Option<yaml::Yaml>, yaml::Hash);
type StructState = (Option<yaml::Yaml>, yaml::Hash);
type StructVariantState = (&'static str, (Option<yaml::Yaml>, yaml::Hash));

fn serialize_bool(&mut self, v: bool) -> Result<()> {
self.doc = Yaml::Boolean(v);
Expand Down Expand Up @@ -272,48 +272,64 @@ impl ser::Serializer for Serializer {
Ok(())
}

fn serialize_map(&mut self, _len: Option<usize>) -> Result<yaml::Hash> {
Ok(yaml::Hash::new())
fn serialize_map(&mut self, _len: Option<usize>) -> Result<(Option<yaml::Yaml>, yaml::Hash)> {
Ok((None, yaml::Hash::new()))
}

fn serialize_map_elt<K, V>(
fn serialize_map_key<T>(
&mut self,
state: &mut yaml::Hash,
key: K,
value: V
state: &mut (Option<yaml::Yaml>, yaml::Hash),
key: T
) -> Result<()>
where T: ser::Serialize
{
state.0 = Some(try!(to_yaml(key)));
Ok(())
}

fn serialize_map_value<T>(
&mut self,
state: &mut (Option<yaml::Yaml>, yaml::Hash),
value: T
) -> Result<()>
where K: ser::Serialize,
V: ser::Serialize,
where T: ser::Serialize
{
state.insert(try!(to_yaml(key)), try!(to_yaml(value)));
match state.0.take() {
Some(key) => state.1.insert(key, try!(to_yaml(value))),
None => {
return Err(Error::Custom("serialize_map_value called without matching \
serialize_map_key call".to_owned()));
}
};
Ok(())
}

fn serialize_map_end(&mut self, state: yaml::Hash) -> Result<()> {
self.doc = Yaml::Hash(state);
fn serialize_map_end(&mut self, state: (Option<yaml::Yaml>, yaml::Hash)) -> Result<()> {
self.doc = Yaml::Hash(state.1);
Ok(())
}

fn serialize_struct(
&mut self,
_name: &'static str,
len: usize
) -> Result<yaml::Hash> {
) -> Result<(Option<yaml::Yaml>, yaml::Hash)> {
self.serialize_map(Some(len))
}

fn serialize_struct_elt<V>(
&mut self,
state: &mut yaml::Hash,
state: &mut (Option<yaml::Yaml>, yaml::Hash),
key: &'static str,
value: V
) -> Result<()>
where V: ser::Serialize,
{
self.serialize_map_elt(state, key, value)
try!(self.serialize_map_key(state, key));
self.serialize_map_value(state, value)
}

fn serialize_struct_end(&mut self, state: yaml::Hash) -> Result<()> {
fn serialize_struct_end(&mut self, state: (Option<yaml::Yaml>, yaml::Hash)) -> Result<()> {
self.serialize_map_end(state)
}

Expand All @@ -323,25 +339,26 @@ impl ser::Serializer for Serializer {
_idx: usize,
variant: &'static str,
len: usize
) -> Result<(&'static str, yaml::Hash)> {
) -> Result<(&'static str, (Option<yaml::Yaml>, yaml::Hash))> {
let state = try!(self.serialize_map(Some(len)));
Ok((variant, state))
}

fn serialize_struct_variant_elt<V>(
&mut self,
state: &mut (&'static str, yaml::Hash),
state: &mut (&'static str, (Option<yaml::Yaml>, yaml::Hash)),
field: &'static str,
v: V
) -> Result<()>
where V: ser::Serialize,
{
self.serialize_map_elt(&mut state.1, field, v)
try!(self.serialize_map_key(&mut state.1, field));
self.serialize_map_value(&mut state.1, v)
}

fn serialize_struct_variant_end(
&mut self,
state: (&'static str, yaml::Hash)
state: (&'static str, (Option<yaml::Yaml>, yaml::Hash))
) -> Result<()> {
try!(self.serialize_map_end(state.1));
self.doc = singleton_hash(try!(to_yaml(state.0)),
Expand Down
3 changes: 3 additions & 0 deletions yaml_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ indoc = "*"
[[test]]
name = "test"
path = "tests/test.rs"

[replace]
"serde:0.8.0-rc3" = { git = "https://github.com/serde-rs/serde" }

0 comments on commit 588225a

Please sign in to comment.