Skip to main content

Encoding

Enum Encoding 

Source
#[non_exhaustive]
pub enum Encoding {
Show 21 variants Arabic, Celtic, CentralEuropean, Croatian, Cyrillic, Devanagari, Dingbats, Farsi, Gaelic, Greek, Gujarati, Gurmukhi, Hebrew, Icelandic, Inuit, Keyboard, Roman, Romanian, Symbol, Thai, Turkish,
}
Expand description

The encodings. The generator writes this list and the tables together, so the two always agree.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Arabic

§

Celtic

§

CentralEuropean

§

Croatian

§

Cyrillic

§

Devanagari

§

Dingbats

§

Farsi

§

Gaelic

§

Greek

§

Gujarati

§

Gurmukhi

§

Hebrew

§

Icelandic

§

Inuit

§

Keyboard

§

Roman

§

Romanian

§

Symbol

§

Thai

§

Turkish

Implementations§

Source§

impl Encoding

Source

pub fn id(self) -> &'static str

The permanent identifier, for example roman or central-european.

Each encoding has an identifier. Only two have a name in the standard. Refer to Self::whatwg_name.

Source

pub fn apple_name(self) -> &'static str

Apple’s name, for example Mac OS Roman.

Source

pub fn whatwg_name(self) -> Option<&'static str>

The name in the standard, if the standard has one.

The result is Some("macintosh") for Encoding::Roman and Some("x-mac-cyrillic") for Encoding::Cyrillic. For the other 19 encodings the result is None.

Source

pub fn labels(self) -> &'static [&'static str]

The labels in the standard for this encoding.

The list is empty if the standard does not have this encoding.

Source

pub fn is_directional(self) -> bool

Tells you if the table gives a direction to its mappings.

The result is true for Mac OS Arabic, Farsi, and Hebrew. For these three encodings, the direction is also the cause of the condition in Self::encode_is_lossy. But the two conditions are not the same. Mac OS Keyboard has the second condition and not the first.

Source

pub fn encode_is_lossy(self) -> bool

Tells you if two codes give the same text.

If the result is true, one byte can decode to text that encodes to a different byte. The opposite sequence is always correct. To encode text and then to decode it always gives the first text again.

The result is true for Mac OS Arabic, Farsi, Hebrew, and Keyboard. In the first three encodings, the left-to-right form and the right-to-left form of a character have the same code point. Mac OS Keyboard has one such condition at U+2423 OPEN BOX. Apple’s comment for that mapping says “duplicates mapping for 0x61, hence no round-trip”.

Source

pub fn defines_every_byte(self) -> bool

Tells you if all 256 bytes have a mapping.

If the result is true, Self::decode_strict cannot fail. Mac OS Roman is such an encoding. This is the reason that a resource type code of four bytes always decodes.

Source

pub fn from_id(id: &str) -> Option<Self>

Finds the encoding with this Self::id. The text must agree fully.

Source

pub fn from_label(label: &str) -> Option<Self>

Finds the encoding with this label.

This function obeys “get an encoding” in section 4.2 of the standard. It removes the ASCII space characters at the start and at the end. Then it compares the text. A capital letter and a small letter are equivalent.

Only two encodings have labels. For the other 19 encodings the result is None. Use Self::from_id for them.

assert_eq!(Encoding::from_label("  MACINTOSH\n"), Some(Encoding::Roman));
assert_eq!(Encoding::from_label("x-mac-ukrainian"), Some(Encoding::Cyrillic));
assert_eq!(Encoding::from_label("Mac OS Thai"), None);
Source

pub fn decode(self, bytes: &[u8]) -> String

Decodes bytes. A byte with no mapping becomes U+FFFD.

This is the “replacement” error mode in the standard. If Self::defines_every_byte is true, no byte becomes U+FFFD.

Source

pub fn decode_strict(self, bytes: &[u8]) -> Result<String, DecodeError>

Decodes bytes. The first byte with no mapping gives an error.

This is the “fatal” error mode in the standard.

Source

pub fn encode(self, text: &str) -> Result<Vec<u8>, EncodeError>

Encodes text. The first code point with no byte gives an error.

This is the “fatal” error mode in the standard. Use this function for resource data. A type code of four characters must give four bytes. If the encoder replaces a character, you find the wrong resource.

Source

pub fn encode_html(self, text: &str) -> Vec<u8>

Encodes text. A code point with no byte becomes &#NNN;.

This is the “html” error mode in the standard. HTML forms need an encoder that cannot fail. The standard gives a warning about this mode. You cannot see a difference between this result and text that contains the same characters. Use Self::encode for all data that is not form data.

Trait Implementations§

Source§

impl Clone for Encoding

Source§

fn clone(&self) -> Encoding

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Encoding

Source§

impl Debug for Encoding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Encoding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Encoding

Source§

impl Hash for Encoding

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Encoding

Source§

fn cmp(&self, other: &Encoding) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Encoding

Source§

fn eq(&self, other: &Encoding) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Encoding

Source§

fn partial_cmp(&self, other: &Encoding) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Encoding

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.