Skip to content

Commit 60249ae

Browse files
authored
Merge pull request #615 from Mingun/borrowing-resolver
Add ability to set entity resolver when deserialize using borrowing reader
2 parents 0db486f + 9430e2a commit 60249ae

File tree

2 files changed

+65
-65
lines changed

2 files changed

+65
-65
lines changed

Changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
- [#609]: Added `Writer::write_serializable` to provide the capability to serialize
1616
arbitrary types using serde when using the lower-level `Writer` API.
17+
- [#615]: Added ability to set entity resolver when deserialize using borrowing reader.
1718

1819
### Bug Fixes
1920

2021
### Misc Changes
2122

2223

23-
[#609]: https://github.com/tafia/quick-xml/issues/609
24+
[#609]: https://github.com/tafia/quick-xml/pull/609
25+
[#615]: https://github.com/tafia/quick-xml/pull/615
2426

2527

2628
## 0.29.0 -- 2023-06-13

src/de/mod.rs

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,14 +1813,6 @@ macro_rules! deserialize_primitives {
18131813
str2bool(&text, visitor)
18141814
}
18151815

1816-
/// Representation of owned strings the same as [non-owned](#method.deserialize_str).
1817-
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, DeError>
1818-
where
1819-
V: Visitor<'de>,
1820-
{
1821-
self.deserialize_str(visitor)
1822-
}
1823-
18241816
/// Character represented as [strings](#method.deserialize_str).
18251817
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, DeError>
18261818
where
@@ -1840,6 +1832,14 @@ macro_rules! deserialize_primitives {
18401832
}
18411833
}
18421834

1835+
/// Representation of owned strings the same as [non-owned](#method.deserialize_str).
1836+
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, DeError>
1837+
where
1838+
V: Visitor<'de>,
1839+
{
1840+
self.deserialize_str(visitor)
1841+
}
1842+
18431843
/// Returns [`DeError::Unsupported`]
18441844
fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value, DeError>
18451845
where
@@ -1856,7 +1856,7 @@ macro_rules! deserialize_primitives {
18561856
self.deserialize_bytes(visitor)
18571857
}
18581858

1859-
/// Representation of the named units the same as [unnamed units](#method.deserialize_unit)
1859+
/// Representation of the named units the same as [unnamed units](#method.deserialize_unit).
18601860
fn deserialize_unit_struct<V>(
18611861
self,
18621862
_name: &'static str,
@@ -1868,6 +1868,7 @@ macro_rules! deserialize_primitives {
18681868
self.deserialize_unit(visitor)
18691869
}
18701870

1871+
/// Representation of the newtypes the same as one-element [tuple](#method.deserialize_tuple).
18711872
fn deserialize_newtype_struct<V>(
18721873
self,
18731874
_name: &'static str,
@@ -2068,14 +2069,7 @@ struct XmlReader<'i, R: XmlRead<'i>, E: EntityResolver = NoEntityResolver> {
20682069
}
20692070

20702071
impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
2071-
fn new(reader: R) -> Self
2072-
where
2073-
E: Default,
2074-
{
2075-
Self::with_resolver(reader, E::default())
2076-
}
2077-
2078-
fn with_resolver(mut reader: R, entity_resolver: E) -> Self {
2072+
fn new(mut reader: R, entity_resolver: E) -> Self {
20792073
// Lookahead by one event immediately, so we do not need to check in the
20802074
// loop if we need lookahead or not
20812075
let lookahead = reader.next();
@@ -2319,19 +2313,20 @@ where
23192313
peek: Option<DeEvent<'de>>,
23202314
}
23212315

2322-
impl<'de, R> Deserializer<'de, R>
2316+
impl<'de, R, E> Deserializer<'de, R, E>
23232317
where
23242318
R: XmlRead<'de>,
2319+
E: EntityResolver,
23252320
{
23262321
/// Create an XML deserializer from one of the possible quick_xml input sources.
23272322
///
23282323
/// Typically it is more convenient to use one of these methods instead:
23292324
///
23302325
/// - [`Deserializer::from_str`]
23312326
/// - [`Deserializer::from_reader`]
2332-
fn new(reader: R) -> Self {
2327+
fn new(reader: R, entity_resolver: E) -> Self {
23332328
Self {
2334-
reader: XmlReader::new(reader),
2329+
reader: XmlReader::new(reader, entity_resolver),
23352330

23362331
#[cfg(feature = "overlapped-lists")]
23372332
read: VecDeque::new(),
@@ -2344,13 +2339,7 @@ where
23442339
peek: None,
23452340
}
23462341
}
2347-
}
23482342

2349-
impl<'de, R, E> Deserializer<'de, R, E>
2350-
where
2351-
R: XmlRead<'de>,
2352-
E: EntityResolver,
2353-
{
23542343
/// Set the maximum number of events that could be skipped during deserialization
23552344
/// of sequences.
23562345
///
@@ -2659,15 +2648,32 @@ where
26592648
}
26602649

26612650
impl<'de> Deserializer<'de, SliceReader<'de>> {
2662-
/// Create new deserializer that will borrow data from the specified string
2651+
/// Create new deserializer that will borrow data from the specified string.
2652+
///
2653+
/// Deserializer created with this method will not resolve custom entities.
26632654
#[allow(clippy::should_implement_trait)]
2664-
pub fn from_str(s: &'de str) -> Self {
2665-
let mut reader = Reader::from_str(s);
2666-
reader.expand_empty_elements(true).check_end_names(true);
2667-
Self::new(SliceReader {
2668-
reader,
2669-
start_trimmer: StartTrimmer::default(),
2670-
})
2655+
pub fn from_str(source: &'de str) -> Self {
2656+
Self::from_str_with_resolver(source, NoEntityResolver)
2657+
}
2658+
}
2659+
2660+
impl<'de, E> Deserializer<'de, SliceReader<'de>, E>
2661+
where
2662+
E: EntityResolver,
2663+
{
2664+
/// Create new deserializer that will borrow data from the specified string
2665+
/// and use specified entity resolver.
2666+
pub fn from_str_with_resolver(source: &'de str, entity_resolver: E) -> Self {
2667+
let mut reader = Reader::from_str(source);
2668+
reader.expand_empty_elements(true);
2669+
2670+
Self::new(
2671+
SliceReader {
2672+
reader,
2673+
start_trimmer: StartTrimmer::default(),
2674+
},
2675+
entity_resolver,
2676+
)
26712677
}
26722678
}
26732679

@@ -2676,9 +2682,13 @@ where
26762682
R: BufRead,
26772683
{
26782684
/// Create new deserializer that will copy data from the specified reader
2679-
/// into internal buffer. If you already have a string use [`Self::from_str`]
2680-
/// instead, because it will borrow instead of copy. If you have `&[u8]` which
2681-
/// is known to represent UTF-8, you can decode it first before using [`from_str`].
2685+
/// into internal buffer.
2686+
///
2687+
/// If you already have a string use [`Self::from_str`] instead, because it
2688+
/// will borrow instead of copy. If you have `&[u8]` which is known to represent
2689+
/// UTF-8, you can decode it first before using [`from_str`].
2690+
///
2691+
/// Deserializer created with this method will not resolve custom entities.
26822692
pub fn from_reader(reader: R) -> Self {
26832693
Self::with_resolver(reader, NoEntityResolver)
26842694
}
@@ -2690,32 +2700,23 @@ where
26902700
E: EntityResolver,
26912701
{
26922702
/// Create new deserializer that will copy data from the specified reader
2693-
/// into internal buffer. If you already have a string use [`Self::from_str`]
2694-
/// instead, because it will borrow instead of copy. If you have `&[u8]` which
2695-
/// is known to represent UTF-8, you can decode it first before using [`from_str`].
2703+
/// into internal buffer and use specified entity resolver.
2704+
///
2705+
/// If you already have a string use [`Self::from_str`] instead, because it
2706+
/// will borrow instead of copy. If you have `&[u8]` which is known to represent
2707+
/// UTF-8, you can decode it first before using [`from_str`].
26962708
pub fn with_resolver(reader: R, entity_resolver: E) -> Self {
26972709
let mut reader = Reader::from_reader(reader);
2698-
reader.expand_empty_elements(true).check_end_names(true);
2699-
2700-
let io_reader = IoReader {
2701-
reader,
2702-
start_trimmer: StartTrimmer::default(),
2703-
buf: Vec::new(),
2704-
};
2705-
2706-
Self {
2707-
reader: XmlReader::with_resolver(io_reader, entity_resolver),
2708-
2709-
#[cfg(feature = "overlapped-lists")]
2710-
read: VecDeque::new(),
2711-
#[cfg(feature = "overlapped-lists")]
2712-
write: VecDeque::new(),
2713-
#[cfg(feature = "overlapped-lists")]
2714-
limit: None,
2710+
reader.expand_empty_elements(true);
27152711

2716-
#[cfg(not(feature = "overlapped-lists"))]
2717-
peek: None,
2718-
}
2712+
Self::new(
2713+
IoReader {
2714+
reader,
2715+
start_trimmer: StartTrimmer::default(),
2716+
buf: Vec::new(),
2717+
},
2718+
entity_resolver,
2719+
)
27192720
}
27202721
}
27212722

@@ -3606,10 +3607,7 @@ mod tests {
36063607
start_trimmer: StartTrimmer::default(),
36073608
};
36083609

3609-
reader
3610-
.reader
3611-
.expand_empty_elements(true)
3612-
.check_end_names(true);
3610+
reader.reader.expand_empty_elements(true);
36133611

36143612
let mut events = Vec::new();
36153613

0 commit comments

Comments
 (0)