extern crate std;
use std::fmt::{self,Display,Formatter};
use std::error::Error;
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum FromStrError {
SeveralCodePoints,
Empty,
}
use self::FromStrError::*;
impl Error for FromStrError {
fn description(&self) -> &'static str {match *self {
SeveralCodePoints => "has more than one codepoint",
Empty => "is empty",
}}
}
impl Display for FromStrError {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}", self.description())
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidCodePoint {
Utf16Reserved,
TooHigh,
}
use self::InvalidCodePoint::*;
impl InvalidCodePoint {
pub fn error_range(self) -> (u32,u32) {match self {
Utf16Reserved => (0xd8_00, 0xdf_ff),
TooHigh => (0x00_10_ff_ff, 0xff_ff_ff_ff),
}}
}
impl Error for InvalidCodePoint {
fn description(&self) -> &'static str {match *self {
Utf16Reserved => "is reserved for utf-16 surrogate pairs",
TooHigh => "is higher than the highest codepoint of 0x10ffff",
}}
}
impl Display for InvalidCodePoint {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}", self.description())
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidUtf8FirstByte {
TooLongSeqence,
ContinuationByte,
}
use self::InvalidUtf8FirstByte::*;
impl Error for InvalidUtf8FirstByte {
fn description(&self) -> &'static str {match *self {
TooLongSeqence => "is greater than 239 (UTF-8 seqences cannot be longer than four bytes)",
ContinuationByte => "is a continuation of a previous sequence",
}}
}
impl Display for InvalidUtf8FirstByte {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}", self.description())
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidUtf8 {
FirstByte(InvalidUtf8FirstByte),
NotAContinuationByte(usize),
OverLong,
}
use self::InvalidUtf8::*;
impl Error for InvalidUtf8 {
fn description(&self) -> &'static str {match *self {
FirstByte(TooLongSeqence) => "the first byte is greater than 239 (UTF-8 seqences cannot be longer than four bytes)",
FirstByte(ContinuationByte) => "the first byte is a continuation of a previous sequence",
OverLong => "the seqence contains too many zeros and could be shorter",
NotAContinuationByte(_) => "the sequence is too short",
}}
fn cause(&self) -> Option<&Error> {match *self {
FirstByte(ref cause) => Some(cause),
_ => None,
}}
}
impl Display for InvalidUtf8 {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}", self.description())
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidUtf8Array {
Utf8(InvalidUtf8),
CodePoint(InvalidCodePoint),
}
impl Error for InvalidUtf8Array {
fn description(&self) -> &'static str {match *self {
InvalidUtf8Array::Utf8(_) => "the seqence is invalid UTF-8",
InvalidUtf8Array::CodePoint(_) => "the encoded codepoint is invalid",
}}
fn cause(&self) -> Option<&Error> {match *self {
InvalidUtf8Array::Utf8(ref u) => Some(u),
InvalidUtf8Array::CodePoint(ref c) => Some(c),
}}
}
impl Display for InvalidUtf8Array {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}: {}", self.description(), self.cause().unwrap().description())
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidUtf8Slice {
Utf8(InvalidUtf8),
CodePoint(InvalidCodePoint),
TooShort(usize),
}
impl Error for InvalidUtf8Slice {
fn description(&self) -> &'static str {match *self {
InvalidUtf8Slice::Utf8(_) => "the seqence is invalid UTF-8",
InvalidUtf8Slice::CodePoint(_) => "the encoded codepoint is invalid",
InvalidUtf8Slice::TooShort(0) => "the slice is empty",
InvalidUtf8Slice::TooShort(_) => "the slice is shorter than the seqence",
}}
fn cause(&self) -> Option<&Error> {match *self {
InvalidUtf8Slice::Utf8(ref u) => Some(u),
InvalidUtf8Slice::CodePoint(ref c) => Some(c),
InvalidUtf8Slice::TooShort(_) => None,
}}
}
impl Display for InvalidUtf8Slice {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
match self.cause() {
Some(d) => write!(fmtr, "{}: {}", self.description(), d),
None => write!(fmtr, "{}", self.description()),
}
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidUtf16Tuple {
FirstIsTrailingSurrogate,
SuperfluousSecond,
MissingSecond,
InvalidSecond,
}
impl Error for InvalidUtf16Tuple {
fn description(&self) -> &'static str {match *self {
InvalidUtf16Tuple::FirstIsTrailingSurrogate => "the first unit is a trailing / low surrogate, which is never valid",
InvalidUtf16Tuple::SuperfluousSecond => "the second unit is superfluous",
InvalidUtf16Tuple::MissingSecond => "the first unit requires a second unit",
InvalidUtf16Tuple::InvalidSecond => "the required second unit is not a trailing / low surrogate",
}}
}
impl Display for InvalidUtf16Tuple {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}", self.description())
}
}
#[derive(Clone,Copy, Debug, PartialEq,Eq)]
pub enum InvalidUtf16Slice {
EmptySlice,
FirstLowSurrogate,
MissingSecond,
SecondNotLowSurrogate,
}
impl Error for InvalidUtf16Slice {
fn description(&self) -> &'static str {match *self {
InvalidUtf16Slice::EmptySlice => "the slice is empty",
InvalidUtf16Slice::FirstLowSurrogate => "the first unit is a low surrogate",
InvalidUtf16Slice::MissingSecond => "the first and only unit requires a second one",
InvalidUtf16Slice::SecondNotLowSurrogate => "the required second unit is not a low surrogate",
}}
}
impl Display for InvalidUtf16Slice {
fn fmt(&self, fmtr: &mut Formatter) -> fmt::Result {
write!(fmtr, "{}", self.description())
}
}