Function ecoji::decode [] [src]

pub fn decode<R: Read + ?Sized, W: Write + ?Sized>(
    source: &mut R,
    destination: &mut W
) -> Result<usize>

Decodes the entire source from the Ecoji format (assumed to be UTF-8-encoded) and writes the result of the decoding to the provided destination.

If successful, returns the number of bytes which were written to the destination writer.

Returns an error when either source or destination operation has failed, if the number of code points in the input is wrong (it must be a multiple of 4), if the source is not a valid UTF-8 stream or if one of the code points in the source is not a valid character of the Ecoji alphabet. No guarantees are made about the state of the destination if an error occurs, so it is possible for the destination to contain only a part of the decoded data.

Examples

Successful decoding:

let input = "👶😲🇲👅🍉🔙🌥🌩";

let mut output: Vec<u8> = Vec::new();
ecoji::decode(&mut input.as_bytes(), &mut output)?;

assert_eq!(output, b"input data");

Invalid input data, not enough code points:

use std::io;

let input = "👶😲🇲👅🍉🔙🌥";  // one less than needed

let mut output: Vec<u8> = Vec::new();
match ecoji::decode(&mut input.as_bytes(), &mut output) {
    Ok(_) => panic!("Unexpected success"),
    Err(e) => assert_eq!(e.kind(), io::ErrorKind::UnexpectedEof),
}

Invalid input data, not a correct UTF-8 stream:

use std::io;

let input: &[u8] = &[0xfe, 0xfe, 0xff, 0xff];

let mut output: Vec<u8> = Vec::new();
match ecoji::decode(&mut input.clone(), &mut output) {
    Ok(_) => panic!("Unexpected success"),
    Err(e) => assert_eq!(e.kind(), io::ErrorKind::InvalidData),
}

Invalid input data, input code point is not a part of the Ecoji alphabet:

use std::io;

// Padded with spaces for the length to be a multiple of 4
let input = "Not emoji data  ";

let mut output: Vec<u8> = Vec::new();
match ecoji::decode(&mut input.as_bytes(), &mut output) {
    Ok(_) => panic!("Unexpected success"),
    Err(e) => assert_eq!(e.kind(), io::ErrorKind::InvalidData),
}