use ascii;
use std::fmt;
use std::result;
use crate::tag::TagClass;
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
EmptyTag(TagClass),
NotEnoughTagOctets(TagClass),
UnmatchedTag(TagClass),
LengthEmpty,
NotEnoughLengthOctects,
NoValue,
NoComponent,
SequenceFieldError(String, String, Box<Error>),
SequenceError(String, Box<Error>),
NoDataForLength,
NoAllDataConsumed,
IncorrectValue(String),
Utf8Error,
AsciiError,
ParseIntError,
ImplementationError(String),
ConstraintError(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self, f)
}
}
impl From<std::str::Utf8Error> for Error {
fn from(_inner: std::str::Utf8Error) -> Self {
return Self::Utf8Error;
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(_inner: std::string::FromUtf8Error) -> Self {
return Self::Utf8Error;
}
}
impl From<std::num::ParseIntError> for Error {
fn from(_inner: std::num::ParseIntError) -> Self {
return Self::ParseIntError;
}
}
impl From<ascii::ToAsciiCharError> for Error {
fn from(_inner: ascii::ToAsciiCharError) -> Self {
return Self::AsciiError;
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::tag::TagClass;
#[test]
fn raise_empty_tag_error() {
let error_kind = super::Error::from(Error::EmptyTag(TagClass::Context));
match error_kind {
Error::EmptyTag(tag_class) => assert_eq!(TagClass::Context, tag_class),
_ => unreachable!(),
}
}
}