use std::fmt;
use std::io;
use std::path::PathBuf;
use denise::SurfaceError;
use crate::info::FbInfoError;
#[derive(Debug)]
#[non_exhaustive]
pub enum FbdevError {
NoDevice,
Open {
path: PathBuf,
source: io::Error,
},
Sysfs {
path: PathBuf,
source: io::Error,
},
Geometry(FbInfoError),
Map(io::Error),
TooSmall {
required: usize,
actual: usize,
},
}
impl fmt::Display for FbdevError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoDevice => f.write_str("no framebuffer device found"),
Self::Open { path, .. } => write!(f, "opening {}", path.display()),
Self::Sysfs { path, .. } => write!(f, "reading {}", path.display()),
Self::Geometry(err) => core::fmt::Display::fmt(err, f),
Self::Map(_) => f.write_str("mapping the framebuffer"),
Self::TooSmall { required, actual } => write!(
f,
"framebuffer is {actual} bytes but the geometry needs {required}"
),
}
}
}
impl std::error::Error for FbdevError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Open { source, .. } | Self::Sysfs { source, .. } | Self::Map(source) => {
Some(source)
}
Self::NoDevice | Self::Geometry(_) | Self::TooSmall { .. } => None,
}
}
}
impl From<FbInfoError> for FbdevError {
fn from(err: FbInfoError) -> Self {
Self::Geometry(err)
}
}
impl From<FbdevError> for SurfaceError {
fn from(err: FbdevError) -> Self {
SurfaceError::backend(err)
}
}