From 6d479985e04447e3db3d7babeae2d94c0049158f Mon Sep 17 00:00:00 2001 From: RedPhoenixQ Date: Sat, 28 Sep 2024 22:39:06 +0200 Subject: [PATCH] Change Writer errors to only io errors The other error types in `crate::errors::Error` are not applicable to the Writer. Only io error can be given and putting the io error in an `std::sync::Arc` and making users match on all other kinds is not needed. The returned `std::io::Error` can still be turned into `crate::errors::Error` if needed, but on demand. --- src/writer.rs | 31 ++++++++++++++----------------- tests/writer-indentation.rs | 3 +-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/writer.rs b/src/writer.rs index 0f25e0cd..0d5b1b56 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,11 +1,9 @@ //! Contains high-level interface for an events-based XML emitter. use std::borrow::Cow; -use std::io::Write; -use std::result::Result as StdResult; +use std::io::{self, Write}; use crate::encoding::UTF8_BOM; -use crate::errors::{Error, Result}; use crate::events::{attributes::Attribute, BytesCData, BytesPI, BytesStart, BytesText, Event}; #[cfg(feature = "async-tokio")] @@ -129,7 +127,7 @@ impl Writer { /// // writes appleorange /// writer.create_element("tag") /// // We need to provide error type, because it is not named somewhere explicitly - /// .write_inner_content::<_, Error>(|writer| { + /// .write_inner_content(|writer| { /// let fruits = ["apple", "orange"]; /// for (quant, item) in fruits.iter().enumerate() { /// writer @@ -187,12 +185,12 @@ impl Writer { /// # } /// ``` /// [Byte-Order-Mark]: https://unicode.org/faq/utf_bom.html#BOM - pub fn write_bom(&mut self) -> Result<()> { + pub fn write_bom(&mut self) -> io::Result<()> { self.write(UTF8_BOM) } /// Writes the given event to the underlying writer. - pub fn write_event<'a, E: Into>>(&mut self, event: E) -> Result<()> { + pub fn write_event<'a, E: Into>>(&mut self, event: E) -> io::Result<()> { let mut next_should_line_break = true; let result = match event.into() { Event::Start(e) => { @@ -233,12 +231,12 @@ impl Writer { /// Writes bytes #[inline] - pub(crate) fn write(&mut self, value: &[u8]) -> Result<()> { + pub(crate) fn write(&mut self, value: &[u8]) -> io::Result<()> { self.writer.write_all(value).map_err(Into::into) } #[inline] - fn write_wrapped(&mut self, before: &[u8], value: &[u8], after: &[u8]) -> Result<()> { + fn write_wrapped(&mut self, before: &[u8], value: &[u8], after: &[u8]) -> io::Result<()> { if let Some(ref i) = self.indent { if i.should_line_break { self.writer.write_all(b"\n")?; @@ -262,7 +260,7 @@ impl Writer { /// [`Text`]: Event::Text /// [`Start`]: Event::Start /// [`new_with_indent`]: Self::new_with_indent - pub fn write_indent(&mut self) -> Result<()> { + pub fn write_indent(&mut self) -> io::Result<()> { if let Some(ref i) = self.indent { self.writer.write_all(b"\n")?; self.writer.write_all(i.current())?; @@ -320,7 +318,7 @@ impl Writer { &mut self, tag_name: &str, content: &T, - ) -> std::result::Result<(), DeError> { + ) -> Result<(), DeError> { use crate::se::{Indent, Serializer}; self.write_indent()?; @@ -530,7 +528,7 @@ impl<'a, W> ElementWriter<'a, W> { impl<'a, W: Write> ElementWriter<'a, W> { /// Write some text inside the current element. - pub fn write_text_content(self, text: BytesText) -> Result<&'a mut Writer> { + pub fn write_text_content(self, text: BytesText) -> io::Result<&'a mut Writer> { self.writer .write_event(Event::Start(self.start_tag.borrow()))?; self.writer.write_event(Event::Text(text))?; @@ -540,7 +538,7 @@ impl<'a, W: Write> ElementWriter<'a, W> { } /// Write a CData event `` inside the current element. - pub fn write_cdata_content(self, text: BytesCData) -> Result<&'a mut Writer> { + pub fn write_cdata_content(self, text: BytesCData) -> io::Result<&'a mut Writer> { self.writer .write_event(Event::Start(self.start_tag.borrow()))?; self.writer.write_event(Event::CData(text))?; @@ -550,7 +548,7 @@ impl<'a, W: Write> ElementWriter<'a, W> { } /// Write a processing instruction `` inside the current element. - pub fn write_pi_content(self, pi: BytesPI) -> Result<&'a mut Writer> { + pub fn write_pi_content(self, pi: BytesPI) -> io::Result<&'a mut Writer> { self.writer .write_event(Event::Start(self.start_tag.borrow()))?; self.writer.write_event(Event::PI(pi))?; @@ -560,16 +558,15 @@ impl<'a, W: Write> ElementWriter<'a, W> { } /// Write an empty (self-closing) tag. - pub fn write_empty(self) -> Result<&'a mut Writer> { + pub fn write_empty(self) -> io::Result<&'a mut Writer> { self.writer.write_event(Event::Empty(self.start_tag))?; Ok(self.writer) } /// Create a new scope for writing XML inside the current element. - pub fn write_inner_content(self, closure: F) -> StdResult<&'a mut Writer, E> + pub fn write_inner_content(self, closure: F) -> io::Result<&'a mut Writer> where - F: FnOnce(&mut Writer) -> StdResult<(), E>, - E: From, + F: FnOnce(&mut Writer) -> io::Result<()>, { self.writer .write_event(Event::Start(self.start_tag.borrow()))?; diff --git a/tests/writer-indentation.rs b/tests/writer-indentation.rs index 31240fd3..f96f6063 100644 --- a/tests/writer-indentation.rs +++ b/tests/writer-indentation.rs @@ -1,4 +1,3 @@ -use quick_xml::errors::Error; use quick_xml::events::{BytesStart, BytesText, Event}; use quick_xml::writer::Writer; @@ -276,7 +275,7 @@ fn element_writer_nested() { .create_element("outer") .with_attribute(("attr1", "value1")) .with_attribute(("attr2", "value2")) - .write_inner_content::<_, Error>(|writer| { + .write_inner_content(|writer| { let fruits = ["apple", "orange", "banana"]; for (quant, item) in fruits.iter().enumerate() { writer