use core::{
fmt::{self, Debug, Display, Formatter},
num::ParseIntError as StdParseIntError,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseIntError {
pub(crate) kind: ParseIntErrorKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ParseIntErrorKind {
Overflow,
Underflow,
Unknown(StdParseIntError),
}
impl From<StdParseIntError> for ParseIntError {
fn from(e: StdParseIntError) -> Self {
ParseIntError { kind: ParseIntErrorKind::Unknown(e) }
}
}
impl Display for ParseIntError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match &self.kind {
ParseIntErrorKind::Overflow => f.write_str("number too large to fit in target type"),
ParseIntErrorKind::Underflow => f.write_str("number too small to fit in target type"),
ParseIntErrorKind::Unknown(e) => write!(f, "{}", e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseIntError {}
#[derive(Clone)]
pub struct TryFromIntError {
_private: (),
}
impl TryFromIntError {
pub(crate) fn new() -> Self {
Self { _private: () }
}
}
impl Display for TryFromIntError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("out of range integral type conversion attempted")
}
}
impl Debug for TryFromIntError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("TryFromIntError")
}
}
#[cfg(feature = "std")]
impl std::error::Error for TryFromIntError {}