use std::error;
use std::fmt;
use std::result;
use crate::header::MaxSizeReached;
use crate::{header, method, status};
pub struct Error {
inner: ErrorKind,
}
pub type Result<T> = result::Result<T, Error>;
enum ErrorKind {
StatusCode(status::InvalidStatusCode),
Method(method::InvalidMethod),
Version(rama_net::http::InvalidVersion),
Uri(rama_net::uri::ParseError),
HeaderName(header::InvalidHeaderName),
HeaderValue(header::InvalidHeaderValue),
MaxSizeReached(MaxSizeReached),
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("rama_http_types::Error")
.field(&self.get_ref())
.finish()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.get_ref(), f)
}
}
impl Error {
pub fn is<T: error::Error + 'static>(&self) -> bool {
self.get_ref().is::<T>()
}
pub fn get_ref(&self) -> &(dyn error::Error + 'static) {
match self.inner {
ErrorKind::StatusCode(ref e) => e,
ErrorKind::Method(ref e) => e,
ErrorKind::Version(ref e) => e,
ErrorKind::Uri(ref e) => e,
ErrorKind::HeaderName(ref e) => e,
ErrorKind::HeaderValue(ref e) => e,
ErrorKind::MaxSizeReached(ref e) => e,
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.get_ref().source()
}
}
impl From<status::InvalidStatusCode> for Error {
fn from(err: status::InvalidStatusCode) -> Self {
Self {
inner: ErrorKind::StatusCode(err),
}
}
}
impl From<method::InvalidMethod> for Error {
fn from(err: method::InvalidMethod) -> Self {
Self {
inner: ErrorKind::Method(err),
}
}
}
impl From<rama_net::http::InvalidVersion> for Error {
fn from(err: rama_net::http::InvalidVersion) -> Self {
Self {
inner: ErrorKind::Version(err),
}
}
}
impl From<rama_net::uri::ParseError> for Error {
fn from(err: rama_net::uri::ParseError) -> Self {
Self {
inner: ErrorKind::Uri(err),
}
}
}
impl From<header::InvalidHeaderName> for Error {
fn from(err: header::InvalidHeaderName) -> Self {
Self {
inner: ErrorKind::HeaderName(err),
}
}
}
impl From<header::InvalidHeaderValue> for Error {
fn from(err: header::InvalidHeaderValue) -> Self {
Self {
inner: ErrorKind::HeaderValue(err),
}
}
}
impl From<MaxSizeReached> for Error {
fn from(err: MaxSizeReached) -> Self {
Self {
inner: ErrorKind::MaxSizeReached(err),
}
}
}
impl From<std::convert::Infallible> for Error {
fn from(err: std::convert::Infallible) -> Self {
match err {}
}
}