You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
3.0 KiB
160 lines
3.0 KiB
use std::{
|
|
error,
|
|
fmt,
|
|
io,
|
|
path::PathBuf,
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
pub enum DecodeError
|
|
{
|
|
/// Map failed
|
|
Map(io::Error, PathBuf),
|
|
/// Map contained invalid UTF-8
|
|
Corrupt,
|
|
/// Map contained invalid base64
|
|
Base64,
|
|
/// Couldn't find base64 bounds
|
|
Bounds,
|
|
/// Bad size
|
|
Size,
|
|
}
|
|
|
|
impl error::Error for DecodeError
|
|
{
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
|
{
|
|
Some(match &self {
|
|
Self::Map(io, _) => io,
|
|
_ => return None,
|
|
})
|
|
}
|
|
}
|
|
impl fmt::Display for DecodeError
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
{
|
|
match self {
|
|
Self::Map(io, path) => write!(f, "mmap (file {:?}) failed: {}", path, io),
|
|
Self::Corrupt => write!(f, "data was corrupt (invalid utf-8)"),
|
|
Self::Base64 => write!(f, "data was corrupt (invalid base64)"),
|
|
Self::Bounds => write!(f, "couldn't find base64 bounds"),
|
|
Self::Size => write!(f, "bad size"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum Error
|
|
{
|
|
/// Image format could not be determined.
|
|
UnknownFormat,
|
|
/// Image is not valid
|
|
InvalidFormat,
|
|
/// Image decode failed
|
|
DecodeError(DecodeError),
|
|
|
|
// Internals
|
|
/// IO error
|
|
IO(io::Error),
|
|
/// Failed to format text
|
|
Formatter(fmt::Error),
|
|
/// Something bad
|
|
Unknown,
|
|
}
|
|
|
|
impl error::Error for Error
|
|
{
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
|
{
|
|
Some(match &self {
|
|
Self::IO(io) => io,
|
|
Self::Formatter(fmt) => fmt,
|
|
Self::DecodeError(de) => de,
|
|
_ => return None,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Error
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
{
|
|
write!(f, "loli module: ")?;
|
|
match self {
|
|
Self::UnknownFormat => write!(f, "could not determine image format"),
|
|
Self::InvalidFormat => write!(f, "image is not valid"),
|
|
Self::DecodeError(m) => write!(f, "image decode failed: {}", m),
|
|
Self::IO(io) => write!(f, "io: {}", io),
|
|
Self::Formatter(fmt) => write!(f, "formatting: {}", fmt),
|
|
_ => write!(f, "unknown error"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<io::Error> for Error
|
|
{
|
|
fn from(er: io::Error) -> Self
|
|
{
|
|
Self::IO(er)
|
|
}
|
|
}
|
|
|
|
impl From<fmt::Error> for Error
|
|
{
|
|
fn from(er: fmt::Error) -> Self
|
|
{
|
|
Self::Formatter(er)
|
|
}
|
|
}
|
|
|
|
impl From<base64::DecodeError> for Error
|
|
{
|
|
fn from(_er: base64::DecodeError) -> Self
|
|
{
|
|
Self::DecodeError(DecodeError::Base64)
|
|
}
|
|
}
|
|
|
|
impl From<std::str::Utf8Error> for Error
|
|
{
|
|
fn from(_er: std::str::Utf8Error) -> Self
|
|
{
|
|
Self::DecodeError(DecodeError::Corrupt)
|
|
}
|
|
}
|
|
|
|
|
|
impl From<std::num::TryFromIntError> for Error
|
|
{
|
|
fn from(_er: std::num::TryFromIntError) -> Self
|
|
{
|
|
Self::DecodeError(DecodeError::Size)
|
|
}
|
|
}
|
|
|
|
|
|
impl From<DecodeError> for Error
|
|
{
|
|
fn from(de: DecodeError) -> Self
|
|
{
|
|
Self::DecodeError(de)
|
|
}
|
|
}
|
|
|
|
impl From<std::str::Utf8Error> for DecodeError
|
|
{
|
|
fn from(_er: std::str::Utf8Error) -> Self
|
|
{
|
|
DecodeError::Corrupt
|
|
}
|
|
}
|
|
|
|
impl From<std::num::TryFromIntError> for DecodeError
|
|
{
|
|
fn from(_er: std::num::TryFromIntError) -> Self
|
|
{
|
|
Self::Size
|
|
}
|
|
}
|