Trait Encoding

Source
pub trait Encoding {
    // Required methods
    fn name(&self) -> &'static str;
    fn raw_encoder(&self) -> Box<dyn RawEncoder>;
    fn raw_decoder(&self) -> Box<dyn RawDecoder>;

    // Provided methods
    fn whatwg_name(&self) -> Option<&'static str> { ... }
    fn encode(
        &self,
        input: &str,
        trap: EncoderTrap,
    ) -> Result<Vec<u8>, Cow<'static, str>> { ... }
    fn encode_to(
        &self,
        input: &str,
        trap: EncoderTrap,
        ret: &mut dyn ByteWriter,
    ) -> Result<(), Cow<'static, str>> { ... }
    fn decode(
        &self,
        input: &[u8],
        trap: DecoderTrap,
    ) -> Result<String, Cow<'static, str>> { ... }
    fn decode_to(
        &self,
        input: &[u8],
        trap: DecoderTrap,
        ret: &mut dyn StringWriter,
    ) -> Result<(), Cow<'static, str>> { ... }
}
Expand description

Character encoding.

Required Methods§

Source

fn name(&self) -> &'static str

Returns the canonical name of given encoding. This name is guaranteed to be unique across built-in encodings, but it is not normative and would be at most arbitrary.

Source

fn raw_encoder(&self) -> Box<dyn RawEncoder>

Creates a new encoder.

Source

fn raw_decoder(&self) -> Box<dyn RawDecoder>

Creates a new decoder.

Provided Methods§

Source

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

Returns a name of given encoding defined in the WHATWG Encoding standard, if any. This name often differs from name due to the compatibility reason.

Source

fn encode( &self, input: &str, trap: EncoderTrap, ) -> Result<Vec<u8>, Cow<'static, str>>

An easy-to-use interface to RawEncoder. On the encoder error trap is called, which may return a replacement sequence to continue processing, or a failure to return the error.

Source

fn encode_to( &self, input: &str, trap: EncoderTrap, ret: &mut dyn ByteWriter, ) -> Result<(), Cow<'static, str>>

Encode into a dyn ByteWriter.

Source

fn decode( &self, input: &[u8], trap: DecoderTrap, ) -> Result<String, Cow<'static, str>>

An easy-to-use interface to RawDecoder. On the decoder error trap is called, which may return a replacement string to continue processing, or a failure to return the error.

Examples found in repository?
examples/rot13.rs (line 135)
127fn main() {
128    use std::io;
129    use std::io::Read;
130
131    let mut ret = Vec::new();
132    io::stdin()
133        .read_to_end(&mut ret)
134        .expect("cannot read from the input");
135    match ROT13Encoding.decode(&ret, DecoderTrap::Strict) {
136        Ok(s) => println!("{}", s),
137        Err(e) => panic!("decoder error: {}", e),
138    };
139}
Source

fn decode_to( &self, input: &[u8], trap: DecoderTrap, ret: &mut dyn StringWriter, ) -> Result<(), Cow<'static, str>>

Decode into a StringWriter.

This does not handle partial characters at the beginning or end of input! Use RawDecoder for incremental decoding.

Trait Implementations§

Source§

impl<'a> Debug for &'a dyn Encoding

Source§

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

Formats the value using the given formatter. Read more

Implementors§