Skip to content

Commit

Permalink
Change Writer errors to only io errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RedPhoenixQ committed Sep 28, 2024
1 parent ccb8b6f commit 6d47998
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
31 changes: 14 additions & 17 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -129,7 +127,7 @@ impl<W> Writer<W> {
/// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
/// 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
Expand Down Expand Up @@ -187,12 +185,12 @@ impl<W: Write> Writer<W> {
/// # }
/// ```
/// [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<Event<'a>>>(&mut self, event: E) -> Result<()> {
pub fn write_event<'a, E: Into<Event<'a>>>(&mut self, event: E) -> io::Result<()> {
let mut next_should_line_break = true;
let result = match event.into() {
Event::Start(e) => {
Expand Down Expand Up @@ -233,12 +231,12 @@ impl<W: Write> Writer<W> {

/// 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")?;
Expand All @@ -262,7 +260,7 @@ impl<W: Write> Writer<W> {
/// [`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())?;
Expand Down Expand Up @@ -320,7 +318,7 @@ impl<W: Write> Writer<W> {
&mut self,
tag_name: &str,
content: &T,
) -> std::result::Result<(), DeError> {
) -> Result<(), DeError> {
use crate::se::{Indent, Serializer};

self.write_indent()?;
Expand Down Expand Up @@ -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<W>> {
pub fn write_text_content(self, text: BytesText) -> io::Result<&'a mut Writer<W>> {
self.writer
.write_event(Event::Start(self.start_tag.borrow()))?;
self.writer.write_event(Event::Text(text))?;
Expand All @@ -540,7 +538,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
}

/// Write a CData event `<![CDATA[...]]>` inside the current element.
pub fn write_cdata_content(self, text: BytesCData) -> Result<&'a mut Writer<W>> {
pub fn write_cdata_content(self, text: BytesCData) -> io::Result<&'a mut Writer<W>> {
self.writer
.write_event(Event::Start(self.start_tag.borrow()))?;
self.writer.write_event(Event::CData(text))?;
Expand All @@ -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<W>> {
pub fn write_pi_content(self, pi: BytesPI) -> io::Result<&'a mut Writer<W>> {
self.writer
.write_event(Event::Start(self.start_tag.borrow()))?;
self.writer.write_event(Event::PI(pi))?;
Expand All @@ -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<W>> {
pub fn write_empty(self) -> io::Result<&'a mut Writer<W>> {
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<F, E>(self, closure: F) -> StdResult<&'a mut Writer<W>, E>
pub fn write_inner_content<F>(self, closure: F) -> io::Result<&'a mut Writer<W>>
where
F: FnOnce(&mut Writer<W>) -> StdResult<(), E>,
E: From<Error>,
F: FnOnce(&mut Writer<W>) -> io::Result<()>,
{
self.writer
.write_event(Event::Start(self.start_tag.borrow()))?;
Expand Down
3 changes: 1 addition & 2 deletions tests/writer-indentation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use quick_xml::errors::Error;
use quick_xml::events::{BytesStart, BytesText, Event};
use quick_xml::writer::Writer;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6d47998

Please sign in to comment.