Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt to add newlines between attributes. (#275) #731

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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