httplz/src/common.rs
2024-10-16 23:40:53 -04:00

39 lines
882 B
Rust

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum ErrorKind {
NeedMoreData,
InvalidConnectionState,
Parse,
TrailingBytes,
BufNotBigEnough,
InvalidEventForConnectionState,
BodySizeMismatch,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Error {
pub kind: ErrorKind,
pub details: &'static str,
}
impl Error {
pub fn new(kind: ErrorKind) -> Self {
Self::with_details(kind, "")
}
pub fn with_details(kind: ErrorKind, details: &'static str) -> Self {
Error { kind, details }
}
}
impl From<ErrorKind> for Error {
fn from(value: ErrorKind) -> Self {
Self::new(value)
}
}
pub fn fail<T>(error_kind: ErrorKind) -> Result<T, Error> {
fail_details(error_kind, "")
}
pub fn fail_details<T>(error_kind: ErrorKind, details: &'static str) -> Result<T, Error> {
Err(Error::with_details(error_kind, details))
}