Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set entity resolver when deserialize using borrowing reader #615

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

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

### Bug Fixes

### Misc Changes


[#609]: https://github.com/tafia/quick-xml/issues/609
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be pull #609, not issue #609

My mistake, looks like I didn't fix it up completely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will fix. Just a moment...

[#615]: https://github.com/tafia/quick-xml/pull/615


## 0.29.0 -- 2023-06-13
Expand Down
35 changes: 27 additions & 8 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2648,9 +2648,22 @@ where
}

impl<'de> Deserializer<'de, SliceReader<'de>> {
/// Create new deserializer that will borrow data from the specified string
/// Create new deserializer that will borrow data from the specified string.
///
/// Deserializer created with this method will not resolve custom entities.
#[allow(clippy::should_implement_trait)]
pub fn from_str(source: &'de str) -> Self {
Self::from_str_with_resolver(source, NoEntityResolver)
}
}

impl<'de, E> Deserializer<'de, SliceReader<'de>, E>
where
E: EntityResolver,
{
/// Create new deserializer that will borrow data from the specified string
/// and use specified entity resolver.
pub fn from_str_with_resolver(source: &'de str, entity_resolver: E) -> Self {
let mut reader = Reader::from_str(source);
reader.expand_empty_elements(true);

Expand All @@ -2659,7 +2672,7 @@ impl<'de> Deserializer<'de, SliceReader<'de>> {
reader,
start_trimmer: StartTrimmer::default(),
},
NoEntityResolver,
entity_resolver,
)
}
}
Expand All @@ -2669,9 +2682,13 @@ where
R: BufRead,
{
/// Create new deserializer that will copy data from the specified reader
/// into internal buffer. If you already have a string use [`Self::from_str`]
/// instead, because it will borrow instead of copy. If you have `&[u8]` which
/// is known to represent UTF-8, you can decode it first before using [`from_str`].
/// into internal buffer.
///
/// If you already have a string use [`Self::from_str`] instead, because it
/// will borrow instead of copy. If you have `&[u8]` which is known to represent
/// UTF-8, you can decode it first before using [`from_str`].
///
/// Deserializer created with this method will not resolve custom entities.
pub fn from_reader(reader: R) -> Self {
Self::with_resolver(reader, NoEntityResolver)
}
Expand All @@ -2683,9 +2700,11 @@ where
E: EntityResolver,
{
/// Create new deserializer that will copy data from the specified reader
/// into internal buffer. If you already have a string use [`Self::from_str`]
/// instead, because it will borrow instead of copy. If you have `&[u8]` which
/// is known to represent UTF-8, you can decode it first before using [`from_str`].
/// into internal buffer and use specified entity resolver.
///
/// If you already have a string use [`Self::from_str`] instead, because it
/// will borrow instead of copy. If you have `&[u8]` which is known to represent
/// UTF-8, you can decode it first before using [`from_str`].
pub fn with_resolver(reader: R, entity_resolver: E) -> Self {
let mut reader = Reader::from_reader(reader);
reader.expand_empty_elements(true);
Expand Down