2
2
//!
3
3
//! Provides an iterator over attributes key/value pairs
4
4
5
- use crate :: errors:: { Error , Result as XmlResult } ;
5
+ use crate :: errors:: Result as XmlResult ;
6
6
use crate :: escape:: { escape, unescape_with} ;
7
7
use crate :: name:: QName ;
8
8
use crate :: reader:: { is_whitespace, Reader } ;
@@ -14,11 +14,11 @@ use std::{borrow::Cow, ops::Range};
14
14
/// A struct representing a key/value XML attribute.
15
15
///
16
16
/// 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 `]
18
18
/// functions.
19
19
///
20
20
/// [`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
22
22
#[ derive( Clone , PartialEq ) ]
23
23
pub struct Attribute < ' a > {
24
24
/// The key to uniquely define the attribute.
@@ -37,32 +37,33 @@ impl<'a> Attribute<'a> {
37
37
///
38
38
/// This will allocate if the value contains any escape sequences.
39
39
///
40
- /// See also [`unescaped_value_with_custom_entities ()`](Self::unescaped_value_with_custom_entities )
40
+ /// See also [`unescaped_value_with ()`](Self::unescaped_value_with )
41
41
pub fn unescaped_value ( & self ) -> XmlResult < Cow < [ u8 ] > > {
42
- self . unescaped_value_with_custom_entities ( |_| None )
42
+ self . unescaped_value_with ( |_| None )
43
43
}
44
44
45
45
/// Returns the unescaped value, using custom entities.
46
46
///
47
47
/// This is normally the value you are interested in. Escape sequences such as `>` are
48
48
/// 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`.
50
51
///
51
52
/// This will allocate if the value contains any escape sequences.
52
53
///
53
54
/// See also [`unescaped_value()`](Self::unescaped_value)
54
55
///
55
56
/// # Pre-condition
56
57
///
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 > (
59
60
& ' s self ,
60
61
resolve_entity : impl Fn ( & [ u8 ] ) -> Option < & ' entity str > ,
61
62
) -> XmlResult < Cow < ' s , [ u8 ] > > {
62
- unescape_with ( & * self . value , resolve_entity) . map_err ( Error :: EscapeError )
63
+ Ok ( unescape_with ( & * self . value , resolve_entity) ? )
63
64
}
64
65
65
- /// Decode then unescapes the value
66
+ /// Decodes then unescapes the value
66
67
///
67
68
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
68
69
/// instead use one of:
@@ -72,25 +73,25 @@ impl<'a> Attribute<'a> {
72
73
///
73
74
/// [`unescaped_value()`]: Self::unescaped_value
74
75
/// [`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 )
77
78
}
78
79
79
- /// Decode then unescapes the value with custom entities
80
+ /// Decodes then unescapes the value with custom entities
80
81
///
81
82
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
82
83
/// instead use one of:
83
84
///
84
85
/// * [`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.
86
87
///
87
- /// [`unescaped_value_with_custom_entities ()`]: Self::unescaped_value_with_custom_entities
88
+ /// [`unescaped_value_with ()`]: Self::unescaped_value_with
88
89
/// [`Reader::decoder().decode()`]: crate::reader::Decoder::decode
89
90
///
90
91
/// # Pre-condition
91
92
///
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 > (
94
95
& self ,
95
96
reader : & Reader < B > ,
96
97
resolve_entity : impl Fn ( & [ u8 ] ) -> Option < & ' entity str > ,
0 commit comments