use crate::stream::{error::StreamError, MSG_EOF, MSG_EOL};
use std::fmt::{self, Debug, Display};
#[derive(Debug)]
pub enum ReadError<E> {
IOError(std::io::Error),
EOF,
EOL,
UnexpectedChar(String, &'static str),
FromStrError(E, String, &'static str),
}
impl<E> Display for ReadError<E>
where
E: std::error::Error,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IOError(error) => Display::fmt(error, f),
Self::EOF => f.write_str(MSG_EOF),
Self::EOL => f.write_str(MSG_EOL),
Self::FromStrError(error, s, name) => {
write!(
f,
"error during converting a string {s:?} to a value of `{name}`: ",
)?;
Display::fmt(error, f)
}
Self::UnexpectedChar(s, t) => write!(f, "found unexpected character at the end of the string {s:?} during converting it to a value of {t:?}"),
}
}
}
impl<E> std::error::Error for ReadError<E> where E: std::error::Error {}
impl<E> From<StreamError> for ReadError<E> {
#[inline]
fn from(error: StreamError) -> Self {
match error {
StreamError::IOError(e) => Self::IOError(e),
StreamError::Eof => Self::EOF,
StreamError::Eol => Self::EOL,
}
}
}