Struct charset::Charset

source ·
pub struct Charset { /* private fields */ }
Expand description

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!

Implementations

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.