Skip to content

Commit

Permalink
Merge pull request #431 from Mingun/renames
Browse files Browse the repository at this point in the history
Use consistent naming for event constructors
  • Loading branch information
dralley authored Jul 24, 2022
2 parents e738b68 + 3e70ce4 commit d8ae1c3
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 273 deletions.
19 changes: 18 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

- [#423]: All escaping functions now accepts and returns strings instead of byte slices
- [#423]: Removed `BytesText::from_plain` because it internally did escaping of a byte array,
but since now escaping works on strings. Use `BytesText::from_plain_str` instead
but since now escaping works on strings. Use `BytesText::new` instead

- [#428]: Removed `BytesText::escaped()`. Use `.as_ref()` provided by `Deref` impl instead.
- [#428]: Removed `BytesText::from_escaped()`. Use constructors from strings instead,
Expand All @@ -159,6 +159,22 @@
- [#428]: Removed `Decoder` parameter from `_and_decode` versions of functions for
`BytesText` (remember, that those functions was renamed in #415).

- [#431]: Changed event constructors:
|Old names |New name
|--------------------------------------------------|----------------------------------------------
|`BytesStart::owned_name(impl Into<Vec<u8>>)` |`BytesStart::new(impl Into<Cow<str>>)`
|`BytesStart::borrowed_name(&[u8])` |_(as above)_
|`BytesStart::owned(impl Into<Vec<u8>>, usize)` |`BytesStart::from_content(impl Into<Cow<str>>, usize)`
|`BytesStart::borrowed(&[u8], usize)` |_(as above)_
|`BytesEnd::owned(Vec<u8>)` |`BytesEnd::new(impl Into<Cow<str>>)`
|`BytesEnd::borrowed(&[u8])` |_(as above)_
|`BytesText::from_escaped(impl Into<Cow<[u8]>>)` |`BytesText::from_escaped(impl Into<Cow<str>>)`
|`BytesText::from_escaped_str(impl Into<Cow<str>>)`|_(as above)_
|`BytesText::from_plain(&[u8])` |`BytesText::new(&str)`
|`BytesText::from_plain_str(&str)` |_(as above)_
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
|`BytesCData::from_str(&str)` |_(as above)_

### New Tests

- [#9]: Added tests for incorrect nested tags in input
Expand Down Expand Up @@ -190,6 +206,7 @@
[#421]: https://github.com/tafia/quick-xml/pull/421
[#423]: https://github.com/tafia/quick-xml/pull/423
[#428]: https://github.com/tafia/quick-xml/pull/428
[#431]: https://github.com/tafia/quick-xml/pull/431
[#434]: https://github.com/tafia/quick-xml/pull/434
[#437]: https://github.com/tafia/quick-xml/pull/437

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ loop {

// crates a new element ... alternatively we could reuse `e` by calling
// `e.into_owned()`
let mut elem = BytesStart::owned_name("my_elem");
let mut elem = BytesStart::new("my_elem");

// collect existing attributes
elem.extend_attributes(e.attributes().map(|attr| attr.unwrap()));
Expand All @@ -92,7 +92,7 @@ loop {
assert!(writer.write_event(Event::Start(elem)).is_ok());
},
Ok(Event::End(e)) if e.name().as_ref() == b"this_tag" => {
assert!(writer.write_event(Event::End(BytesEnd::borrowed("my_elem"))).is_ok());
assert!(writer.write_event(Event::End(BytesEnd::new("my_elem"))).is_ok());
},
Ok(Event::Eof) => break,
// we can either move or borrow the event to write, depending on your use-case
Expand Down
152 changes: 64 additions & 88 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,23 +1041,20 @@ mod tests {
assert_eq!(de.read, vec![]);
assert_eq!(de.write, vec![]);

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

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

Expand All @@ -1069,8 +1066,8 @@ mod tests {
// </inner>
// <target/>
// </root>
assert_eq!(de.next().unwrap(), Start(BytesStart::borrowed_name("next")));
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("next")));
assert_eq!(de.next().unwrap(), Start(BytesStart::new("next")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("next")));

// We finish writing. Next call to `next()` should start replay that messages:
//
Expand All @@ -1087,43 +1084,37 @@ mod tests {
assert_eq!(
de.read,
vec![
Start(BytesStart::borrowed_name("inner")),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name("inner")),
End(BytesEnd::borrowed("inner")),
End(BytesEnd::borrowed("inner")),
Start(BytesStart::new("inner")),
Text(BytesText::from_escaped("text")),
Start(BytesStart::new("inner")),
End(BytesEnd::new("inner")),
End(BytesEnd::new("inner")),
]
);
assert_eq!(de.write, vec![]);
assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name("inner"))
);
assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));

// Skip `#text` node and consume <inner/> after it
de.skip().unwrap();
assert_eq!(
de.read,
vec![
Start(BytesStart::borrowed_name("inner")),
End(BytesEnd::borrowed("inner")),
End(BytesEnd::borrowed("inner")),
Start(BytesStart::new("inner")),
End(BytesEnd::new("inner")),
End(BytesEnd::new("inner")),
]
);
assert_eq!(
de.write,
vec![
// This comment here to keep the same formatting of both arrays
// otherwise rustfmt suggest one-line it
Text(BytesText::from_escaped_str("text")),
Text(BytesText::from_escaped("text")),
]
);

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

// We finish writing. Next call to `next()` should start replay messages:
//
Expand All @@ -1138,22 +1129,16 @@ mod tests {
assert_eq!(
de.read,
vec![
Text(BytesText::from_escaped_str("text")),
End(BytesEnd::borrowed("inner")),
Text(BytesText::from_escaped("text")),
End(BytesEnd::new("inner")),
]
);
assert_eq!(de.write, vec![]);
assert_eq!(
de.next().unwrap(),
Text(BytesText::from_escaped_str("text"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner")));
assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed_name("target"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("target")));
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
assert_eq!(de.next().unwrap(), Text(BytesText::from_escaped("text")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("target")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
}

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

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

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

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

Expand All @@ -1231,19 +1213,19 @@ mod tests {
assert_eq!(
de.read,
vec![
Start(BytesStart::borrowed_name("skip")),
Text(BytesText::from_escaped_str("text")),
Start(BytesStart::borrowed_name("skip")),
End(BytesEnd::borrowed("skip")),
End(BytesEnd::borrowed("skip")),
Start(BytesStart::new("skip")),
Text(BytesText::from_escaped("text")),
Start(BytesStart::new("skip")),
End(BytesEnd::new("skip")),
End(BytesEnd::new("skip")),
]
);
assert_eq!(de.write, vec![]);

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

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

/// Checks that limiting buffer size works correctly
Expand Down Expand Up @@ -1293,31 +1275,25 @@ mod tests {
"#,
);

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

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed(r#"tag a="1""#, 3))
Start(BytesStart::from_content(r#"tag a="1""#, 3))
);
assert_eq!(de.read_to_end(QName(b"tag")).unwrap(), ());

assert_eq!(
de.next().unwrap(),
Start(BytesStart::borrowed(r#"tag a="2""#, 3))
Start(BytesStart::from_content(r#"tag a="2""#, 3))
);
assert_eq!(
de.next().unwrap(),
CData(BytesCData::from_str("cdata content"))
);
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("tag")));
assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));

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

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

Expand Down Expand Up @@ -1385,18 +1361,18 @@ mod tests {
assert_eq!(
events,
vec![
Start(BytesStart::borrowed(
Start(BytesStart::from_content(
r#"item name="hello" source="world.rs""#,
4
)),
Text(BytesText::from_escaped_str("Some text")),
End(BytesEnd::borrowed("item")),
Start(BytesStart::borrowed("item2", 5)),
End(BytesEnd::borrowed("item2")),
Start(BytesStart::borrowed("item3", 5)),
End(BytesEnd::borrowed("item3")),
Start(BytesStart::borrowed(r#"item4 value="world" "#, 5)),
End(BytesEnd::borrowed("item4")),
Text(BytesText::from_escaped("Some text")),
End(BytesEnd::new("item")),
Start(BytesStart::from_content("item2", 5)),
End(BytesEnd::new("item2")),
Start(BytesStart::from_content("item3", 5)),
End(BytesEnd::new("item3")),
Start(BytesStart::from_content(r#"item4 value="world" "#, 5)),
End(BytesEnd::new("item4")),
]
)
}
Expand All @@ -1416,7 +1392,7 @@ mod tests {

assert_eq!(
reader.next().unwrap(),
DeEvent::Start(BytesStart::borrowed("item ", 4))
DeEvent::Start(BytesStart::from_content("item ", 4))
);
reader.read_to_end(QName(b"item")).unwrap();
assert_eq!(reader.next().unwrap(), DeEvent::Eof);
Expand Down
2 changes: 1 addition & 1 deletion src/de/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ where

#[test]
fn test_not_in() {
let tag = BytesStart::borrowed_name("tag");
let tag = BytesStart::new("tag");

assert_eq!(not_in(&[], &tag, Decoder::utf8()).unwrap(), true);
assert_eq!(
Expand Down
Loading

0 comments on commit d8ae1c3

Please sign in to comment.