use std::error::Error;
use std::fmt;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum UTF8Validation {
MaximumCodePoint,
TwoByteContinuation,
TwoByteOverlong,
ThreeByteContinuation(u8),
ThreeByteOverlong,
FourByteContinuation(u8),
FourByteOverlong,
InvalidFirstByte(u8),
UTF16Surrogate,
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::UTF16Surrogate => write!(f, "Found a UTF-16 surrogate sequence"),
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::UTF16Surrogate => "Found a UTF-16 surrogate sequence",
UTF8Validation::Unknown => "unknown UTF8Validation error",
}
}
}