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

Add expand_empty_elements fn to Serializer #620

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions src/se/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub struct ContentSerializer<'w, 'i, W: Write> {
/// If `true`, then current indent will be written before writing the content,
/// but only if content is not empty.
pub write_indent: bool,
// If `true`, then empty elements will be serialized as `<element></element>`
// instead of `<element/>`.
pub expand_empty_elements: bool,
//TODO: add settings to disallow consequent serialization of primitives
}

Expand Down Expand Up @@ -85,6 +88,7 @@ impl<'w, 'i, W: Write> ContentSerializer<'w, 'i, W> {
level: self.level,
indent: self.indent.borrow(),
write_indent: self.write_indent,
expand_empty_elements: false,
}
}

Expand Down Expand Up @@ -483,6 +487,7 @@ pub(super) mod tests {
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: false,
};

$data.serialize(ser).unwrap();
Expand All @@ -503,6 +508,7 @@ pub(super) mod tests {
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: false,
};

match $data.serialize(ser).unwrap_err() {
Expand Down Expand Up @@ -672,6 +678,7 @@ pub(super) mod tests {
level: QuoteLevel::Full,
indent: Indent::Owned(Indentation::new(b' ', 2)),
write_indent: false,
expand_empty_elements: false,
};

$data.serialize(ser).unwrap();
Expand All @@ -692,6 +699,7 @@ pub(super) mod tests {
level: QuoteLevel::Full,
indent: Indent::Owned(Indentation::new(b' ', 2)),
write_indent: false,
expand_empty_elements: false,
};

match $data.serialize(ser).unwrap_err() {
Expand Down
38 changes: 37 additions & 1 deletion src/se/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'w, 'k, W: Write> Serializer for ElementSerializer<'w, 'k, W> {
write_primitive!(serialize_bytes(&[u8]));

fn serialize_str(self, value: &str) -> Result<Self::Ok, Self::Error> {
if value.is_empty() {
if value.is_empty() && !self.ser.expand_empty_elements {
self.ser.write_empty(self.key)
} else {
self.ser
Expand Down Expand Up @@ -397,6 +397,7 @@ impl<'w, 'k, W: Write> Struct<'w, 'k, W> {
level: self.ser.ser.level,
indent: self.ser.ser.indent.borrow(),
write_indent: true,
expand_empty_elements: false,
};

if key == TEXT_KEY {
Expand Down Expand Up @@ -574,6 +575,7 @@ mod tests {
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: false,
},
key: XmlName("root"),
};
Expand All @@ -597,6 +599,7 @@ mod tests {
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: false,
},
key: XmlName("root"),
};
Expand Down Expand Up @@ -1533,6 +1536,7 @@ mod tests {
level: QuoteLevel::Full,
indent: Indent::Owned(Indentation::new(b' ', 2)),
write_indent: false,
expand_empty_elements: false,
},
key: XmlName("root"),
};
Expand All @@ -1556,6 +1560,7 @@ mod tests {
level: QuoteLevel::Full,
indent: Indent::Owned(Indentation::new(b' ', 2)),
write_indent: false,
expand_empty_elements: false,
},
key: XmlName("root"),
};
Expand Down Expand Up @@ -2533,4 +2538,35 @@ mod tests {
</root>");
}
}

mod expand_empty_elements {
use super::*;
use pretty_assertions::assert_eq;

/// Checks that given `$data` successfully serialized as `$expected`
macro_rules! serialize_as {
($name:ident: $data:expr => $expected:expr) => {
#[test]
fn $name() {
let mut buffer = String::new();
let ser = ElementSerializer {
ser: ContentSerializer {
writer: &mut buffer,
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: true,
},
key: XmlName("root"),
};

$data.serialize(ser).unwrap();
assert_eq!(buffer, $expected);
}
};
}

serialize_as!(option_some_empty: Some("") => "<root></root>");
serialize_as!(option_some_empty_str: Some("") => "<root></root>");
}
}
8 changes: 8 additions & 0 deletions src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ impl<'w, 'r, W: Write> Serializer<'w, 'r, W> {
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: false,
},
root_tag: None,
}
Expand Down Expand Up @@ -522,11 +523,18 @@ impl<'w, 'r, W: Write> Serializer<'w, 'r, W> {
level: QuoteLevel::Full,
indent: Indent::None,
write_indent: false,
expand_empty_elements: false,
},
root_tag: root_tag.map(|tag| XmlName::try_from(tag)).transpose()?,
})
}

/// Enable or disable expansion of empty elements.
pub fn expand_empty_elements(&mut self, expand: bool) -> &mut Self {
self.ser.expand_empty_elements = expand;
self
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a doctest example and specify that default value is false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mingun I added a dockest and noticed changing the expand_empty_elements in the method does not affect the ContentSerializer that actually does the serializing.

I didn't find the culprit yet, is this due to macros or cloning?


/// Configure indent for a serializer
pub fn indent(&mut self, indent_char: char, indent_size: usize) -> &mut Self {
self.ser.indent = Indent::Owned(Indentation::new(indent_char as u8, indent_size));
Expand Down