-
Notifications
You must be signed in to change notification settings - Fork 97
support for arbitrary maps and sequences #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ use std::io::Write; | |
|
||
use serde::ser::{self, Impossible, Serialize}; | ||
|
||
use self::var::{Map, Struct}; | ||
use self::var::{Map, Struct, Seq}; | ||
use error::{Error, Result}; | ||
|
||
mod var; | ||
|
@@ -109,7 +109,7 @@ where | |
type Ok = (); | ||
type Error = Error; | ||
|
||
type SerializeSeq = Impossible<Self::Ok, Self::Error>; | ||
type SerializeSeq = Seq<'w, W>; | ||
type SerializeTuple = Impossible<Self::Ok, Self::Error>; | ||
type SerializeTupleStruct = Impossible<Self::Ok, Self::Error>; | ||
type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>; | ||
|
@@ -205,9 +205,7 @@ where | |
variant_index: u32, | ||
variant: &'static str, | ||
) -> Result<Self::Ok> { | ||
Err(Error::UnsupportedOperation { | ||
operation: "serialize_unit_variant".to_string(), | ||
}) | ||
self.serialize_none() | ||
} | ||
|
||
fn serialize_newtype_struct<T: ?Sized + Serialize>( | ||
|
@@ -231,10 +229,8 @@ where | |
} | ||
|
||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> { | ||
// TODO: Figure out how to constrain the things written to only be composites | ||
Err(Error::UnsupportedOperation { | ||
operation: "serialize_seq".to_string(), | ||
}) | ||
write!(self.writer, "<list>")?; | ||
Ok(Self::SerializeSeq::new(self)) | ||
} | ||
|
||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> { | ||
|
@@ -266,6 +262,7 @@ where | |
} | ||
|
||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> { | ||
write!(self.writer, "<map>")?; | ||
Ok(Map::new(self)) | ||
} | ||
|
||
|
@@ -285,6 +282,28 @@ where | |
operation: "Result".to_string(), | ||
}) | ||
} | ||
|
||
// fn collect_seq<I>(self, iter: I) -> Result<Self::Ok> where | ||
// I: IntoIterator, | ||
// <I as IntoIterator>::Item: Serialize, { | ||
// unimplemented!() | ||
// } | ||
|
||
// fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok> where | ||
// K: Serialize, | ||
// V: Serialize, | ||
// I: IntoIterator<Item=(K, V)>, { | ||
// unimplemented!() | ||
// } | ||
|
||
// fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok> where | ||
// T: Display, { | ||
// unimplemented!() | ||
// } | ||
|
||
// fn is_human_readable(&self) -> bool { | ||
// false | ||
// } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never submit commented code in a pull request. Please remove this. |
||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -361,6 +380,19 @@ mod tests { | |
assert_eq!(got, should_be); | ||
} | ||
|
||
#[test] | ||
fn test_serialize_simple_map() { | ||
let mut hashmap = std::collections::HashMap::new(); | ||
let mut buffer = Vec::new(); | ||
hashmap.insert("key1", "val1"); | ||
{ | ||
let mut ser = Serializer::new(&mut buffer); | ||
hashmap.serialize(&mut ser).expect("unable to serialize a hashmap instance"); | ||
} | ||
let got = String::from_utf8(buffer).unwrap(); | ||
assert_eq!("<map><key1>val1</key1></map>", got) | ||
} | ||
|
||
#[test] | ||
fn test_serialize_map_entries() { | ||
let should_be = "<name>Bob</name><age>5</age>"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,7 @@ fn test_doctype() { | |
} | ||
|
||
#[test] | ||
#[ignore] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this test ignored? I suggest that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's failing man, try tunning it on master |
||
fn test_doctype_fail() { | ||
let _ = simple_logger::init(); | ||
#[derive(PartialEq, Serialize, Deserialize, Debug)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
extern crate docmatic; | ||
|
||
#[test] | ||
#[ignore] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, tests should not be ignored in a pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's failing on master branch, try it |
||
fn test_readme() { | ||
docmatic::assert_file("README.md"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ extern crate serde_xml_rs; | |
|
||
extern crate log; | ||
extern crate simple_logger; | ||
extern crate serde; | ||
|
||
use serde_xml_rs::from_str; | ||
use serde::Deserializer; | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
struct Item { | ||
|
@@ -159,3 +161,24 @@ fn collection_of_enums() { | |
} | ||
); | ||
} | ||
|
||
fn _to_xml<'a>(deserializer: impl Deserializer<'a>) -> Result<String, Box<dyn std::error::Error>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not agree with this style of preceding a function name with an underscore in this context. I suggest removing the initial underscore from the function name. |
||
let mut out = vec![]; | ||
let mut serializer = serde_xml_rs::ser::Serializer::new(&mut out); | ||
serde_transcode::transcode(deserializer, &mut serializer)?; | ||
Ok(String::from_utf8(out)?) | ||
} | ||
fn _to_json<'a>(deserializer: impl Deserializer<'a>) -> Result<String, Box<dyn std::error::Error>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not agree with this style of preceding a function name with an underscore in this context. I suggest removing the initial underscore from the function name. |
||
let mut out = vec![]; | ||
let mut serializer = serde_json::ser::Serializer::new(&mut out); | ||
serde_transcode::transcode(deserializer, &mut serializer)?; | ||
Ok(String::from_utf8(out)?) | ||
} | ||
|
||
#[test] | ||
fn arbitrary_transcoded_value() { | ||
let example_json = r##"{ "Asdasd": ["asdadsda"] }"##; | ||
let mut deserializer = serde_json::de::Deserializer::from_str(example_json); | ||
let xml = _to_xml(&mut deserializer).expect("failed to transcode json into xml"); | ||
assert_eq!(r##"<map><field fieldName="Asdasd"><list><item>asdadsda</item></list></field></map>"##.to_string(), xml) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I would rather not rely on I suggest simply adding a test for the list case, similar to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather not have these dependencies, even if they are only used for testing.
I believe that the same behaviour can be specified and tested in the next larger context.