#[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
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
impl Encoding
Sourcepub fn id(self) -> &'static str
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.
Sourcepub fn apple_name(self) -> &'static str
pub fn apple_name(self) -> &'static str
Apple’s name, for example Mac OS Roman.
Sourcepub fn whatwg_name(self) -> Option<&'static str>
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.
Sourcepub fn labels(self) -> &'static [&'static str]
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.
Sourcepub fn is_directional(self) -> bool
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.
Sourcepub fn encode_is_lossy(self) -> bool
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”.
Sourcepub fn defines_every_byte(self) -> bool
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.
Sourcepub fn from_id(id: &str) -> Option<Self>
pub fn from_id(id: &str) -> Option<Self>
Finds the encoding with this Self::id. The text must agree fully.
Sourcepub fn from_label(label: &str) -> Option<Self>
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);Sourcepub fn decode(self, bytes: &[u8]) -> String
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.
Sourcepub fn decode_strict(self, bytes: &[u8]) -> Result<String, DecodeError>
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.
Sourcepub fn encode(self, text: &str) -> Result<Vec<u8>, EncodeError>
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.
Sourcepub fn encode_html(self, text: &str) -> Vec<u8> ⓘ
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.