use crate::corety::AzString;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct InvalidValueErr<'a>(pub &'a str);
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct InvalidValueErrOwned {
pub value: AzString,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C)]
pub enum ParseFloatError {
Empty,
Invalid,
}
impl ParseFloatError {
fn from_std(e: &core::num::ParseFloatError) -> Self {
let empty_err = "".parse::<f32>().unwrap_err();
if *e == empty_err {
ParseFloatError::Empty
} else {
ParseFloatError::Invalid
}
}
pub fn to_std(&self) -> core::num::ParseFloatError {
match self {
ParseFloatError::Empty => "".parse::<f32>().unwrap_err(),
ParseFloatError::Invalid => "x".parse::<f32>().unwrap_err(),
}
}
}
impl core::fmt::Display for ParseFloatError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ParseFloatError::Empty => write!(f, "cannot parse float from empty string"),
ParseFloatError::Invalid => write!(f, "invalid float literal"),
}
}
}
impl From<core::num::ParseFloatError> for ParseFloatError {
fn from(e: core::num::ParseFloatError) -> Self {
ParseFloatError::from_std(&e)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(C)]
pub enum ParseIntError {
Empty,
InvalidDigit,
PosOverflow,
NegOverflow,
Zero,
}
impl ParseIntError {
fn from_std(e: &core::num::ParseIntError) -> Self {
use core::num::IntErrorKind;
match e.kind() {
IntErrorKind::Empty => ParseIntError::Empty,
IntErrorKind::InvalidDigit => ParseIntError::InvalidDigit,
IntErrorKind::PosOverflow => ParseIntError::PosOverflow,
IntErrorKind::NegOverflow => ParseIntError::NegOverflow,
IntErrorKind::Zero => ParseIntError::Zero,
_ => ParseIntError::InvalidDigit, }
}
pub fn to_std(&self) -> core::num::ParseIntError {
match self {
ParseIntError::Empty => "".parse::<i32>().unwrap_err(),
ParseIntError::InvalidDigit => "x".parse::<i32>().unwrap_err(),
ParseIntError::PosOverflow => "99999999999999999999".parse::<i32>().unwrap_err(),
ParseIntError::NegOverflow => "-99999999999999999999".parse::<i32>().unwrap_err(),
ParseIntError::Zero => {
"x".parse::<i32>().unwrap_err()
}
}
}
}
impl core::fmt::Display for ParseIntError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ParseIntError::Empty => write!(f, "cannot parse integer from empty string"),
ParseIntError::InvalidDigit => write!(f, "invalid digit found in string"),
ParseIntError::PosOverflow => write!(f, "number too large to fit in target type"),
ParseIntError::NegOverflow => write!(f, "number too small to fit in target type"),
ParseIntError::Zero => write!(f, "number would be zero for non-zero type"),
}
}
}
impl From<core::num::ParseIntError> for ParseIntError {
fn from(e: core::num::ParseIntError) -> Self {
ParseIntError::from_std(&e)
}
}
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct ParseFloatErrorWithInput {
pub error: ParseFloatError,
pub input: AzString,
}
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct WrongComponentCountError {
pub expected: usize,
pub got: usize,
pub input: AzString,
}
impl<'a> InvalidValueErr<'a> {
pub fn to_contained(&self) -> InvalidValueErrOwned {
InvalidValueErrOwned { value: self.0.to_string().into() }
}
}
impl InvalidValueErrOwned {
pub fn to_shared<'a>(&'a self) -> InvalidValueErr<'a> {
InvalidValueErr(self.value.as_str())
}
}