Simplify logic for serializing prefixes
This commit is contained in:
parent
da536d072f
commit
5bd58698d7
|
@ -54,7 +54,7 @@ fn serialize_scalar_enum(
|
|||
) -> Result<(), instant_xml::Error> {
|
||||
let prefix = match field {
|
||||
Some(id) => {
|
||||
let prefix = serializer.write_start(id.name, id.ns, true)?;
|
||||
let prefix = serializer.write_start(id.name, id.ns)?;
|
||||
serializer.end_start()?;
|
||||
Some((prefix, id.name))
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ fn serialize_wrapped_enum(
|
|||
serializer: &mut instant_xml::Serializer<W>,
|
||||
) -> Result<(), instant_xml::Error> {
|
||||
// Start tag
|
||||
let prefix = serializer.write_start(#tag, #default_namespace, false)?;
|
||||
let prefix = serializer.write_start(#tag, #default_namespace)?;
|
||||
debug_assert_eq!(prefix, None);
|
||||
|
||||
// Set up element context, this will also emit namespace declarations
|
||||
|
@ -234,7 +234,7 @@ fn serialize_struct(
|
|||
serializer: &mut instant_xml::Serializer<W>,
|
||||
) -> Result<(), instant_xml::Error> {
|
||||
// Start tag
|
||||
let prefix = serializer.write_start(#tag, #default_namespace, false)?;
|
||||
let prefix = serializer.write_start(#tag, #default_namespace)?;
|
||||
debug_assert_eq!(prefix, None);
|
||||
|
||||
// Set up element context, this will also emit namespace declarations
|
||||
|
|
|
@ -69,7 +69,7 @@ where
|
|||
) -> Result<(), Error> {
|
||||
let prefix = match field {
|
||||
Some(id) => {
|
||||
let prefix = serializer.write_start(id.name, id.ns, true)?;
|
||||
let prefix = serializer.write_start(id.name, id.ns)?;
|
||||
serializer.end_start()?;
|
||||
Some((prefix, id.name))
|
||||
}
|
||||
|
@ -489,7 +489,7 @@ impl ToXml for DateTime<Utc> {
|
|||
) -> Result<(), Error> {
|
||||
let prefix = match field {
|
||||
Some(id) => {
|
||||
let prefix = serializer.write_start(id.name, id.ns, true)?;
|
||||
let prefix = serializer.write_start(id.name, id.ns)?;
|
||||
serializer.end_start()?;
|
||||
Some((prefix, id.name))
|
||||
}
|
||||
|
|
|
@ -28,35 +28,25 @@ impl<'xml, W: fmt::Write + ?Sized> Serializer<'xml, W> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn write_start(
|
||||
&mut self,
|
||||
name: &str,
|
||||
ns: &str,
|
||||
scalar: bool,
|
||||
) -> Result<Option<&'static str>, Error> {
|
||||
pub fn write_start(&mut self, name: &str, ns: &str) -> Result<Option<&'static str>, Error> {
|
||||
if self.state != State::Element {
|
||||
return Err(Error::UnexpectedState("invalid state for element start"));
|
||||
}
|
||||
|
||||
let prefix = match ns == self.default_ns {
|
||||
true => {
|
||||
let prefix = match (ns == self.default_ns, self.prefixes.get(ns)) {
|
||||
(true, _) => {
|
||||
self.output.write_fmt(format_args!("<{name}"))?;
|
||||
None
|
||||
}
|
||||
// In the case of scalars, we can use the prefix instead of setting a
|
||||
// default namespace (since there are no child elements that will
|
||||
// expect the default namespace to be set).
|
||||
false => match (scalar, self.prefixes.get(ns)) {
|
||||
(false, _) | (true, None) => {
|
||||
(false, Some(prefix)) => {
|
||||
self.output.write_fmt(format_args!("<{prefix}:{name}"))?;
|
||||
Some(*prefix)
|
||||
}
|
||||
_ => {
|
||||
self.output
|
||||
.write_fmt(format_args!("<{name} xmlns=\"{ns}\""))?;
|
||||
None
|
||||
}
|
||||
(true, Some(&prefix)) => {
|
||||
self.output.write_fmt(format_args!("<{prefix}:{name}"))?;
|
||||
Some(prefix)
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
self.state = State::Attribute;
|
||||
|
|
Loading…
Reference in New Issue