Skip to content

Commit

Permalink
Deduplicate methods that creates deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jun 21, 2023
1 parent 6ab17be commit 4d8804d
Showing 1 changed file with 23 additions and 42 deletions.
65 changes: 23 additions & 42 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,14 +2069,7 @@ struct XmlReader<'i, R: XmlRead<'i>, E: EntityResolver = NoEntityResolver> {
}

impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
fn new(reader: R) -> Self
where
E: Default,
{
Self::with_resolver(reader, E::default())
}

fn with_resolver(mut reader: R, entity_resolver: E) -> Self {
fn new(mut reader: R, entity_resolver: E) -> Self {
// Lookahead by one event immediately, so we do not need to check in the
// loop if we need lookahead or not
let lookahead = reader.next();
Expand Down Expand Up @@ -2320,19 +2313,20 @@ where
peek: Option<DeEvent<'de>>,
}

impl<'de, R> Deserializer<'de, R>
impl<'de, R, E> Deserializer<'de, R, E>
where
R: XmlRead<'de>,
E: EntityResolver,
{
/// Create an XML deserializer from one of the possible quick_xml input sources.
///
/// Typically it is more convenient to use one of these methods instead:
///
/// - [`Deserializer::from_str`]
/// - [`Deserializer::from_reader`]
fn new(reader: R) -> Self {
fn new(reader: R, entity_resolver: E) -> Self {
Self {
reader: XmlReader::new(reader),
reader: XmlReader::new(reader, entity_resolver),

#[cfg(feature = "overlapped-lists")]
read: VecDeque::new(),
Expand All @@ -2345,13 +2339,7 @@ where
peek: None,
}
}
}

impl<'de, R, E> Deserializer<'de, R, E>
where
R: XmlRead<'de>,
E: EntityResolver,
{
/// Set the maximum number of events that could be skipped during deserialization
/// of sequences.
///
Expand Down Expand Up @@ -2662,13 +2650,17 @@ where
impl<'de> Deserializer<'de, SliceReader<'de>> {
/// Create new deserializer that will borrow data from the specified string
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &'de str) -> Self {
let mut reader = Reader::from_str(s);
pub fn from_str(source: &'de str) -> Self {
let mut reader = Reader::from_str(source);
reader.expand_empty_elements(true).check_end_names(true);
Self::new(SliceReader {
reader,
start_trimmer: StartTrimmer::default(),
})

Self::new(
SliceReader {
reader,
start_trimmer: StartTrimmer::default(),
},
NoEntityResolver,
)
}
}

Expand Down Expand Up @@ -2698,25 +2690,14 @@ where
let mut reader = Reader::from_reader(reader);
reader.expand_empty_elements(true).check_end_names(true);

let io_reader = IoReader {
reader,
start_trimmer: StartTrimmer::default(),
buf: Vec::new(),
};

Self {
reader: XmlReader::with_resolver(io_reader, entity_resolver),

#[cfg(feature = "overlapped-lists")]
read: VecDeque::new(),
#[cfg(feature = "overlapped-lists")]
write: VecDeque::new(),
#[cfg(feature = "overlapped-lists")]
limit: None,

#[cfg(not(feature = "overlapped-lists"))]
peek: None,
}
Self::new(
IoReader {
reader,
start_trimmer: StartTrimmer::default(),
buf: Vec::new(),
},
entity_resolver,
)
}
}

Expand Down

0 comments on commit 4d8804d

Please sign in to comment.