Skip to content
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

Support Option in plain String serializer #220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/ser/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'ser, W: Write> serde::ser::SerializeMap for MapSerializer<'ser, W> {
where
T: ?Sized + Serialize,
{
self.ser.open_tag(&to_plain_string(key)?)?;
self.ser.open_tag(&to_plain_string(key)?.unwrap())?;
Ok(())
}

Expand Down Expand Up @@ -65,20 +65,22 @@ impl<'ser, W: 'ser + Write> StructSerializer<'ser, W> {
{
if key.starts_with("@") {
debug!("attribute {}", key);
self.ser.add_attr(&key[1..], to_plain_string(value)?)
if let Some(string_value) = to_plain_string(value)? {
self.ser.add_attr(&key[1..], string_value)?
}
// key was not present
} else if key == "$value" {
self.ser.build_start_tag()?;
debug!("body");
value.serialize(&mut *self.ser)?;
Ok(())
} else {
self.ser.build_start_tag()?;
self.ser.open_tag(key)?;
debug!("field {}", key);
value.serialize(&mut *self.ser)?;
debug!("end field");
Ok(())
}
Ok(())
}

fn after_fields(self) -> Result<()> {
Expand Down
27 changes: 18 additions & 9 deletions src/ser/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@ use serde::ser::{Impossible, Serialize};

use crate::error::{Error, Result};

pub fn to_plain_string<T>(value: &T) -> Result<String>
pub fn to_plain_string<T>(value: &T) -> Result<Option<String>>
where
T: ?Sized + Serialize,
{
let mut writer = Vec::with_capacity(128);
value.serialize(&mut PlainStringSerializer::new(&mut writer))?;
let mut pss = PlainStringSerializer::new(&mut writer);
value.serialize(&mut pss)?;

let string = String::from_utf8(writer)?;
Ok(string)
if !pss.present {
// String does not present
return Ok(None);
}
Ok(Some(String::from_utf8(writer)?))
}

struct PlainStringSerializer<W: Write> {
writer: W,
present: bool,
}

impl<W: Write> PlainStringSerializer<W> {
fn new(writer: W) -> Self {
PlainStringSerializer { writer }
PlainStringSerializer {
writer,
present: true,
}
}

fn characters(&mut self, s: &str) -> Result<()> {
Expand Down Expand Up @@ -99,18 +107,19 @@ impl<'ser, W: 'ser + Write> serde::ser::Serializer for &'ser mut PlainStringSeri
}

fn serialize_none(self) -> Result<Self::Ok> {
unimplemented!()
self.serialize_unit()
}

fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok>
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
where
T: Serialize,
{
unimplemented!()
value.serialize(self)
}

fn serialize_unit(self) -> Result<Self::Ok> {
unimplemented!()
self.present = false;
Ok(())
}

fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> {
Expand Down
11 changes: 11 additions & 0 deletions tests/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ mod ser {
#[case::string(r#"<bla value="" />"#, "".to_string())]
#[case::bool(r#"<bla value="true" />"#, true)]
#[case::bool(r#"<bla value="false" />"#, false)]
#[case::option(r#"<bla value="apple" />"#, Some("apple".to_string()))]
fn attribute_ok<T>(_logger: (), #[case] expected: &str, #[case] value: T)
where
T: Serialize + Debug,
Expand All @@ -146,4 +147,14 @@ mod ser {
format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, expected)
);
}

#[rstest]
#[case::option(r#"<bla />"#, None)]
fn attribute_none_ok(_logger: (), #[case] expected: &str, #[case] value: Option<String>) {
let actual = to_string(&DummyAttribute { value }).unwrap();
assert_eq!(
actual,
format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, expected)
);
}
}