Skip to content

Commit

Permalink
Cleanup implementation of ToXml
Browse files Browse the repository at this point in the history
  • Loading branch information
njaremko committed Jul 3, 2024
1 parent e7a9b1a commit f8a0b16
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,21 @@ where
type Error = <&'a FromType as TryInto<Event<'a>>>::Error;

fn to_string(&'a self) -> Result<String, Self::Error> {
let mut v = Vec::new();
let mut writer = Writer::new(Cursor::new(&mut v));
let e: Event<'a> = self.try_into()?;
writer.write_event(e)?;
let v = self.to_vec()?;
let output = std::str::from_utf8(v.as_slice())?.to_string();
Ok(output)
}

fn to_vec(&'a self) -> Result<Vec<u8>, Self::Error> {
let mut v = Vec::new();
let mut writer = Writer::new(Cursor::new(&mut v));
let e: Event<'a> = self.try_into()?;
writer.write_event(e)?;
self.to_writer(Cursor::new(&mut v))?;
Ok(v)
}

fn to_writer(&'a self, writer: impl std::io::Write) -> Result<(), Self::Error> {
let mut writer = Writer::new(writer);
let mut xml_writer = Writer::new(writer);
let e: Event<'a> = self.try_into()?;
writer.write_event(e)?;
xml_writer.write_event(e)?;
Ok(())
}
}

0 comments on commit f8a0b16

Please sign in to comment.