Simplify logic for serializing prefixes

This commit is contained in:
Dirkjan Ochtman 2022-11-29 12:14:24 +01:00
parent da536d072f
commit 5bd58698d7
3 changed files with 17 additions and 27 deletions

View File

@ -54,7 +54,7 @@ fn serialize_scalar_enum(
) -> Result<(), instant_xml::Error> { ) -> Result<(), instant_xml::Error> {
let prefix = match field { let prefix = match field {
Some(id) => { Some(id) => {
let prefix = serializer.write_start(id.name, id.ns, true)?; let prefix = serializer.write_start(id.name, id.ns)?;
serializer.end_start()?; serializer.end_start()?;
Some((prefix, id.name)) Some((prefix, id.name))
} }
@ -143,7 +143,7 @@ fn serialize_wrapped_enum(
serializer: &mut instant_xml::Serializer<W>, serializer: &mut instant_xml::Serializer<W>,
) -> Result<(), instant_xml::Error> { ) -> Result<(), instant_xml::Error> {
// Start tag // Start tag
let prefix = serializer.write_start(#tag, #default_namespace, false)?; let prefix = serializer.write_start(#tag, #default_namespace)?;
debug_assert_eq!(prefix, None); debug_assert_eq!(prefix, None);
// Set up element context, this will also emit namespace declarations // Set up element context, this will also emit namespace declarations
@ -234,7 +234,7 @@ fn serialize_struct(
serializer: &mut instant_xml::Serializer<W>, serializer: &mut instant_xml::Serializer<W>,
) -> Result<(), instant_xml::Error> { ) -> Result<(), instant_xml::Error> {
// Start tag // Start tag
let prefix = serializer.write_start(#tag, #default_namespace, false)?; let prefix = serializer.write_start(#tag, #default_namespace)?;
debug_assert_eq!(prefix, None); debug_assert_eq!(prefix, None);
// Set up element context, this will also emit namespace declarations // Set up element context, this will also emit namespace declarations

View File

@ -69,7 +69,7 @@ where
) -> Result<(), Error> { ) -> Result<(), Error> {
let prefix = match field { let prefix = match field {
Some(id) => { Some(id) => {
let prefix = serializer.write_start(id.name, id.ns, true)?; let prefix = serializer.write_start(id.name, id.ns)?;
serializer.end_start()?; serializer.end_start()?;
Some((prefix, id.name)) Some((prefix, id.name))
} }
@ -489,7 +489,7 @@ impl ToXml for DateTime<Utc> {
) -> Result<(), Error> { ) -> Result<(), Error> {
let prefix = match field { let prefix = match field {
Some(id) => { Some(id) => {
let prefix = serializer.write_start(id.name, id.ns, true)?; let prefix = serializer.write_start(id.name, id.ns)?;
serializer.end_start()?; serializer.end_start()?;
Some((prefix, id.name)) Some((prefix, id.name))
} }

View File

@ -28,35 +28,25 @@ impl<'xml, W: fmt::Write + ?Sized> Serializer<'xml, W> {
} }
} }
pub fn write_start( pub fn write_start(&mut self, name: &str, ns: &str) -> Result<Option<&'static str>, Error> {
&mut self,
name: &str,
ns: &str,
scalar: bool,
) -> Result<Option<&'static str>, Error> {
if self.state != State::Element { if self.state != State::Element {
return Err(Error::UnexpectedState("invalid state for element start")); return Err(Error::UnexpectedState("invalid state for element start"));
} }
let prefix = match ns == self.default_ns { let prefix = match (ns == self.default_ns, self.prefixes.get(ns)) {
true => { (true, _) => {
self.output.write_fmt(format_args!("<{name}"))?; self.output.write_fmt(format_args!("<{name}"))?;
None None
} }
// In the case of scalars, we can use the prefix instead of setting a (false, Some(prefix)) => {
// default namespace (since there are no child elements that will self.output.write_fmt(format_args!("<{prefix}:{name}"))?;
// expect the default namespace to be set). Some(*prefix)
false => match (scalar, self.prefixes.get(ns)) { }
(false, _) | (true, None) => { _ => {
self.output self.output
.write_fmt(format_args!("<{name} xmlns=\"{ns}\""))?; .write_fmt(format_args!("<{name} xmlns=\"{ns}\""))?;
None None
} }
(true, Some(&prefix)) => {
self.output.write_fmt(format_args!("<{prefix}:{name}"))?;
Some(prefix)
}
},
}; };
self.state = State::Attribute; self.state = State::Attribute;