Skip to content

Commit 82146d5

Browse files
committed
Annotate everything to make it work
1 parent fda559a commit 82146d5

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

examples/custom_entities.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3535
match reader.read_event_into(&mut buf) {
3636
Ok(Event::DocType(ref e)) => {
3737
for cap in entity_re.captures_iter(&e) {
38-
custom_entities.insert(cap[1].to_vec(), String::from_utf8(cap[2].to_vec())?);
38+
custom_entities.insert(cap[1].to_vec(), reader.decoder().decode(&cap[2])?.into_owned());
3939
}
4040
}
4141
Ok(Event::Start(ref e)) => match e.name().as_ref() {
4242
b"test" => {
43-
let lookup_custom_entity = |ent| custom_entities.get(ent).map(|s| s.as_str());
4443
let attributes = e
4544
.attributes()
4645
.map(|a| {
4746
a.unwrap()
48-
.unescape_and_decode_value_with_custom_entities(
49-
&reader,
50-
lookup_custom_entity,
51-
)
47+
.unescape_and_decode_value_with_custom_entities(&reader, |ent| {
48+
custom_entities.get(ent).map(|s| s.as_str())
49+
})
5250
.unwrap()
5351
})
5452
.collect::<Vec<_>>();
@@ -57,10 +55,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5755
_ => (),
5856
},
5957
Ok(Event::Text(ref e)) => {
60-
let lookup_custom_entity = |ent| custom_entities.get(ent).map(|s| s.as_str());
6158
println!(
6259
"text value: {}",
63-
e.unescape_and_decode_with_custom_entities(&reader, lookup_custom_entity)
60+
e.unescape_and_decode_with_custom_entities(&reader, |ent| custom_entities
61+
.get(ent)
62+
.map(|s| s.as_str()))
6463
.unwrap()
6564
);
6665
}

src/escapei.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,15 @@ pub fn unescape(raw: &[u8]) -> Result<Cow<[u8]>, EscapeError> {
121121
///
122122
/// # Pre-condition
123123
///
124-
/// The implementation of `lookup_custom_entity` is expected to operate over UTF-8 inputs.
125-
pub fn unescape_with<'a>(raw: &'a [u8], resolve_entity: impl Fn(&[u8]) -> Option<&str>) -> Result<Cow<'a, [u8]>, EscapeError> {
124+
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
125+
pub fn unescape_with<'input, 'entity, F>(
126+
raw: &'input [u8],
127+
resolve_entity: F,
128+
) -> Result<Cow<'input, [u8]>, EscapeError>
129+
where
130+
// the lifetime of the output comes from a capture or is `'static`
131+
F: Fn(&[u8]) -> Option<&'entity str>,
132+
{
126133
let mut unescaped = None;
127134
let mut last_end = 0;
128135
let mut iter = memchr::memchr2_iter(b'&', b';', raw);

src/events/attributes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ impl<'a> Attribute<'a> {
5555
/// # Pre-condition
5656
///
5757
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
58-
pub fn unescaped_value_with_custom_entities<'s>(
58+
pub fn unescaped_value_with_custom_entities<'s, 'entity>(
5959
&'s self,
60-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
60+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
6161
) -> XmlResult<Cow<'s, [u8]>> {
6262
unescape_with(&*self.value, resolve_entity).map_err(Error::EscapeError)
6363
}
@@ -90,10 +90,10 @@ impl<'a> Attribute<'a> {
9090
/// # Pre-condition
9191
///
9292
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
93-
pub fn unescape_and_decode_value_with_custom_entities<B>(
93+
pub fn unescape_and_decode_value_with_custom_entities<'entity, B>(
9494
&self,
9595
reader: &Reader<B>,
96-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
96+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
9797
) -> XmlResult<String> {
9898
let decoded = reader.decoder().decode(&*self.value)?;
9999
let unescaped = unescape_with(decoded.as_bytes(), resolve_entity)?;

src/events/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -765,12 +765,12 @@ impl<'a> BytesText<'a> {
765765
///
766766
/// # Pre-condition
767767
///
768-
/// The implementation of `lookup_custom_entity` is expected to operate over UTF-8 inputs.
768+
/// 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>(
771+
pub fn unescaped_with_custom_entities<'s, 'entity>(
772772
&'s self,
773-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
773+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
774774
) -> Result<Cow<'s, [u8]>> {
775775
unescape_with(self, resolve_entity).map_err(Error::EscapeError)
776776
}
@@ -794,11 +794,11 @@ impl<'a> BytesText<'a> {
794794
///
795795
/// # Pre-condition
796796
///
797-
/// The implementation of `lookup_custom_entity` is expected to operate over UTF-8 inputs.
798-
pub fn unescape_and_decode_with_custom_entities<B>(
797+
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
798+
pub fn unescape_and_decode_with_custom_entities<'entity, B>(
799799
&self,
800800
reader: &Reader<B>,
801-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
801+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
802802
) -> Result<String> {
803803
let decoded = reader.decoder().decode(&*self)?;
804804

0 commit comments

Comments
 (0)