[][src]Struct charset::Charset

pub struct Charset { /* fields omitted */ }

A character encoding suitable for decoding email.

This is either an encoding as defined in the Encoding Standard or UTF-7 as defined in RFC 2152.

Each Charset has one or more labels that are used to identify the Charset in protocol text. In MIME/IANA terminology, these are called names and aliases, but for consistency with the Encoding Standard and the encoding_rs crate, they are called labels in this crate. What this crate calls the name (again, for consistency with the Encoding Standard and the encoding_rs crate) is known as preferred name in MIME/IANA terminology.

Instances of Charset can be compared with ==. Charset is Copy and is meant to be passed by value.

Note: It is wrong to use this for decoding Web content. Use encoding_rs::Encoding instead!

Methods

impl Charset
[src]

Implements the get an encoding algorithm with the label "UTF-7" added to the set of labels recognized. GBK is unified with gb18030, since they decode the same and Charset only supports decoding.

If, after ASCII-lowercasing and removing leading and trailing whitespace, the argument matches a label defined in the Encoding Standard or "utf-7", Some(Charset) representing the corresponding encoding is returned. If there is no match, None is returned.

This is the right method to use if the action upon the method returning None is to use a fallback encoding (e.g. WINDOWS_1252) instead. When the action upon the method returning None is not to proceed with a fallback but to refuse processing, for_label_no_replacement() is more appropriate.

The argument is of type &[u8] instead of &str to save callers that are extracting the label from a non-UTF-8 protocol the trouble of conversion to UTF-8. (If you have a &str, just call .as_bytes() on it.)

This method behaves the same as for_label(), except when for_label() would return Some(Charset::for_encoding(encoding_rs::REPLACEMENT)), this method returns None instead.

This method is useful in scenarios where a fatal error is required upon invalid label, because in those cases the caller typically wishes to treat the labels that map to the replacement encoding as fatal errors, too.

It is not OK to use this method when the action upon the method returning None is to use a fallback encoding (e.g. WINDOWS_1252) with text/html email. In such a case, the for_label() method should be used instead in order to avoid unsafe fallback for labels that for_label() maps to Some(REPLACEMENT). Such fallback might be safe, though not particularly useful for text/plain email, though.

Returns the Charset corresponding to an &'static Encoding.

GBK is unified with GB18030, since those two decode the same and Charset only supports decoding.

Performs non-incremental BOM sniffing.

The argument must either be a buffer representing the entire input stream (non-streaming case) or a buffer representing at least the first three bytes of the input stream (streaming case).

Returns Some((Charset::for_encoding(encoding_rs::UTF_8), 3)), Some((Charset::for_encoding(encoding_rs::UTF_16LE), 2)) or Some((Charset::for_encoding(encoding_rs::UTF_16BE), 2)) if the argument starts with the UTF-8, UTF-16LE or UTF-16BE BOM or None otherwise.

Returns the name of this encoding.

Mostly useful for debugging

Checks whether the bytes 0x00...0x7F map exclusively to the characters U+0000...U+007F and vice versa.

Decode complete input to Cow<'a, str> with BOM sniffing and with malformed sequences replaced with the REPLACEMENT CHARACTER when the entire input is available as a single buffer (i.e. the end of the buffer marks the end of the stream).

This method implements the (non-streaming version of) the decode spec concept.

The second item in the returned tuple is the encoding that was actually used (which may differ from this encoding thanks to BOM sniffing).

The third item in the returned tuple indicates whether there were malformed sequences (that were replaced with the REPLACEMENT CHARACTER).

Note: It is wrong to use this when the input buffer represents only a segment of the input instead of the whole input.

Panics

If the size calculation for a heap-allocated backing buffer overflows usize.

Decode complete input to Cow<'a, str> with BOM removal and with malformed sequences replaced with the REPLACEMENT CHARACTER when the entire input is available as a single buffer (i.e. the end of the buffer marks the end of the stream).

When invoked on UTF_8, this method implements the (non-streaming version of) the UTF-8 decode spec concept.

The second item in the returned pair indicates whether there were malformed sequences (that were replaced with the REPLACEMENT CHARACTER).

Note: It is wrong to use this when the input buffer represents only a segment of the input instead of the whole input.

Panics

If the size calculation for a heap-allocated backing buffer overflows usize.

Decode complete input to Cow<'a, str> without BOM handling and with malformed sequences replaced with the REPLACEMENT CHARACTER when the entire input is available as a single buffer (i.e. the end of the buffer marks the end of the stream).

When invoked on UTF_8, this method implements the (non-streaming version of) the UTF-8 decode without BOM spec concept.

The second item in the returned pair indicates whether there were malformed sequences (that were replaced with the REPLACEMENT CHARACTER).

Note: It is wrong to use this when the input buffer represents only a segment of the input instead of the whole input.

Panics

If the size calculation for a heap-allocated backing buffer overflows usize.

Trait Implementations

impl Clone for Charset
[src]

Performs copy-assignment from source. Read more

impl Copy for Charset
[src]

impl From<&'static Encoding> for Charset
[src]

impl PartialEq<Charset> for Charset
[src]

impl Debug for Charset
[src]

impl Hash for Charset
[src]

Feeds a slice of this type into the given [Hasher]. Read more

Auto Trait Implementations

impl Send for Charset

impl Sync for Charset

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]