use std::{error::Error,
fmt::{Display, Result as FmtResult},
result::Result as OGResult};
pub type CommonResult<T> = OGResult<T, Box<dyn Error + Send + Sync>>;
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CommonError {
pub err_type: CommonErrorType,
pub err_msg: Option<String>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum CommonErrorType {
ExitLoop,
DisplaySizeTooSmall,
General,
InvalidArguments,
InvalidResult,
InvalidState,
StackOverflow,
StackUnderflow,
ParsingError,
IOError,
ValueOutOfRange,
InvalidValue,
DoesNotApply,
IndexOutOfBounds,
}
impl Default for CommonErrorType {
fn default() -> Self { CommonErrorType::General }
}
impl Error for CommonError {}
impl Display for CommonError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> FmtResult { write!(f, "{self:?}") }
}
impl CommonError {
#[allow(clippy::all)]
pub fn new<T>(err_type: CommonErrorType, msg: &str) -> CommonResult<T> {
Self::from_err(CommonError {
err_type,
err_msg: msg.to_string().into(),
})
}
pub fn new_err_with_only_type<T>(err_type: CommonErrorType) -> CommonResult<T> {
CommonError::from_err_type_and_msg(err_type, None)
}
pub fn new_err_with_only_msg<T>(msg: &str) -> CommonResult<T> {
CommonError::from_err_type_and_msg(CommonErrorType::General, Some(msg.to_string()))
}
fn from_err_type_and_msg<T>(err_type: CommonErrorType, msg: Option<String>) -> CommonResult<T> {
Self::from_err(CommonError {
err_type,
err_msg: msg,
})
}
fn from_err<T>(err: CommonError) -> CommonResult<T> { Err(Box::new(err)) }
}