diff --git a/src/de/simple_type.rs b/src/de/simple_type.rs index 5c102674..d78db8fc 100644 --- a/src/de/simple_type.rs +++ b/src/de/simple_type.rs @@ -791,7 +791,7 @@ impl<'de, 'a> EnumAccess<'de> for SimpleTypeDeserializer<'de, 'a> { mod tests { use super::*; use crate::se::simple_type::{QuoteTarget, SimpleTypeSerializer}; - use crate::se::{Indent, QuoteLevel}; + use crate::se::QuoteLevel; use crate::utils::{ByteBuf, Bytes}; use serde::de::IgnoredAny; use serde::{Deserialize, Serialize}; @@ -828,7 +828,6 @@ mod tests { writer: String::new(), target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::None, }) .unwrap(), xml @@ -943,7 +942,7 @@ mod tests { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Some(Indent::None), + write_delimiter: false, }) .unwrap(); assert_eq!(buffer, $input); diff --git a/src/se/content.rs b/src/se/content.rs index 6c867426..c07a2d9d 100644 --- a/src/se/content.rs +++ b/src/se/content.rs @@ -80,13 +80,12 @@ pub struct ContentSerializer<'w, 'i, W: Write> { impl<'w, 'i, W: Write> ContentSerializer<'w, 'i, W> { /// Turns this serializer into serializer of a text content #[inline] - pub fn into_simple_type_serializer(self) -> SimpleTypeSerializer<'i, &'w mut W> { + pub fn into_simple_type_serializer(self) -> SimpleTypeSerializer<&'w mut W> { //TODO: Customization point: choose between CDATA and Text representation SimpleTypeSerializer { writer: self.writer, target: QuoteTarget::Text, level: self.level, - indent: Indent::None, } } @@ -128,7 +127,7 @@ impl<'w, 'i, W: Write> ContentSerializer<'w, 'i, W> { serialize: S, ) -> Result where - S: for<'a> FnOnce(SimpleTypeSerializer<'i, &'a mut W>) -> Result<&'a mut W, SeError>, + S: for<'a> FnOnce(SimpleTypeSerializer<&'a mut W>) -> Result<&'a mut W, SeError>, { self.write_indent()?; self.writer.write_char('<')?; diff --git a/src/se/element.rs b/src/se/element.rs index 1ea17194..5f9d3cf7 100644 --- a/src/se/element.rs +++ b/src/se/element.rs @@ -5,7 +5,7 @@ use crate::se::content::ContentSerializer; use crate::se::key::QNameSerializer; use crate::se::simple_type::{QuoteTarget, SimpleSeq, SimpleTypeSerializer}; use crate::se::text::TextSerializer; -use crate::se::{Indent, SeError, WriteResult, XmlName}; +use crate::se::{SeError, WriteResult, XmlName}; use serde::ser::{ Impossible, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, SerializeTupleStruct, SerializeTupleVariant, Serializer, @@ -333,7 +333,7 @@ pub enum Tuple<'w, 'k, W: Write> { /// Serialize each tuple field as an element Element(ElementSerializer<'w, 'k, W>), /// Serialize tuple as an `xs:list`: space-delimited content of fields - Text(SimpleSeq<'k, &'w mut W>), + Text(SimpleSeq<&'w mut W>), } impl<'w, 'k, W: Write> SerializeTupleVariant for Tuple<'w, 'k, W> { @@ -416,7 +416,6 @@ impl<'w, 'k, W: Write> Struct<'w, 'k, W> { writer: &mut self.ser.ser.writer, target: QuoteTarget::DoubleQAttr, level: self.ser.ser.level, - indent: Indent::None, })?; self.ser.ser.writer.write_char('"')?; diff --git a/src/se/simple_type.rs b/src/se/simple_type.rs index dbdfe277..c7336ce6 100644 --- a/src/se/simple_type.rs +++ b/src/se/simple_type.rs @@ -4,7 +4,7 @@ //! [as defined]: https://www.w3.org/TR/xmlschema11-1/#Simple_Type_Definition use crate::escape::_escape; -use crate::se::{Indent, QuoteLevel, SeError}; +use crate::se::{QuoteLevel, SeError}; use serde::ser::{ Impossible, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct, SerializeTupleVariant, Serializer, @@ -179,22 +179,18 @@ macro_rules! write_atomic { /// /// [item]: https://www.w3.org/TR/xmlschema11-1/#std-item_type_definition /// [simple type]: https://www.w3.org/TR/xmlschema11-1/#Simple_Type_Definition -pub struct AtomicSerializer<'i, W: Write> { +pub struct AtomicSerializer { pub writer: W, pub target: QuoteTarget, /// Defines which XML characters need to be escaped pub level: QuoteLevel, - /// When `Some`, the indent that should be written before the content - /// if content is not an empty string. - /// When `None` an `xs:list` delimiter (a space) should be written - pub(crate) indent: Option>, + /// When `true` an `xs:list` delimiter (a space) should be written + pub(crate) write_delimiter: bool, } -impl<'i, W: Write> AtomicSerializer<'i, W> { +impl AtomicSerializer { fn write_str(&mut self, value: &str) -> Result<(), SeError> { - if let Some(indent) = self.indent.as_mut() { - indent.write_indent(&mut self.writer)?; - } else { + if self.write_delimiter { // TODO: Customization point -- possible non-XML compatible extension to specify delimiter char self.writer.write_char(' ')?; } @@ -202,7 +198,7 @@ impl<'i, W: Write> AtomicSerializer<'i, W> { } } -impl<'i, W: Write> Serializer for AtomicSerializer<'i, W> { +impl Serializer for AtomicSerializer { type Ok = bool; type Error = SeError; @@ -401,31 +397,28 @@ impl<'i, W: Write> Serializer for AtomicSerializer<'i, W> { /// - CDATA content (`<...>`) /// /// [simple types]: https://www.w3.org/TR/xmlschema11-1/#Simple_Type_Definition -pub struct SimpleTypeSerializer<'i, W: Write> { +pub struct SimpleTypeSerializer { /// Writer to which this serializer writes content pub writer: W, /// Target for which element is serializing. Affects additional characters to escape. pub target: QuoteTarget, /// Defines which XML characters need to be escaped pub level: QuoteLevel, - /// Indent that should be written before the content if content is not an empty string - pub(crate) indent: Indent<'i>, } -impl<'i, W: Write> SimpleTypeSerializer<'i, W> { +impl SimpleTypeSerializer { fn write_str(&mut self, value: &str) -> Result<(), SeError> { - self.indent.write_indent(&mut self.writer)?; Ok(self.writer.write_str(value)?) } } -impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> { +impl Serializer for SimpleTypeSerializer { type Ok = W; type Error = SeError; - type SerializeSeq = SimpleSeq<'i, W>; - type SerializeTuple = SimpleSeq<'i, W>; - type SerializeTupleStruct = SimpleSeq<'i, W>; + type SerializeSeq = SimpleSeq; + type SerializeTuple = SimpleSeq; + type SerializeTupleStruct = SimpleSeq; type SerializeTupleVariant = Impossible; type SerializeMap = Impossible; type SerializeStruct = Impossible; @@ -470,7 +463,6 @@ impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> { writer: self.writer, target: self.target, level: self.level, - indent: self.indent, is_empty: true, }) } @@ -535,17 +527,15 @@ impl<'i, W: Write> Serializer for SimpleTypeSerializer<'i, W> { } /// Serializer for a sequence of atomic values delimited by space -pub struct SimpleSeq<'i, W: Write> { +pub struct SimpleSeq { writer: W, target: QuoteTarget, level: QuoteLevel, - /// Indent that should be written before the content if content is not an empty string - indent: Indent<'i>, /// If `true`, nothing was written yet to the `writer` is_empty: bool, } -impl<'i, W: Write> SerializeSeq for SimpleSeq<'i, W> { +impl SerializeSeq for SimpleSeq { type Ok = W; type Error = SeError; @@ -553,17 +543,11 @@ impl<'i, W: Write> SerializeSeq for SimpleSeq<'i, W> { where T: ?Sized + Serialize, { - // Write indent for the first element and delimiter for others - let indent = if self.is_empty { - Some(self.indent.borrow()) - } else { - None - }; if value.serialize(AtomicSerializer { writer: &mut self.writer, target: self.target, level: self.level, - indent, + write_delimiter: !self.is_empty, })? { self.is_empty = false; } @@ -576,7 +560,7 @@ impl<'i, W: Write> SerializeSeq for SimpleSeq<'i, W> { } } -impl<'i, W: Write> SerializeTuple for SimpleSeq<'i, W> { +impl SerializeTuple for SimpleSeq { type Ok = W; type Error = SeError; @@ -594,7 +578,7 @@ impl<'i, W: Write> SerializeTuple for SimpleSeq<'i, W> { } } -impl<'i, W: Write> SerializeTupleStruct for SimpleSeq<'i, W> { +impl SerializeTupleStruct for SimpleSeq { type Ok = W; type Error = SeError; @@ -612,7 +596,7 @@ impl<'i, W: Write> SerializeTupleStruct for SimpleSeq<'i, W> { } } -impl<'i, W: Write> SerializeTupleVariant for SimpleSeq<'i, W> { +impl SerializeTupleVariant for SimpleSeq { type Ok = W; type Error = SeError; @@ -928,7 +912,7 @@ mod tests { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Some(Indent::None), + write_delimiter: false, }; let has_written = $data.serialize(ser).unwrap(); @@ -949,7 +933,7 @@ mod tests { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Some(Indent::None), + write_delimiter: false, }; match $data.serialize(ser).unwrap_err() { @@ -1047,7 +1031,6 @@ mod tests { writer: String::new(), target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::None, }; let buffer = $data.serialize(ser).unwrap(); @@ -1067,7 +1050,6 @@ mod tests { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::None, }; match $data.serialize(ser).unwrap_err() { @@ -1153,19 +1135,15 @@ mod tests { mod simple_seq { use super::*; - use crate::writer::Indentation; use pretty_assertions::assert_eq; #[test] fn empty_seq() { let mut buffer = String::new(); - let mut indent = Indentation::new(b'*', 2); - indent.grow(); let ser = SimpleSeq { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::Owned(indent), is_empty: true, }; @@ -1176,13 +1154,10 @@ mod tests { #[test] fn all_items_empty() { let mut buffer = String::new(); - let mut indent = Indentation::new(b'*', 2); - indent.grow(); let mut ser = SimpleSeq { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::Owned(indent), is_empty: true, }; @@ -1196,13 +1171,10 @@ mod tests { #[test] fn some_items_empty1() { let mut buffer = String::new(); - let mut indent = Indentation::new(b'*', 2); - indent.grow(); let mut ser = SimpleSeq { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::Owned(indent), is_empty: true, }; @@ -1210,19 +1182,16 @@ mod tests { SerializeSeq::serialize_element(&mut ser, &1).unwrap(); SerializeSeq::serialize_element(&mut ser, "").unwrap(); SerializeSeq::end(ser).unwrap(); - assert_eq!(buffer, "\n**1"); + assert_eq!(buffer, "1"); } #[test] fn some_items_empty2() { let mut buffer = String::new(); - let mut indent = Indentation::new(b'*', 2); - indent.grow(); let mut ser = SimpleSeq { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::Owned(indent), is_empty: true, }; @@ -1230,19 +1199,16 @@ mod tests { SerializeSeq::serialize_element(&mut ser, "").unwrap(); SerializeSeq::serialize_element(&mut ser, &2).unwrap(); SerializeSeq::end(ser).unwrap(); - assert_eq!(buffer, "\n**1 2"); + assert_eq!(buffer, "1 2"); } #[test] fn items() { let mut buffer = String::new(); - let mut indent = Indentation::new(b'*', 2); - indent.grow(); let mut ser = SimpleSeq { writer: &mut buffer, target: QuoteTarget::Text, level: QuoteLevel::Full, - indent: Indent::Owned(indent), is_empty: true, }; @@ -1250,7 +1216,7 @@ mod tests { SerializeSeq::serialize_element(&mut ser, &2).unwrap(); SerializeSeq::serialize_element(&mut ser, &3).unwrap(); SerializeSeq::end(ser).unwrap(); - assert_eq!(buffer, "\n**1 2 3"); + assert_eq!(buffer, "1 2 3"); } } } diff --git a/src/se/text.rs b/src/se/text.rs index 9dd30509..7094adba 100644 --- a/src/se/text.rs +++ b/src/se/text.rs @@ -23,16 +23,16 @@ macro_rules! write_primitive { /// This serializer a very similar to [`SimpleTypeSerializer`], but different /// from it in how it processes unit enum variants. Unlike [`SimpleTypeSerializer`] /// this serializer does not write anything for the unit variant. -pub struct TextSerializer<'i, W: Write>(pub SimpleTypeSerializer<'i, W>); +pub struct TextSerializer(pub SimpleTypeSerializer); -impl<'i, W: Write> Serializer for TextSerializer<'i, W> { +impl Serializer for TextSerializer { type Ok = W; type Error = SeError; - type SerializeSeq = SimpleSeq<'i, W>; - type SerializeTuple = SimpleSeq<'i, W>; - type SerializeTupleStruct = SimpleSeq<'i, W>; - type SerializeTupleVariant = SimpleSeq<'i, W>; + type SerializeSeq = SimpleSeq; + type SerializeTuple = SimpleSeq; + type SerializeTupleStruct = SimpleSeq; + type SerializeTupleVariant = SimpleSeq; type SerializeMap = Impossible; type SerializeStruct = Impossible; type SerializeStructVariant = Impossible;