Skip to content

Commit

Permalink
Merge Decoder methods to avoid wrong remark about necessarily of `enc…
Browse files Browse the repository at this point in the history
…oding` feature for them in the doc

This also allow to show doc for both variants of the methods
  • Loading branch information
Mingun committed Aug 26, 2022
1 parent 6e2b2f5 commit 265ff08
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,47 @@ impl Decoder {
}
}

#[cfg(not(feature = "encoding"))]
impl Decoder {
/// Decodes a UTF8 slice regardless of XML declaration and ignoring BOM if
/// it is present in the `bytes`.
///
/// Returns an error in case of malformed sequences in the `bytes`.
///
/// If you instead want to use XML declared encoding, use the `encoding` feature
#[inline]
pub fn decode<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
Ok(Cow::Borrowed(std::str::from_utf8(bytes)?))
}

/// Decodes a slice regardless of XML declaration with BOM removal if
/// it is present in the `bytes`.
///
/// Returns an error in case of malformed sequences in the `bytes`.
///
/// If you instead want to use XML declared encoding, use the `encoding` feature
pub fn decode_with_bom_removal<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
let bytes = if bytes.starts_with(UTF8_BOM) {
&bytes[3..]
} else {
bytes
};
self.decode(bytes)
}
}

#[cfg(feature = "encoding")]
impl Decoder {
/// Returns the `Reader`s encoding.
///
/// This encoding will be used by [`decode`].
///
/// [`decode`]: Self::decode
#[cfg(feature = "encoding")]
pub fn encoding(&self) -> &'static Encoding {
self.encoding
}

/// ## Without `encoding` feature
///
/// Decodes an UTF-8 slice regardless of XML declaration and ignoring BOM
/// if it is present in the `bytes`.
///
/// ## With `encoding` feature
///
/// Decodes specified bytes using encoding, declared in the XML, if it was
/// declared there, or UTF-8 otherwise, and ignoring BOM if it is present
/// in the `bytes`.
///
/// ----
/// Returns an error in case of malformed sequences in the `bytes`.
pub fn decode<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
decode(bytes, self.encoding)
#[cfg(not(feature = "encoding"))]
let decoded = Ok(Cow::Borrowed(std::str::from_utf8(bytes)?));

#[cfg(feature = "encoding")]
let decoded = decode(bytes, self.encoding);

decoded
}

/// ## Without `encoding` feature
///
/// Decodes an UTF-8 a slice regardless of XML declaration with BOM removal
/// if it is present in the `bytes`.
///
/// ## With `encoding` feature
///
/// Decodes a slice with BOM removal if it is present in the `bytes` using
/// the reader encoding.
///
Expand All @@ -111,9 +103,20 @@ impl Decoder {
///
/// If XML declaration is absent in the XML, UTF-8 is used.
///
/// ----
/// Returns an error in case of malformed sequences in the `bytes`.
pub fn decode_with_bom_removal<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
self.decode(remove_bom(bytes, self.encoding))
#[cfg(not(feature = "encoding"))]
let without_bom = if bytes.starts_with(UTF8_BOM) {
&bytes[3..]
} else {
bytes
};

#[cfg(feature = "encoding")]
let without_bom = remove_bom(bytes, self.encoding);

self.decode(without_bom)
}
}

Expand Down

0 comments on commit 265ff08

Please sign in to comment.