vatfluid 0.1.0

UTF-8 slice validation, with incomplete results to support byte streams.
Documentation
// Copyright (c) 2016 vatfluid developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! UTF-8 Validation Errors
use std::error::Error;
use std::fmt;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
/// UTF-8 Validation Errors
pub enum UTF8Validation {
    /// The code point is larger thant the maximum allowed (U+10FFFF)
    MaximumCodePoint,
    /// The 2nd-byte in a 2-byte sequence in not a valid continuation sequence.
    TwoByteContinuation,
    /// Found a 2-byte sequence that could be represented as 1-byte.
    TwoByteOverlong,
    /// The xth-byte in a 3-byte sequence is not a valid continuation sequence.
    ThreeByteContinuation(u8),
    /// Found a 3-byte sequence that could be represented as 2 or 1-byte.
    ThreeByteOverlong,
    /// The xth-byte in a 4-byte sequence is not a valid continuation sequence.
    FourByteContinuation(u8),
    /// Found a 4-byte sequence that could be represented as 3, 2 or 1-byte.
    FourByteOverlong,
    /// Found an invalid first byte (0x80-0xbf)
    InvalidFirstByte(u8),
    /// Unknown Error
    Unknown,
}

impl fmt::Display for UTF8Validation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            UTF8Validation::MaximumCodePoint => {
                write!(f,
                       "The code point is larger thant the maximum allowed (U+10FFFF)")
            }
            UTF8Validation::TwoByteContinuation => {
                write!(f,
                       "The 2nd byte in a 2 byte sequence in not a valid continuation sequence.")
            }
            UTF8Validation::TwoByteOverlong => {
                write!(f,
                       "Found a 2 byte sequence that could be represented as 1 byte.")
            }
            UTF8Validation::ThreeByteContinuation(b) => {
                write!(f,
                       "The xth-byte ({}) in a 3 byte sequence is not a valid continuation byte",
                       b)
            }
            UTF8Validation::ThreeByteOverlong => {
                write!(f,
                       "Found a 3 byte sequence that could be represented as 2 or 1 bytes.")
            }
            UTF8Validation::FourByteContinuation(b) => {
                write!(f,
                       "The xth-byte ({}) in a 4 byte sequence is not a valid continuation byte",
                       b)
            }
            UTF8Validation::FourByteOverlong => {
                write!(f,
                       "Found a 4 byte sequence that could be represented as 3, 2 or 1 bytes.")
            }
            UTF8Validation::InvalidFirstByte(b) => write!(f, "Found an invalid first byte ({})", b),
            UTF8Validation::Unknown => write!(f, "unknown UTF8Validation error"),
        }
    }
}

impl Error for UTF8Validation {
    fn description(&self) -> &str {
        match *self {
            UTF8Validation::MaximumCodePoint => {
                "The code point is larger thant the maximum allowed \
            (U+10FFFF)"
            }
            UTF8Validation::TwoByteContinuation => {
                "The 2nd byte in a 2 byte sequence in not a valid \
            continuation sequence"
            }
            UTF8Validation::TwoByteOverlong => {
                "Found a 2 byte sequence that could be represented as \
            1 byte."
            }
            UTF8Validation::ThreeByteContinuation(_) => {
                "The xth-byte in a 3 byte sequence is not a \
            valid continuation byte"
            }
            UTF8Validation::ThreeByteOverlong => {
                "Found a 3 byte sequence that could be represented as \
            2 or 1 bytes."
            }
            UTF8Validation::FourByteContinuation(_) => {
                "The xth-byte in a 4 byte sequence is not a \
            valid continuation byte"
            }
            UTF8Validation::FourByteOverlong => {
                "Found a 4 byte sequence that could be represented as \
            3, 2 or 1 bytes."
            }
            UTF8Validation::InvalidFirstByte(_) => "Found an invalid first byte",
            UTF8Validation::Unknown => "unknown UTF8Validation error",
        }
    }
}