Skip to content

Commit

Permalink
Implement From<QName> for BytesStart and BytesEnd
Browse files Browse the repository at this point in the history
Use new conversion whenever possible for reader. This will help later,
when events for reader and writer will split
  • Loading branch information
Mingun committed Jul 10, 2022
1 parent 84105e6 commit 7280390
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 88 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -25,6 +25,7 @@
- [#395]: Add support for XML Schema `xs:list`
- [#324]: `Reader::from_str` / `Deserializer::from_str` / `from_str` now ignore
the XML declared encoding and always use UTF-8
- [#416]: Add `From<QName>` for `BytesStart` and `BytesEnd`

### Bug Fixes

Expand Down Expand Up @@ -131,6 +132,7 @@
[#403]: https://github.com/tafia/quick-xml/pull/403
[#407]: https://github.com/tafia/quick-xml/pull/407
[#412]: https://github.com/tafia/quick-xml/pull/412
[#416]: https://github.com/tafia/quick-xml/pull/416

## 0.23.0 -- 2022-05-08

Expand Down
124 changes: 47 additions & 77 deletions src/de/mod.rs
Expand Up @@ -1031,7 +1031,7 @@ mod tests {
mod skip {
use super::*;
use crate::de::DeEvent::*;
use crate::events::{BytesEnd, BytesText};
use crate::events::BytesText;
use pretty_assertions::assert_eq;

/// Checks that `peek()` and `read()` behaves correctly after `skip()`
Expand All @@ -1054,26 +1054,20 @@ mod tests {
assert_eq!(de.read, vec![]);
assert_eq!(de.write, vec![]);

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"root"))
);
assert_eq!(
de.peek().unwrap(),
&Start(BytesStart::borrowed_name(b"inner"))
);
assert_eq!(de.next().unwrap(), Start(QName(b"root").into()));
assert_eq!(de.peek().unwrap(), &Start(QName(b"inner").into()));

// Should skip first <inner> tree
de.skip().unwrap();
assert_eq!(de.read, vec![]);
assert_eq!(
de.write,
vec![
Start(BytesStart::borrowed_name(b"inner")),
Start(QName(b"inner").into()),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name(b"inner")),
End(BytesEnd::borrowed(b"inner")),
End(BytesEnd::borrowed(b"inner")),
Start(QName(b"inner").into()),
End(QName(b"inner").into()),
End(QName(b"inner").into()),
]
);

Expand All @@ -1085,11 +1079,8 @@ mod tests {
// </inner>
// <target/>
// </root>
assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"next"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"next")));
assert_eq!(de.next().unwrap(), Start(QName(b"next").into()));
assert_eq!(de.next().unwrap(), End(QName(b"next").into()));

// We finish writing. Next call to `next()` should start replay that messages:
//
Expand All @@ -1106,27 +1097,24 @@ mod tests {
assert_eq!(
de.read,
vec![
Start(BytesStart::borrowed_name(b"inner")),
Start(QName(b"inner").into()),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name(b"inner")),
End(BytesEnd::borrowed(b"inner")),
End(BytesEnd::borrowed(b"inner")),
Start(QName(b"inner").into()),
End(QName(b"inner").into()),
End(QName(b"inner").into()),
]
);
assert_eq!(de.write, vec![]);
assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"inner"))
);
assert_eq!(de.next().unwrap(), Start(QName(b"inner").into()));

// Skip `#text` node and consume <inner/> after it
de.skip().unwrap();
assert_eq!(
de.read,
vec![
Start(BytesStart::borrowed_name(b"inner")),
End(BytesEnd::borrowed(b"inner")),
End(BytesEnd::borrowed(b"inner")),
Start(QName(b"inner").into()),
End(QName(b"inner").into()),
End(QName(b"inner").into()),
]
);
assert_eq!(
Expand All @@ -1138,11 +1126,8 @@ mod tests {
]
);

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"inner"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"inner")));
assert_eq!(de.next().unwrap(), Start(QName(b"inner").into()));
assert_eq!(de.next().unwrap(), End(QName(b"inner").into()));

// We finish writing. Next call to `next()` should start replay messages:
//
Expand All @@ -1158,21 +1143,18 @@ mod tests {
de.read,
vec![
Text(BytesText::from_escaped_str("text")),
End(BytesEnd::borrowed(b"inner")),
End(QName(b"inner").into()),
]
);
assert_eq!(de.write, vec![]);
assert_eq!(
de.next().unwrap(),
Text(BytesText::from_escaped_str("text"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"inner")));
assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"target"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"target")));
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"root")));
assert_eq!(de.next().unwrap(), End(QName(b"inner").into()));
assert_eq!(de.next().unwrap(), Start(QName(b"target").into()));
assert_eq!(de.next().unwrap(), End(QName(b"target").into()));
assert_eq!(de.next().unwrap(), End(QName(b"root").into()));
}

/// Checks that `read_to_end()` behaves correctly after `skip()`
Expand All @@ -1196,22 +1178,19 @@ mod tests {
assert_eq!(de.read, vec![]);
assert_eq!(de.write, vec![]);

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"root"))
);
assert_eq!(de.next().unwrap(), Start(QName(b"root").into()));

// Skip the <skip> tree
de.skip().unwrap();
assert_eq!(de.read, vec![]);
assert_eq!(
de.write,
vec![
Start(BytesStart::borrowed_name(b"skip")),
Start(QName(b"skip").into()),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name(b"skip")),
End(BytesEnd::borrowed(b"skip")),
End(BytesEnd::borrowed(b"skip")),
Start(QName(b"skip").into()),
End(QName(b"skip").into()),
End(QName(b"skip").into()),
]
);

Expand All @@ -1222,20 +1201,17 @@ mod tests {
// <skip/>
// </skip>
// </root>
assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"target"))
);
assert_eq!(de.next().unwrap(), Start(QName(b"target").into()));
de.read_to_end(QName(b"target")).unwrap();
assert_eq!(de.read, vec![]);
assert_eq!(
de.write,
vec![
Start(BytesStart::borrowed_name(b"skip")),
Start(QName(b"skip").into()),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name(b"skip")),
End(BytesEnd::borrowed(b"skip")),
End(BytesEnd::borrowed(b"skip")),
Start(QName(b"skip").into()),
End(QName(b"skip").into()),
End(QName(b"skip").into()),
]
);

Expand All @@ -1253,22 +1229,19 @@ mod tests {
assert_eq!(
de.read,
vec![
Start(BytesStart::borrowed_name(b"skip")),
Start(QName(b"skip").into()),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name(b"skip")),
End(BytesEnd::borrowed(b"skip")),
End(BytesEnd::borrowed(b"skip")),
Start(QName(b"skip").into()),
End(QName(b"skip").into()),
End(QName(b"skip").into()),
]
);
assert_eq!(de.write, vec![]);

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"skip"))
);
assert_eq!(de.next().unwrap(), Start(QName(b"skip").into()));
de.read_to_end(QName(b"skip")).unwrap();

assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"root")));
assert_eq!(de.next().unwrap(), End(QName(b"root").into()));
}

/// Checks that limiting buffer size works correctly
Expand Down Expand Up @@ -1318,10 +1291,7 @@ mod tests {
"#,
);

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name(b"root"))
);
assert_eq!(de.next().unwrap(), Start(QName(b"root").into()));

assert_eq!(
de.next().unwrap(),
Expand All @@ -1337,15 +1307,15 @@ mod tests {
de.next().unwrap(),
CData(BytesCData::from_str("cdata content"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"tag")));
assert_eq!(de.next().unwrap(), End(QName(b"tag").into()));

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed(b"self-closed", 11))
);
assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());

assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed(b"root")));
assert_eq!(de.next().unwrap(), End(QName(b"root").into()));
assert_eq!(de.next().unwrap(), Eof);
}

Expand Down Expand Up @@ -1418,13 +1388,13 @@ mod tests {
4
)),
Text(BytesText::from_escaped(b"Some text".as_ref())),
End(BytesEnd::borrowed(b"item")),
End(QName(b"item").into()),
Start(BytesStart::borrowed(b"item2", 5)),
End(BytesEnd::borrowed(b"item2")),
End(QName(b"item2").into()),
Start(BytesStart::borrowed(b"item3", 5)),
End(BytesEnd::borrowed(b"item3")),
End(QName(b"item3").into()),
Start(BytesStart::borrowed(br#"item4 value="world" "#, 5)),
End(BytesEnd::borrowed(b"item4")),
End(QName(b"item4").into()),
]
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/de/seq.rs
Expand Up @@ -134,7 +134,9 @@ where

#[test]
fn test_not_in() {
let tag = BytesStart::borrowed_name(b"tag");
use crate::name::QName;

let tag = BytesStart::from(QName(b"tag"));

assert_eq!(not_in(&[], &tag, Decoder::utf8()).unwrap(), true);
assert_eq!(
Expand Down
15 changes: 14 additions & 1 deletion src/events/mod.rs
Expand Up @@ -209,7 +209,7 @@ impl<'a> BytesStart<'a> {

/// Creates new paired close tag
pub fn to_end(&self) -> BytesEnd {
BytesEnd::borrowed(self.name().into_inner())
BytesEnd::from(self.name())
}

/// Gets the undecoded raw tag name, as present in the input stream.
Expand Down Expand Up @@ -336,6 +336,13 @@ impl<'a> Deref for BytesStart<'a> {
}
}

impl<'a> From<QName<'a>> for BytesStart<'a> {
fn from(name: QName<'a>) -> Self {
let len = name.as_ref().len();
Self::borrowed(name.into_inner(), len)
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

/// An XML declaration (`Event::Decl`).
Expand Down Expand Up @@ -640,6 +647,12 @@ impl<'a> Deref for BytesEnd<'a> {
}
}

impl<'a> From<QName<'a>> for BytesEnd<'a> {
fn from(name: QName<'a>) -> Self {
Self::borrowed(name.into_inner())
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

/// Data from various events (most notably, `Event::Text`) that stored in XML
Expand Down

0 comments on commit 7280390

Please sign in to comment.