use std::fmt;
pub mod display;
pub mod metadata;
pub mod operation;
pub use metadata::ErrorMetadata;
#[derive(Debug)]
pub(super) enum TryFromNumberErrorKind {
OutsideIntegerRange(std::num::TryFromIntError),
U64ToFloatLossyConversion(u64),
I64ToFloatLossyConversion(i64),
F64ToF32LossyConversion(f64),
FloatToIntegerLossyConversion(f64),
NegativeToUnsignedLossyConversion(i64),
}
#[derive(Debug)]
pub struct TryFromNumberError {
pub(super) kind: TryFromNumberErrorKind,
}
impl fmt::Display for TryFromNumberError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use TryFromNumberErrorKind::*;
match self.kind {
OutsideIntegerRange(_) => write!(f, "integer too large"),
FloatToIntegerLossyConversion(v) => write!(
f,
"cannot convert floating point number {v} into an integer"
),
NegativeToUnsignedLossyConversion(v) => write!(
f,
"cannot convert negative integer {v} into an unsigned integer type"
),
U64ToFloatLossyConversion(v) => {
write!(
f,
"cannot convert {v}u64 into a floating point type without precision loss"
)
}
I64ToFloatLossyConversion(v) => {
write!(
f,
"cannot convert {v}i64 into a floating point type without precision loss"
)
}
F64ToF32LossyConversion(v) => {
write!(f, "will not attempt to convert {v}f64 into a f32")
}
}
}
}
impl std::error::Error for TryFromNumberError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use TryFromNumberErrorKind::*;
match &self.kind {
OutsideIntegerRange(err) => Some(err as _),
FloatToIntegerLossyConversion(_)
| NegativeToUnsignedLossyConversion(_)
| U64ToFloatLossyConversion(_)
| I64ToFloatLossyConversion(_)
| F64ToF32LossyConversion(_) => None,
}
}
}
impl From<std::num::TryFromIntError> for TryFromNumberError {
fn from(value: std::num::TryFromIntError) -> Self {
Self {
kind: TryFromNumberErrorKind::OutsideIntegerRange(value),
}
}
}
impl From<TryFromNumberErrorKind> for TryFromNumberError {
fn from(kind: TryFromNumberErrorKind) -> Self {
Self { kind }
}
}