Skip to content

Commit

Permalink
Reuse existing impl
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 3, 2024
1 parent 21194f4 commit b8e2db7
Showing 1 changed file with 5 additions and 35 deletions.
40 changes: 5 additions & 35 deletions src/writer/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,42 +257,12 @@ impl<'a> From<StartElementBuilder<'a>> for XmlEvent<'a> {
}

impl<'a> TryFrom<&'a crate::reader::XmlEvent> for XmlEvent<'a> {
type Error = ();
type Error = crate::reader::Error;

fn try_from(event: &crate::reader::XmlEvent) -> Result<XmlEvent<'_>, Self::Error> {
match event {
crate::reader::XmlEvent::StartDocument {
version,
encoding,
standalone,
} => Ok(XmlEvent::StartDocument {
version: version.clone(),
encoding: Some(&encoding),
standalone: standalone.clone(),
}),
crate::reader::XmlEvent::EndDocument => Err(()),
crate::reader::XmlEvent::ProcessingInstruction { name, data } => {
Ok(XmlEvent::ProcessingInstruction {
name: &name,
data: data.as_deref(),
})
}
crate::reader::XmlEvent::StartElement {
name,
attributes,
namespace,
} => Ok(XmlEvent::StartElement {
name: name.borrow(),
attributes: Cow::Owned(attributes.iter().map(|attr| attr.borrow()).collect()),
namespace: namespace.borrow(),
}),
crate::reader::XmlEvent::EndElement { name } => Ok(XmlEvent::EndElement {
name: Some(name.borrow()),
}),
crate::reader::XmlEvent::CData(chars) => Ok(XmlEvent::CData(chars)),
crate::reader::XmlEvent::Comment(chars) => Ok(XmlEvent::Comment(chars)),
crate::reader::XmlEvent::Characters(chars) => Ok(XmlEvent::Characters(chars)),
crate::reader::XmlEvent::Whitespace(chars) => Ok(XmlEvent::Characters(chars)),
}
event.as_writer_event().ok_or(crate::reader::Error {
pos: crate::common::TextPosition::new(),
kind: crate::reader::ErrorKind::UnexpectedEof,
})
}
}

0 comments on commit b8e2db7

Please sign in to comment.