Skip to content

Commit b54bad8

Browse files
committed
BytesStart::to_borrowed renamed to BytesStart::borrow, the same method added to all events
1 parent 84105e6 commit b54bad8

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
- [#395]: Add support for XML Schema `xs:list`
2626
- [#324]: `Reader::from_str` / `Deserializer::from_str` / `from_str` now ignore
2727
the XML declared encoding and always use UTF-8
28+
- [#416]: Add `borrow()` methods in all event structs which allows to get
29+
a borrowed version of any event
2830

2931
### Bug Fixes
3032

@@ -108,6 +110,9 @@
108110
|`read_to_end_unbuffered` |`read_to_end`
109111
- [#412]: Change `read_to_end*` and `read_text_into` to accept `QName` instead of `AsRef<[u8]>`
110112

113+
- [#416]: `BytesStart::to_borrowed` renamed to `BytesStart::borrow`, the same method
114+
added to all events
115+
111116
### New Tests
112117

113118
- [#9]: Added tests for incorrect nested tags in input
@@ -131,6 +136,7 @@
131136
[#403]: https://github.com/tafia/quick-xml/pull/403
132137
[#407]: https://github.com/tafia/quick-xml/pull/407
133138
[#412]: https://github.com/tafia/quick-xml/pull/412
139+
[#416]: https://github.com/tafia/quick-xml/pull/416
134140

135141
## 0.23.0 -- 2022-05-08
136142

src/events/mod.rs

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ impl<'a> BytesStartText<'a> {
7575
self.content.into_inner()
7676
}
7777

78+
/// Converts the event into a borrowed event.
79+
#[inline]
80+
pub fn borrow(&self) -> BytesStartText {
81+
BytesStartText {
82+
content: self.content.borrow(),
83+
}
84+
}
85+
7886
/// Decodes bytes of event, stripping byte order mark (BOM) if it is presented
7987
/// in the event.
8088
///
@@ -195,15 +203,15 @@ impl<'a> BytesStart<'a> {
195203
/// # fn example(&self) -> Result<(), Error> {
196204
/// # let mut writer = Writer::new(Vec::new());
197205
///
198-
/// writer.write_event(Event::Start(self.attrs.to_borrowed()))?;
206+
/// writer.write_event(Event::Start(self.attrs.borrow()))?;
199207
/// // ...
200208
/// writer.write_event(Event::End(self.attrs.to_end()))?;
201209
/// # Ok(())
202210
/// # }}
203211
/// ```
204212
///
205213
/// [`to_end`]: Self::to_end
206-
pub fn to_borrowed(&self) -> BytesStart {
214+
pub fn borrow(&self) -> BytesStart {
207215
BytesStart::borrowed(&self.buf, self.name_len)
208216
}
209217

@@ -343,13 +351,13 @@ impl<'a> Deref for BytesStart<'a> {
343351
/// [W3C XML 1.1 Prolog and Document Type Declaration](http://w3.org/TR/xml11/#sec-prolog-dtd)
344352
#[derive(Clone, Debug, Eq, PartialEq)]
345353
pub struct BytesDecl<'a> {
346-
element: BytesStart<'a>,
354+
content: BytesStart<'a>,
347355
}
348356

349357
impl<'a> BytesDecl<'a> {
350358
/// Creates a `BytesDecl` from a `BytesStart`
351-
pub fn from_start(start: BytesStart<'a>) -> BytesDecl<'a> {
352-
BytesDecl { element: start }
359+
pub fn from_start(start: BytesStart<'a>) -> Self {
360+
Self { content: start }
353361
}
354362

355363
/// Gets xml version, excluding quotes (`'` or `"`).
@@ -407,7 +415,7 @@ impl<'a> BytesDecl<'a> {
407415
/// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
408416
pub fn version(&self) -> Result<Cow<[u8]>> {
409417
// The version *must* be the first thing in the declaration.
410-
match self.element.attributes().with_checks(false).next() {
418+
match self.content.attributes().with_checks(false).next() {
411419
Some(Ok(a)) if a.key.as_ref() == b"version" => Ok(a.value),
412420
// first attribute was not "version"
413421
Some(Ok(a)) => {
@@ -457,7 +465,7 @@ impl<'a> BytesDecl<'a> {
457465
///
458466
/// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
459467
pub fn encoding(&self) -> Option<Result<Cow<[u8]>>> {
460-
self.element
468+
self.content
461469
.try_get_attribute("encoding")
462470
.map(|a| a.map(|a| a.value))
463471
.transpose()
@@ -499,7 +507,7 @@ impl<'a> BytesDecl<'a> {
499507
///
500508
/// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
501509
pub fn standalone(&self) -> Option<Result<Cow<[u8]>>> {
502-
self.element
510+
self.content
503511
.try_get_attribute("standalone")
504512
.map(|a| a.map(|a| a.value))
505513
.transpose()
@@ -548,7 +556,7 @@ impl<'a> BytesDecl<'a> {
548556
buf.push(b'"');
549557

550558
BytesDecl {
551-
element: BytesStart::owned(buf, 3),
559+
content: BytesStart::owned(buf, 3),
552560
}
553561
}
554562

@@ -563,7 +571,15 @@ impl<'a> BytesDecl<'a> {
563571
/// Converts the event into an owned event.
564572
pub fn into_owned(self) -> BytesDecl<'static> {
565573
BytesDecl {
566-
element: self.element.into_owned(),
574+
content: self.content.into_owned(),
575+
}
576+
}
577+
578+
/// Converts the event into a borrowed event.
579+
#[inline]
580+
pub fn borrow(&self) -> BytesDecl {
581+
BytesDecl {
582+
content: self.content.borrow(),
567583
}
568584
}
569585
}
@@ -572,7 +588,7 @@ impl<'a> Deref for BytesDecl<'a> {
572588
type Target = [u8];
573589

574590
fn deref(&self) -> &[u8] {
575-
&*self.element
591+
&*self.content
576592
}
577593
}
578594

@@ -608,6 +624,14 @@ impl<'a> BytesEnd<'a> {
608624
}
609625
}
610626

627+
/// Converts the event into a borrowed event.
628+
#[inline]
629+
pub fn borrow(&self) -> BytesEnd {
630+
BytesEnd {
631+
name: Cow::Borrowed(&self.name),
632+
}
633+
}
634+
611635
/// Gets the undecoded raw tag name, as present in the input stream.
612636
#[inline]
613637
pub fn name(&self) -> QName {
@@ -699,6 +723,14 @@ impl<'a> BytesText<'a> {
699723
self.content
700724
}
701725

726+
/// Converts the event into a borrowed event.
727+
#[inline]
728+
pub fn borrow(&self) -> BytesText {
729+
BytesText {
730+
content: Cow::Borrowed(&self.content),
731+
}
732+
}
733+
702734
/// Returns unescaped version of the text content, that can be written
703735
/// as CDATA in XML
704736
#[cfg(feature = "serialize")]
@@ -853,6 +885,14 @@ impl<'a> BytesCData<'a> {
853885
self.content
854886
}
855887

888+
/// Converts the event into a borrowed event.
889+
#[inline]
890+
pub fn borrow(&self) -> BytesCData {
891+
BytesCData {
892+
content: Cow::Borrowed(&self.content),
893+
}
894+
}
895+
856896
/// Converts this CDATA content to an escaped version, that can be written
857897
/// as an usual text in XML.
858898
///
@@ -1015,6 +1055,24 @@ impl<'a> Event<'a> {
10151055
Event::Eof => Event::Eof,
10161056
}
10171057
}
1058+
1059+
/// Converts the event into a borrowed event.
1060+
#[inline]
1061+
pub fn borrow(&self) -> Event {
1062+
match self {
1063+
Event::StartText(e) => Event::StartText(e.borrow()),
1064+
Event::Start(e) => Event::Start(e.borrow()),
1065+
Event::End(e) => Event::End(e.borrow()),
1066+
Event::Empty(e) => Event::Empty(e.borrow()),
1067+
Event::Text(e) => Event::Text(e.borrow()),
1068+
Event::Comment(e) => Event::Comment(e.borrow()),
1069+
Event::CData(e) => Event::CData(e.borrow()),
1070+
Event::Decl(e) => Event::Decl(e.borrow()),
1071+
Event::PI(e) => Event::PI(e.borrow()),
1072+
Event::DocType(e) => Event::DocType(e.borrow()),
1073+
Event::Eof => Event::Eof,
1074+
}
1075+
}
10181076
}
10191077

10201078
impl<'a> Deref for Event<'a> {

src/se/var.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ where
154154
} else {
155155
self.parent
156156
.writer
157-
.write_event(Event::Start(self.attrs.to_borrowed()))?;
157+
.write_event(Event::Start(self.attrs.borrow()))?;
158158
self.parent.writer.write(&self.children)?;
159159
self.parent
160160
.writer

src/writer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
256256
/// Write some text inside the current element.
257257
pub fn write_text_content(self, text: BytesText) -> Result<&'a mut Writer<W>> {
258258
self.writer
259-
.write_event(Event::Start(self.start_tag.to_borrowed()))?;
259+
.write_event(Event::Start(self.start_tag.borrow()))?;
260260
self.writer.write_event(Event::Text(text))?;
261261
self.writer
262262
.write_event(Event::End(self.start_tag.to_end()))?;
@@ -266,7 +266,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
266266
/// Write a CData event `<![CDATA[...]]>` inside the current element.
267267
pub fn write_cdata_content(self, text: BytesCData) -> Result<&'a mut Writer<W>> {
268268
self.writer
269-
.write_event(Event::Start(self.start_tag.to_borrowed()))?;
269+
.write_event(Event::Start(self.start_tag.borrow()))?;
270270
self.writer.write_event(Event::CData(text))?;
271271
self.writer
272272
.write_event(Event::End(self.start_tag.to_end()))?;
@@ -276,7 +276,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
276276
/// Write a processing instruction `<?...?>` inside the current element.
277277
pub fn write_pi_content(self, text: BytesText) -> Result<&'a mut Writer<W>> {
278278
self.writer
279-
.write_event(Event::Start(self.start_tag.to_borrowed()))?;
279+
.write_event(Event::Start(self.start_tag.borrow()))?;
280280
self.writer.write_event(Event::PI(text))?;
281281
self.writer
282282
.write_event(Event::End(self.start_tag.to_end()))?;
@@ -295,7 +295,7 @@ impl<'a, W: Write> ElementWriter<'a, W> {
295295
F: Fn(&mut Writer<W>) -> Result<()>,
296296
{
297297
self.writer
298-
.write_event(Event::Start(self.start_tag.to_borrowed()))?;
298+
.write_event(Event::Start(self.start_tag.borrow()))?;
299299
closure(&mut self.writer)?;
300300
self.writer
301301
.write_event(Event::End(self.start_tag.to_end()))?;

0 commit comments

Comments
 (0)