Skip to content

Commit d764f1e

Browse files
committed
Rename functions for consistency
"unescape_and_decode" -> "decode_and_unescape" "*_with_custom_entities" -> "*escape_with"
1 parent e36d5e2 commit d764f1e

File tree

8 files changed

+42
-39
lines changed

8 files changed

+42
-39
lines changed

Changelog.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@
111111
- [#412]: Change `read_to_end*` and `read_text_into` to accept `QName` instead of `AsRef<[u8]>`
112112
- [#415]: Changed custom entity unescaping API to accept closures rather than a mapping of entity to
113113
replacement text. This avoids needing to allocate a map and provides the user with more flexibility.
114-
114+
- [#415]: Renamed many functions following the pattern `unescape_and_decode*` to `decode_and_unescape*`
115+
to better communicate their function. Renamed functions following the pattern `*_with_custom_entities`
116+
to `decode_and_unescape_with` to be more consistent across the API.
115117
- [#416]: `BytesStart::to_borrowed` renamed to `BytesStart::borrow`, the same method
116118
added to all events
117119

examples/custom_entities.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4747
.attributes()
4848
.map(|a| {
4949
a.unwrap()
50-
.unescape_and_decode_value_with_custom_entities(&reader, |ent| {
50+
.decode_and_unescape_value_with(&reader, |ent| {
5151
custom_entities.get(ent).map(|s| s.as_str())
5252
})
5353
.unwrap()
@@ -60,7 +60,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6060
Ok(Event::Text(ref e)) => {
6161
println!(
6262
"text value: {}",
63-
e.unescape_and_decode_with_custom_entities(&reader, |ent| custom_entities
63+
e.decode_and_unescape_with(&reader, |ent| custom_entities
6464
.get(ent)
6565
.map(|s| s.as_str()))
6666
.unwrap()

src/events/attributes.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! Provides an iterator over attributes key/value pairs
44
5-
use crate::errors::{Error, Result as XmlResult};
5+
use crate::errors::Result as XmlResult;
66
use crate::escape::{escape, unescape_with};
77
use crate::name::QName;
88
use crate::reader::{is_whitespace, Reader};
@@ -14,11 +14,11 @@ use std::{borrow::Cow, ops::Range};
1414
/// A struct representing a key/value XML attribute.
1515
///
1616
/// Field `value` stores raw bytes, possibly containing escape-sequences. Most users will likely
17-
/// want to access the value using one of the [`unescaped_value`] and [`unescape_and_decode_value`]
17+
/// want to access the value using one of the [`unescaped_value`] and [`decode_and_unescape_value`]
1818
/// functions.
1919
///
2020
/// [`unescaped_value`]: Self::unescaped_value
21-
/// [`unescape_and_decode_value`]: Self::unescape_and_decode_value
21+
/// [`decode_and_unescape_value`]: Self::decode_and_unescape_value
2222
#[derive(Clone, PartialEq)]
2323
pub struct Attribute<'a> {
2424
/// The key to uniquely define the attribute.
@@ -37,32 +37,33 @@ impl<'a> Attribute<'a> {
3737
///
3838
/// This will allocate if the value contains any escape sequences.
3939
///
40-
/// See also [`unescaped_value_with_custom_entities()`](Self::unescaped_value_with_custom_entities)
40+
/// See also [`unescaped_value_with()`](Self::unescaped_value_with)
4141
pub fn unescaped_value(&self) -> XmlResult<Cow<[u8]>> {
42-
self.unescaped_value_with_custom_entities(|_| None)
42+
self.unescaped_value_with(|_| None)
4343
}
4444

4545
/// Returns the unescaped value, using custom entities.
4646
///
4747
/// This is normally the value you are interested in. Escape sequences such as `&gt;` are
4848
/// replaced with their unescaped equivalents such as `>`.
49-
/// Additional entities can be provided in `custom_entities`.
49+
/// A fallback resolver for additional custom entities can be provided via
50+
/// `resolve_entity`.
5051
///
5152
/// This will allocate if the value contains any escape sequences.
5253
///
5354
/// See also [`unescaped_value()`](Self::unescaped_value)
5455
///
5556
/// # Pre-condition
5657
///
57-
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
58-
pub fn unescaped_value_with_custom_entities<'s, 'entity>(
58+
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
59+
pub fn unescaped_value_with<'s, 'entity>(
5960
&'s self,
6061
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
6162
) -> XmlResult<Cow<'s, [u8]>> {
62-
unescape_with(&*self.value, resolve_entity).map_err(Error::EscapeError)
63+
Ok(unescape_with(&*self.value, resolve_entity)?)
6364
}
6465

65-
/// Decode then unescapes the value
66+
/// Decodes then unescapes the value
6667
///
6768
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
6869
/// instead use one of:
@@ -72,25 +73,25 @@ impl<'a> Attribute<'a> {
7273
///
7374
/// [`unescaped_value()`]: Self::unescaped_value
7475
/// [`Reader::decoder().decode()`]: crate::reader::Decoder::decode
75-
pub fn unescape_and_decode_value<B>(&self, reader: &Reader<B>) -> XmlResult<String> {
76-
self.unescape_and_decode_value_with_custom_entities(reader, |_| None)
76+
pub fn decode_and_unescape_value<B>(&self, reader: &Reader<B>) -> XmlResult<String> {
77+
self.decode_and_unescape_value_with(reader, |_| None)
7778
}
7879

79-
/// Decode then unescapes the value with custom entities
80+
/// Decodes then unescapes the value with custom entities
8081
///
8182
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
8283
/// instead use one of:
8384
///
8485
/// * [`Reader::decoder().decode()`], as it only allocates when the decoding can't be performed otherwise.
85-
/// * [`unescaped_value_with_custom_entities()`], as it doesn't allocate when no escape sequences are used.
86+
/// * [`unescaped_value_with()`], as it doesn't allocate when no escape sequences are used.
8687
///
87-
/// [`unescaped_value_with_custom_entities()`]: Self::unescaped_value_with_custom_entities
88+
/// [`unescaped_value_with()`]: Self::unescaped_value_with
8889
/// [`Reader::decoder().decode()`]: crate::reader::Decoder::decode
8990
///
9091
/// # Pre-condition
9192
///
92-
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
93-
pub fn unescape_and_decode_value_with_custom_entities<'entity, B>(
93+
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
94+
pub fn decode_and_unescape_value_with<'entity, B>(
9495
&self,
9596
reader: &Reader<B>,
9697
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,

src/events/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -752,50 +752,50 @@ impl<'a> BytesText<'a> {
752752
/// Searches for '&' into content and try to escape the coded character if possible
753753
/// returns Malformed error with index within element if '&' is not followed by ';'
754754
///
755-
/// See also [`unescaped_with_custom_entities()`](Self::unescaped_with_custom_entities)
755+
/// See also [`unescaped_with()`](Self::unescaped_with)
756756
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
757-
self.unescaped_with_custom_entities(|_| None)
757+
self.unescaped_with(|_| None)
758758
}
759759

760760
/// gets escaped content with custom entities
761761
///
762762
/// Searches for '&' into content and try to escape the coded character if possible
763763
/// returns Malformed error with index within element if '&' is not followed by ';'
764-
/// Additional entities can be provided in `custom_entities`.
764+
/// A fallback resolver for additional custom entities can be provided via `resolve_entity`.
765765
///
766766
/// # Pre-condition
767767
///
768768
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
769769
///
770770
/// See also [`unescaped()`](Self::unescaped)
771-
pub fn unescaped_with_custom_entities<'s, 'entity>(
771+
pub fn unescaped_with<'s, 'entity>(
772772
&'s self,
773773
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
774774
) -> Result<Cow<'s, [u8]>> {
775-
unescape_with(self, resolve_entity).map_err(Error::EscapeError)
775+
Ok(unescape_with(self, resolve_entity)?)
776776
}
777777

778778
/// helper method to unescape then decode self using the reader encoding
779779
///
780780
/// for performance reasons (could avoid allocating a `String`),
781781
/// it might be wiser to manually use
782-
/// 1. BytesText::unescaped()
783-
/// 2. Reader::decode(...)
784-
pub fn unescape_and_decode<B>(&self, reader: &Reader<B>) -> Result<String> {
785-
self.unescape_and_decode_with_custom_entities(reader, |_| None)
782+
/// 1. Reader::decode(...)
783+
/// 2. BytesText::unescaped()
784+
pub fn decode_and_unescape<B>(&self, reader: &Reader<B>) -> Result<String> {
785+
self.decode_and_unescape_with(reader, |_| None)
786786
}
787787

788788
/// helper method to unescape then decode self using the reader encoding with custom entities
789789
///
790790
/// for performance reasons (could avoid allocating a `String`),
791791
/// it might be wiser to manually use
792-
/// 1. BytesText::unescaped()
793-
/// 2. Reader::decode(...)
792+
/// 1. Reader::decode(...)
793+
/// 2. BytesText::unescaped()
794794
///
795795
/// # Pre-condition
796796
///
797797
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
798-
pub fn unescape_and_decode_with_custom_entities<'entity, B>(
798+
pub fn decode_and_unescape_with<'entity, B>(
799799
&self,
800800
reader: &Reader<B>,
801801
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
//! }
6060
//! },
6161
//! // unescape and decode the text event using the reader encoding
62-
//! Ok(Event::Text(e)) => txt.push(e.unescape_and_decode(&reader).unwrap()),
62+
//! Ok(Event::Text(e)) => txt.push(e.decode_and_unescape(&reader).unwrap()),
6363
//! Ok(Event::Eof) => break, // exits the loop when reaching end of file
6464
//! Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
6565
//! _ => (), // There are several other `Event`s we do not consider here

src/reader.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl EncodingRef {
135135
/// _ => (),
136136
/// }
137137
/// },
138-
/// Ok(Event::Text(e)) => txt.push(e.unescape_and_decode(&reader).unwrap()),
138+
/// Ok(Event::Text(e)) => txt.push(e.decode_and_unescape(&reader).unwrap()),
139139
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
140140
/// Ok(Event::Eof) => break,
141141
/// _ => (),
@@ -496,7 +496,7 @@ impl<R: BufRead> Reader<R> {
496496
/// loop {
497497
/// match reader.read_event_into(&mut buf) {
498498
/// Ok(Event::Start(ref e)) => count += 1,
499-
/// Ok(Event::Text(e)) => txt.push(e.unescape_and_decode(&reader).expect("Error!")),
499+
/// Ok(Event::Text(e)) => txt.push(e.decode_and_unescape(&reader).expect("Error!")),
500500
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
501501
/// Ok(Event::Eof) => break,
502502
/// _ => (),
@@ -549,7 +549,7 @@ impl<R: BufRead> Reader<R> {
549549
/// panic!("Undeclared namespace prefix {:?}", String::from_utf8(p))
550550
/// }
551551
/// Ok((_, Event::Text(e))) => {
552-
/// txt.push(e.unescape_and_decode(&reader).expect("Error!"))
552+
/// txt.push(e.decode_and_unescape(&reader).expect("Error!"))
553553
/// },
554554
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
555555
/// Ok((_, Event::Eof)) => break,
@@ -750,7 +750,7 @@ impl<R: BufRead> Reader<R> {
750750
let s = match self.read_event_into(buf) {
751751
Err(e) => return Err(e),
752752

753-
Ok(Event::Text(e)) => e.unescape_and_decode(self),
753+
Ok(Event::Text(e)) => e.decode_and_unescape(self),
754754
Ok(Event::End(e)) if e.name() == end => return Ok("".to_string()),
755755
Ok(Event::Eof) => return Err(Error::UnexpectedEof("Text".to_string())),
756756
_ => return Err(Error::TextNotFound),

tests/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn test_koi8_r_encoding() {
104104
loop {
105105
match r.read_event_into(&mut buf) {
106106
Ok(Text(e)) => {
107-
e.unescape_and_decode(&r).unwrap();
107+
e.decode_and_unescape(&r).unwrap();
108108
}
109109
Ok(Eof) => break,
110110
_ => (),

tests/unit_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ fn test_read_write_roundtrip_escape_text() -> Result<()> {
653653
match reader.read_event_into(&mut buf)? {
654654
Eof => break,
655655
Text(e) => {
656-
let t = e.unescape_and_decode(&reader).unwrap();
656+
let t = e.decode_and_unescape(&reader).unwrap();
657657
assert!(writer
658658
.write_event(Text(BytesText::from_plain_str(&t)))
659659
.is_ok());

0 commit comments

Comments
 (0)