Skip to content

Commit d8ae1c3

Browse files
authored
Merge pull request #431 from Mingun/renames
Use consistent naming for event constructors
2 parents e738b68 + 3e70ce4 commit d8ae1c3

14 files changed

+250
-273
lines changed

Changelog.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147

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

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

162+
- [#431]: Changed event constructors:
163+
|Old names |New name
164+
|--------------------------------------------------|----------------------------------------------
165+
|`BytesStart::owned_name(impl Into<Vec<u8>>)` |`BytesStart::new(impl Into<Cow<str>>)`
166+
|`BytesStart::borrowed_name(&[u8])` |_(as above)_
167+
|`BytesStart::owned(impl Into<Vec<u8>>, usize)` |`BytesStart::from_content(impl Into<Cow<str>>, usize)`
168+
|`BytesStart::borrowed(&[u8], usize)` |_(as above)_
169+
|`BytesEnd::owned(Vec<u8>)` |`BytesEnd::new(impl Into<Cow<str>>)`
170+
|`BytesEnd::borrowed(&[u8])` |_(as above)_
171+
|`BytesText::from_escaped(impl Into<Cow<[u8]>>)` |`BytesText::from_escaped(impl Into<Cow<str>>)`
172+
|`BytesText::from_escaped_str(impl Into<Cow<str>>)`|_(as above)_
173+
|`BytesText::from_plain(&[u8])` |`BytesText::new(&str)`
174+
|`BytesText::from_plain_str(&str)` |_(as above)_
175+
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
176+
|`BytesCData::from_str(&str)` |_(as above)_
177+
162178
### New Tests
163179

164180
- [#9]: Added tests for incorrect nested tags in input
@@ -190,6 +206,7 @@
190206
[#421]: https://github.com/tafia/quick-xml/pull/421
191207
[#423]: https://github.com/tafia/quick-xml/pull/423
192208
[#428]: https://github.com/tafia/quick-xml/pull/428
209+
[#431]: https://github.com/tafia/quick-xml/pull/431
193210
[#434]: https://github.com/tafia/quick-xml/pull/434
194211
[#437]: https://github.com/tafia/quick-xml/pull/437
195212

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ loop {
8080

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

8585
// collect existing attributes
8686
elem.extend_attributes(e.attributes().map(|attr| attr.unwrap()));
@@ -92,7 +92,7 @@ loop {
9292
assert!(writer.write_event(Event::Start(elem)).is_ok());
9393
},
9494
Ok(Event::End(e)) if e.name().as_ref() == b"this_tag" => {
95-
assert!(writer.write_event(Event::End(BytesEnd::borrowed("my_elem"))).is_ok());
95+
assert!(writer.write_event(Event::End(BytesEnd::new("my_elem"))).is_ok());
9696
},
9797
Ok(Event::Eof) => break,
9898
// we can either move or borrow the event to write, depending on your use-case

src/de/mod.rs

Lines changed: 64 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,23 +1041,20 @@ mod tests {
10411041
assert_eq!(de.read, vec![]);
10421042
assert_eq!(de.write, vec![]);
10431043

1044-
assert_eq!(de.next().unwrap(), Start(BytesStart::borrowed_name("root")));
1045-
assert_eq!(
1046-
de.peek().unwrap(),
1047-
&Start(BytesStart::borrowed_name("inner"))
1048-
);
1044+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
1045+
assert_eq!(de.peek().unwrap(), &Start(BytesStart::new("inner")));
10491046

10501047
// Should skip first <inner> tree
10511048
de.skip().unwrap();
10521049
assert_eq!(de.read, vec![]);
10531050
assert_eq!(
10541051
de.write,
10551052
vec![
1056-
Start(BytesStart::borrowed_name("inner")),
1057-
Text(BytesText::from_escaped_str("text")),
1058-
Start(BytesStart::borrowed_name("inner")),
1059-
End(BytesEnd::borrowed("inner")),
1060-
End(BytesEnd::borrowed("inner")),
1053+
Start(BytesStart::new("inner")),
1054+
Text(BytesText::from_escaped("text")),
1055+
Start(BytesStart::new("inner")),
1056+
End(BytesEnd::new("inner")),
1057+
End(BytesEnd::new("inner")),
10611058
]
10621059
);
10631060

@@ -1069,8 +1066,8 @@ mod tests {
10691066
// </inner>
10701067
// <target/>
10711068
// </root>
1072-
assert_eq!(de.next().unwrap(), Start(BytesStart::borrowed_name("next")));
1073-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("next")));
1069+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("next")));
1070+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("next")));
10741071

10751072
// We finish writing. Next call to `next()` should start replay that messages:
10761073
//
@@ -1087,43 +1084,37 @@ mod tests {
10871084
assert_eq!(
10881085
de.read,
10891086
vec![
1090-
Start(BytesStart::borrowed_name("inner")),
1091-
Text(BytesText::from_escaped_str("text")),
1092-
Start(BytesStart::borrowed_name("inner")),
1093-
End(BytesEnd::borrowed("inner")),
1094-
End(BytesEnd::borrowed("inner")),
1087+
Start(BytesStart::new("inner")),
1088+
Text(BytesText::from_escaped("text")),
1089+
Start(BytesStart::new("inner")),
1090+
End(BytesEnd::new("inner")),
1091+
End(BytesEnd::new("inner")),
10951092
]
10961093
);
10971094
assert_eq!(de.write, vec![]);
1098-
assert_eq!(
1099-
de.next().unwrap(),
1100-
Start(BytesStart::borrowed_name("inner"))
1101-
);
1095+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));
11021096

11031097
// Skip `#text` node and consume <inner/> after it
11041098
de.skip().unwrap();
11051099
assert_eq!(
11061100
de.read,
11071101
vec![
1108-
Start(BytesStart::borrowed_name("inner")),
1109-
End(BytesEnd::borrowed("inner")),
1110-
End(BytesEnd::borrowed("inner")),
1102+
Start(BytesStart::new("inner")),
1103+
End(BytesEnd::new("inner")),
1104+
End(BytesEnd::new("inner")),
11111105
]
11121106
);
11131107
assert_eq!(
11141108
de.write,
11151109
vec![
11161110
// This comment here to keep the same formatting of both arrays
11171111
// otherwise rustfmt suggest one-line it
1118-
Text(BytesText::from_escaped_str("text")),
1112+
Text(BytesText::from_escaped("text")),
11191113
]
11201114
);
11211115

1122-
assert_eq!(
1123-
de.next().unwrap(),
1124-
Start(BytesStart::borrowed_name("inner"))
1125-
);
1126-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner")));
1116+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("inner")));
1117+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
11271118

11281119
// We finish writing. Next call to `next()` should start replay messages:
11291120
//
@@ -1138,22 +1129,16 @@ mod tests {
11381129
assert_eq!(
11391130
de.read,
11401131
vec![
1141-
Text(BytesText::from_escaped_str("text")),
1142-
End(BytesEnd::borrowed("inner")),
1132+
Text(BytesText::from_escaped("text")),
1133+
End(BytesEnd::new("inner")),
11431134
]
11441135
);
11451136
assert_eq!(de.write, vec![]);
1146-
assert_eq!(
1147-
de.next().unwrap(),
1148-
Text(BytesText::from_escaped_str("text"))
1149-
);
1150-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("inner")));
1151-
assert_eq!(
1152-
de.next().unwrap(),
1153-
Start(BytesStart::borrowed_name("target"))
1154-
);
1155-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("target")));
1156-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
1137+
assert_eq!(de.next().unwrap(), Text(BytesText::from_escaped("text")));
1138+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
1139+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
1140+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("target")));
1141+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
11571142
}
11581143

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

1180-
assert_eq!(de.next().unwrap(), Start(BytesStart::borrowed_name("root")));
1165+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
11811166

11821167
// Skip the <skip> tree
11831168
de.skip().unwrap();
11841169
assert_eq!(de.read, vec![]);
11851170
assert_eq!(
11861171
de.write,
11871172
vec![
1188-
Start(BytesStart::borrowed_name("skip")),
1189-
Text(BytesText::from_escaped_str("text")),
1190-
Start(BytesStart::borrowed_name("skip")),
1191-
End(BytesEnd::borrowed("skip")),
1192-
End(BytesEnd::borrowed("skip")),
1173+
Start(BytesStart::new("skip")),
1174+
Text(BytesText::from_escaped("text")),
1175+
Start(BytesStart::new("skip")),
1176+
End(BytesEnd::new("skip")),
1177+
End(BytesEnd::new("skip")),
11931178
]
11941179
);
11951180

@@ -1200,20 +1185,17 @@ mod tests {
12001185
// <skip/>
12011186
// </skip>
12021187
// </root>
1203-
assert_eq!(
1204-
de.next().unwrap(),
1205-
Start(BytesStart::borrowed_name("target"))
1206-
);
1188+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
12071189
de.read_to_end(QName(b"target")).unwrap();
12081190
assert_eq!(de.read, vec![]);
12091191
assert_eq!(
12101192
de.write,
12111193
vec![
1212-
Start(BytesStart::borrowed_name("skip")),
1213-
Text(BytesText::from_escaped_str("text")),
1214-
Start(BytesStart::borrowed_name("skip")),
1215-
End(BytesEnd::borrowed("skip")),
1216-
End(BytesEnd::borrowed("skip")),
1194+
Start(BytesStart::new("skip")),
1195+
Text(BytesText::from_escaped("text")),
1196+
Start(BytesStart::new("skip")),
1197+
End(BytesEnd::new("skip")),
1198+
End(BytesEnd::new("skip")),
12171199
]
12181200
);
12191201

@@ -1231,19 +1213,19 @@ mod tests {
12311213
assert_eq!(
12321214
de.read,
12331215
vec![
1234-
Start(BytesStart::borrowed_name("skip")),
1235-
Text(BytesText::from_escaped_str("text")),
1236-
Start(BytesStart::borrowed_name("skip")),
1237-
End(BytesEnd::borrowed("skip")),
1238-
End(BytesEnd::borrowed("skip")),
1216+
Start(BytesStart::new("skip")),
1217+
Text(BytesText::from_escaped("text")),
1218+
Start(BytesStart::new("skip")),
1219+
End(BytesEnd::new("skip")),
1220+
End(BytesEnd::new("skip")),
12391221
]
12401222
);
12411223
assert_eq!(de.write, vec![]);
12421224

1243-
assert_eq!(de.next().unwrap(), Start(BytesStart::borrowed_name("skip")));
1225+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("skip")));
12441226
de.read_to_end(QName(b"skip")).unwrap();
12451227

1246-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
1228+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
12471229
}
12481230

12491231
/// Checks that limiting buffer size works correctly
@@ -1293,31 +1275,25 @@ mod tests {
12931275
"#,
12941276
);
12951277

1296-
assert_eq!(de.next().unwrap(), Start(BytesStart::borrowed_name("root")));
1278+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("root")));
12971279

12981280
assert_eq!(
12991281
de.next().unwrap(),
1300-
Start(BytesStart::borrowed(r#"tag a="1""#, 3))
1282+
Start(BytesStart::from_content(r#"tag a="1""#, 3))
13011283
);
13021284
assert_eq!(de.read_to_end(QName(b"tag")).unwrap(), ());
13031285

13041286
assert_eq!(
13051287
de.next().unwrap(),
1306-
Start(BytesStart::borrowed(r#"tag a="2""#, 3))
1288+
Start(BytesStart::from_content(r#"tag a="2""#, 3))
13071289
);
1308-
assert_eq!(
1309-
de.next().unwrap(),
1310-
CData(BytesCData::from_str("cdata content"))
1311-
);
1312-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("tag")));
1290+
assert_eq!(de.next().unwrap(), CData(BytesCData::new("cdata content")));
1291+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));
13131292

1314-
assert_eq!(
1315-
de.next().unwrap(),
1316-
Start(BytesStart::borrowed_name("self-closed"))
1317-
);
1293+
assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed")));
13181294
assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());
13191295

1320-
assert_eq!(de.next().unwrap(), End(BytesEnd::borrowed("root")));
1296+
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
13211297
assert_eq!(de.next().unwrap(), Eof);
13221298
}
13231299

@@ -1385,18 +1361,18 @@ mod tests {
13851361
assert_eq!(
13861362
events,
13871363
vec![
1388-
Start(BytesStart::borrowed(
1364+
Start(BytesStart::from_content(
13891365
r#"item name="hello" source="world.rs""#,
13901366
4
13911367
)),
1392-
Text(BytesText::from_escaped_str("Some text")),
1393-
End(BytesEnd::borrowed("item")),
1394-
Start(BytesStart::borrowed("item2", 5)),
1395-
End(BytesEnd::borrowed("item2")),
1396-
Start(BytesStart::borrowed("item3", 5)),
1397-
End(BytesEnd::borrowed("item3")),
1398-
Start(BytesStart::borrowed(r#"item4 value="world" "#, 5)),
1399-
End(BytesEnd::borrowed("item4")),
1368+
Text(BytesText::from_escaped("Some text")),
1369+
End(BytesEnd::new("item")),
1370+
Start(BytesStart::from_content("item2", 5)),
1371+
End(BytesEnd::new("item2")),
1372+
Start(BytesStart::from_content("item3", 5)),
1373+
End(BytesEnd::new("item3")),
1374+
Start(BytesStart::from_content(r#"item4 value="world" "#, 5)),
1375+
End(BytesEnd::new("item4")),
14001376
]
14011377
)
14021378
}
@@ -1416,7 +1392,7 @@ mod tests {
14161392

14171393
assert_eq!(
14181394
reader.next().unwrap(),
1419-
DeEvent::Start(BytesStart::borrowed("item ", 4))
1395+
DeEvent::Start(BytesStart::from_content("item ", 4))
14201396
);
14211397
reader.read_to_end(QName(b"item")).unwrap();
14221398
assert_eq!(reader.next().unwrap(), DeEvent::Eof);

src/de/seq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ where
134134

135135
#[test]
136136
fn test_not_in() {
137-
let tag = BytesStart::borrowed_name("tag");
137+
let tag = BytesStart::new("tag");
138138

139139
assert_eq!(not_in(&[], &tag, Decoder::utf8()).unwrap(), true);
140140
assert_eq!(

0 commit comments

Comments
 (0)