Skip to content

Commit 8d5aaaa

Browse files
committed
Fix #262: Decrease code redundancy by merging encoding-related methods
1 parent 15f5075 commit 8d5aaaa

File tree

2 files changed

+9
-34
lines changed

2 files changed

+9
-34
lines changed

src/events/attributes.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,17 @@ impl<'a> Attribute<'a> {
108108
}
109109

110110
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
111-
#[cfg(feature = "encoding")]
112111
fn do_unescape_and_decode_value<B: BufRead>(
113112
&self,
114113
reader: &Reader<B>,
115114
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
116115
) -> XmlResult<String> {
116+
#[cfg(feature = "encoding")]
117117
let decoded = reader.decoder().decode(&*self.value);
118-
let unescaped =
119-
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
120-
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
121-
}
122118

123-
#[cfg(not(feature = "encoding"))]
124-
fn do_unescape_and_decode_value<B: BufRead>(
125-
&self,
126-
reader: &Reader<B>,
127-
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
128-
) -> XmlResult<String> {
119+
#[cfg(not(feature = "encoding"))]
129120
let decoded = reader.decoder().decode(&*self.value)?;
121+
130122
let unescaped =
131123
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
132124
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))

src/events/mod.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -311,27 +311,18 @@ impl<'a> BytesStart<'a> {
311311
self.do_unescape_and_decode_with_custom_entities(reader, Some(custom_entities))
312312
}
313313

314-
#[cfg(feature = "encoding")]
315314
#[inline]
316315
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
317316
&self,
318317
reader: &Reader<B>,
319318
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
320319
) -> Result<String> {
320+
#[cfg(feature = "encoding")]
321321
let decoded = reader.decoder().decode(&*self);
322-
let unescaped =
323-
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
324-
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
325-
}
326322

327-
#[cfg(not(feature = "encoding"))]
328-
#[inline]
329-
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
330-
&self,
331-
reader: &Reader<B>,
332-
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
333-
) -> Result<String> {
323+
#[cfg(not(feature = "encoding"))]
334324
let decoded = reader.decoder().decode(&*self)?;
325+
335326
let unescaped =
336327
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
337328
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
@@ -885,25 +876,17 @@ impl<'a> BytesText<'a> {
885876
self.do_unescape_and_decode_with_custom_entities(reader, Some(custom_entities))
886877
}
887878

888-
#[cfg(feature = "encoding")]
889879
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
890880
&self,
891881
reader: &Reader<B>,
892882
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
893883
) -> Result<String> {
884+
#[cfg(feature = "encoding")]
894885
let decoded = reader.decoder().decode(&*self);
895-
let unescaped =
896-
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
897-
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
898-
}
899886

900-
#[cfg(not(feature = "encoding"))]
901-
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
902-
&self,
903-
reader: &Reader<B>,
904-
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
905-
) -> Result<String> {
887+
#[cfg(not(feature = "encoding"))]
906888
let decoded = reader.decoder().decode(&*self)?;
889+
907890
let unescaped =
908891
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
909892
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))

0 commit comments

Comments
 (0)