Skip to content

Commit 21194f4

Browse files
mgreekornelski
authored andcommitted
TryFrom instance to convert reader::XmlEvent to writer::XmlEvent
1 parent a2e8e0e commit 21194f4

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/writer/events.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,44 @@ impl<'a> From<StartElementBuilder<'a>> for XmlEvent<'a> {
255255
}
256256
}
257257
}
258+
259+
impl<'a> TryFrom<&'a crate::reader::XmlEvent> for XmlEvent<'a> {
260+
type Error = ();
261+
262+
fn try_from(event: &crate::reader::XmlEvent) -> Result<XmlEvent<'_>, Self::Error> {
263+
match event {
264+
crate::reader::XmlEvent::StartDocument {
265+
version,
266+
encoding,
267+
standalone,
268+
} => Ok(XmlEvent::StartDocument {
269+
version: version.clone(),
270+
encoding: Some(&encoding),
271+
standalone: standalone.clone(),
272+
}),
273+
crate::reader::XmlEvent::EndDocument => Err(()),
274+
crate::reader::XmlEvent::ProcessingInstruction { name, data } => {
275+
Ok(XmlEvent::ProcessingInstruction {
276+
name: &name,
277+
data: data.as_deref(),
278+
})
279+
}
280+
crate::reader::XmlEvent::StartElement {
281+
name,
282+
attributes,
283+
namespace,
284+
} => Ok(XmlEvent::StartElement {
285+
name: name.borrow(),
286+
attributes: Cow::Owned(attributes.iter().map(|attr| attr.borrow()).collect()),
287+
namespace: namespace.borrow(),
288+
}),
289+
crate::reader::XmlEvent::EndElement { name } => Ok(XmlEvent::EndElement {
290+
name: Some(name.borrow()),
291+
}),
292+
crate::reader::XmlEvent::CData(chars) => Ok(XmlEvent::CData(chars)),
293+
crate::reader::XmlEvent::Comment(chars) => Ok(XmlEvent::Comment(chars)),
294+
crate::reader::XmlEvent::Characters(chars) => Ok(XmlEvent::Characters(chars)),
295+
crate::reader::XmlEvent::Whitespace(chars) => Ok(XmlEvent::Characters(chars)),
296+
}
297+
}
298+
}

0 commit comments

Comments
 (0)