Skip to content

Commit fd609e2

Browse files
committed
Annotate everything to make it work
1 parent 716200b commit fd609e2

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

examples/custom_entities.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3232
let entity_re = Regex::new(r#"<!ENTITY\s+([^ \t\r\n]+)\s+"([^"]*)"\s*>"#)?;
3333

3434
loop {
35+
let lookup_custom_entity = |ent: &_| custom_entities.get(ent).map(|s| s.as_str());
36+
3537
match reader.read_event_into(&mut buf) {
3638
Ok(Event::DocType(ref e)) => {
3739
for cap in entity_re.captures_iter(&e) {
@@ -40,7 +42,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4042
}
4143
Ok(Event::Start(ref e)) => match e.name().as_ref() {
4244
b"test" => {
43-
let lookup_custom_entity = |ent| custom_entities.get(ent).map(|s| s.as_str());
45+
// let lookup_custom_entity = |ent: &_| custom_entities.get(ent).map(|s| s.as_str());
4446
let attributes = e
4547
.attributes()
4648
.map(|a| {
@@ -57,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5759
_ => (),
5860
},
5961
Ok(Event::Text(ref e)) => {
60-
let lookup_custom_entity = |ent| custom_entities.get(ent).map(|s| s.as_str());
62+
// let lookup_custom_entity = |ent: &_| custom_entities.get(ent).map(|s| s.as_str());
6163
println!(
6264
"text value: {}",
6365
e.unescape_and_decode_with_custom_entities(&reader, lookup_custom_entity)

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
@@ -57,9 +57,9 @@ impl<'a> Attribute<'a> {
5757
/// # Pre-condition
5858
///
5959
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
60-
pub fn unescaped_value_with_custom_entities<'s>(
60+
pub fn unescaped_value_with_custom_entities<'s, 'entity>(
6161
&'s self,
62-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
62+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
6363
) -> XmlResult<Cow<'s, [u8]>> {
6464
unescape_with(&*self.value, resolve_entity).map_err(Error::EscapeError)
6565
}
@@ -92,10 +92,10 @@ impl<'a> Attribute<'a> {
9292
/// # Pre-condition
9393
///
9494
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
95-
pub fn unescape_and_decode_value_with_custom_entities<B>(
95+
pub fn unescape_and_decode_value_with_custom_entities<'entity, B>(
9696
&self,
9797
reader: &Reader<B>,
98-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
98+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
9999
) -> XmlResult<String> {
100100
let decoded = reader.decoder().decode(&*self.value)?;
101101
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
@@ -732,12 +732,12 @@ impl<'a> BytesText<'a> {
732732
///
733733
/// # Pre-condition
734734
///
735-
/// The implementation of `lookup_custom_entity` is expected to operate over UTF-8 inputs.
735+
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
736736
///
737737
/// See also [`unescaped()`](#method.unescaped)
738-
pub fn unescaped_with_custom_entities<'s>(
738+
pub fn unescaped_with_custom_entities<'s, 'entity>(
739739
&'s self,
740-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
740+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
741741
) -> Result<Cow<'s, [u8]>> {
742742
unescape_with(self, resolve_entity).map_err(Error::EscapeError)
743743
}
@@ -761,11 +761,11 @@ impl<'a> BytesText<'a> {
761761
///
762762
/// # Pre-condition
763763
///
764-
/// The implementation of `lookup_custom_entity` is expected to operate over UTF-8 inputs.
765-
pub fn unescape_and_decode_with_custom_entities<B>(
764+
/// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
765+
pub fn unescape_and_decode_with_custom_entities<'entity, B>(
766766
&self,
767767
reader: &Reader<B>,
768-
resolve_entity: impl Fn(&[u8]) -> Option<&str>,
768+
resolve_entity: impl Fn(&[u8]) -> Option<&'entity str>,
769769
) -> Result<String> {
770770
let decoded = reader.decoder().decode(&*self)?;
771771

0 commit comments

Comments
 (0)