use std::{error::Error, fmt};
use num_derive::FromPrimitive;
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::{constants::ErrorCode, FromReturnCode};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive)]
pub enum FlagRemoveErrorCode {}
impl FromReturnCode for FlagRemoveErrorCode {
type Error = Self;
fn result_from_i8(val: i8) -> Result<(), Self::Error> {
let maybe_result = Self::try_result_from_i8(val);
#[cfg(feature = "unsafe-return-conversion")]
unsafe {
maybe_result.unwrap_unchecked()
}
#[cfg(not(feature = "unsafe-return-conversion"))]
maybe_result.unwrap()
}
fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
match val {
0 => Some(Ok(())),
_ => None,
}
}
}
impl fmt::Display for FlagRemoveErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg: &'static str = "Zero-variant enum";
write!(f, "{}", msg)
}
}
impl Error for FlagRemoveErrorCode {}
#[derive(
Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum SetColorErrorCode {
InvalidArgs = -10,
}
impl FromReturnCode for SetColorErrorCode {
type Error = Self;
fn result_from_i8(val: i8) -> Result<(), Self::Error> {
let maybe_result = Self::try_result_from_i8(val);
#[cfg(feature = "unsafe-return-conversion")]
unsafe {
maybe_result.unwrap_unchecked()
}
#[cfg(not(feature = "unsafe-return-conversion"))]
maybe_result.unwrap()
}
fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
match val {
0 => Some(Ok(())),
-10 => Some(Err(SetColorErrorCode::InvalidArgs)),
_ => None,
}
}
}
impl fmt::Display for SetColorErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg: &'static str = match self {
SetColorErrorCode::InvalidArgs => {
"color or secondarycolor is not a valid color constant"
}
};
write!(f, "{}", msg)
}
}
impl Error for SetColorErrorCode {}
impl From<SetColorErrorCode> for ErrorCode {
fn from(value: SetColorErrorCode) -> Self {
Self::result_from_i8(value as i8).unwrap_err()
}
}
#[derive(
Debug, PartialEq, Eq, Clone, Copy, Hash, FromPrimitive, Deserialize_repr, Serialize_repr,
)]
#[repr(i8)]
pub enum SetPositionErrorCode {
InvalidTarget = -7,
}
impl FromReturnCode for SetPositionErrorCode {
type Error = Self;
fn result_from_i8(val: i8) -> Result<(), Self::Error> {
let maybe_result = Self::try_result_from_i8(val);
#[cfg(feature = "unsafe-return-conversion")]
unsafe {
maybe_result.unwrap_unchecked()
}
#[cfg(not(feature = "unsafe-return-conversion"))]
maybe_result.unwrap()
}
fn try_result_from_i8(val: i8) -> Option<Result<(), Self::Error>> {
match val {
0 => Some(Ok(())),
-7 => Some(Err(SetPositionErrorCode::InvalidTarget)),
_ => None,
}
}
}
impl fmt::Display for SetPositionErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg: &'static str = match self {
SetPositionErrorCode::InvalidTarget => "the target provided is invalid",
};
write!(f, "{}", msg)
}
}
impl Error for SetPositionErrorCode {}
impl From<SetPositionErrorCode> for ErrorCode {
fn from(value: SetPositionErrorCode) -> Self {
Self::result_from_i8(value as i8).unwrap_err()
}
}