use std::{borrow::Cow, fmt::{Debug, Display}, io, num::ParseIntError, path::StripPrefixError, string::FromUtf8Error};
pub struct ServerError(Cow<'static,str>);
impl ServerError {
#[inline]
pub fn from_str(msg: &'static str) -> Self {
Self(msg.into())
}
#[inline]
pub fn from_string(msg: String) -> Self {
Self(msg.into())
}
#[inline]
pub fn err<T>(self) -> Result<T,Self> {
Err(self)
}
#[inline]
pub fn get_message(&self) -> &str {
&self.0
}
}
macro_rules! err {
($lit:literal) => {
crate::ServerError::from_str($lit).err()
};
($e:expr) => {
crate::ServerError::from_string($e).err()
};
($($e:tt)*) => {
crate::ServerError::from_string(format!($($e)*)).err()
};
}
pub (crate) use err;
impl From<io::Error> for ServerError {
#[inline]
fn from(value: io::Error) -> Self {
Self::from_string(value.to_string())
}
}
impl From<FromUtf8Error> for ServerError {
#[inline]
fn from(value: FromUtf8Error) -> Self {
Self::from_string(value.to_string())
}
}
impl From<std::path::StripPrefixError> for ServerError {
#[inline]
fn from(value: StripPrefixError) -> Self {
Self::from_string(value.to_string())
}
}
impl From<ParseIntError> for ServerError {
#[inline]
fn from(value: ParseIntError) -> Self {
Self::from_string(value.to_string())
}
}
impl From<Cow<'static,str>> for ServerError {
#[inline]
fn from(value: Cow<'static,str>) -> Self {
Self(value)
}
}
impl Debug for ServerError {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.get_message())
}
}
impl Display for ServerError {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.get_message())
}
}
impl From<&'static str> for ServerError {
fn from(value: &'static str) -> Self {
Self(value.into())
}
}
impl From<String> for ServerError {
fn from(value: String) -> Self {
Self(value.into())
}
}
impl std::error::Error for ServerError { }