Skip to content

Commit

Permalink
Merge pull request #731 from areleu/attribute-newlines
Browse files Browse the repository at this point in the history
First attempt to add newlines between attributes. (#275)
  • Loading branch information
Mingun committed May 25, 2024
2 parents 499dbaf + c8b427f commit 740b788
Show file tree
Hide file tree
Showing 3 changed files with 450 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ to get an offset of the error position. For `SyntaxError`s the range
- [#629]: Added a default case to `impl_deserialize_for_internally_tagged_enum` macro so that
it can handle every attribute that does not match existing cases within an enum variant.
- [#722]: Allow to pass owned strings to `Writer::create_element`. This is breaking change!
- [#275]: Added `ElementWriter::new_line()` which enables pretty printing elements with multiple attributes.

### Bug Fixes

Expand Down Expand Up @@ -67,6 +68,7 @@ to get an offset of the error position. For `SyntaxError`s the range
- [#738]: Add an example of how to deserialize XML elements into Rust enums using an
intermediate custom deserializer.

[#275]: https://github.com/tafia/quick-xml/issues/275
[#362]: https://github.com/tafia/quick-xml/issues/362
[#513]: https://github.com/tafia/quick-xml/issues/513
[#622]: https://github.com/tafia/quick-xml/issues/622
Expand Down
29 changes: 22 additions & 7 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,8 @@ impl<'a> BytesStart<'a> {
where
A: Into<Attribute<'b>>,
{
let a = attr.into();
let bytes = self.buf.to_mut();
bytes.push(b' ');
bytes.extend_from_slice(a.key.as_ref());
bytes.extend_from_slice(b"=\"");
bytes.extend_from_slice(a.value.as_ref());
bytes.push(b'"');
self.buf.to_mut().push(b' ');
self.push_attr(attr.into());
}

/// Remove all attributes from the ByteStart
Expand Down Expand Up @@ -287,6 +282,26 @@ impl<'a> BytesStart<'a> {
}
Ok(None)
}

/// Adds an attribute to this element.
pub(crate) fn push_attr<'b>(&mut self, attr: Attribute<'b>) {
let bytes = self.buf.to_mut();
bytes.extend_from_slice(attr.key.as_ref());
bytes.extend_from_slice(b"=\"");
// FIXME: need to escape attribute content
bytes.extend_from_slice(attr.value.as_ref());
bytes.push(b'"');
}

/// Adds new line in existing element
pub(crate) fn push_newline(&mut self) {
self.buf.to_mut().push(b'\n');
}

/// Adds indentation bytes in existing element
pub(crate) fn push_indent(&mut self, indent: &[u8]) {
self.buf.to_mut().extend_from_slice(indent);
}
}

impl<'a> Debug for BytesStart<'a> {
Expand Down
Loading

0 comments on commit 740b788

Please sign in to comment.