wtrenderlib 0.1.0

Dependency-free terminal rendering primitives for RGB and RGBA frames
Documentation
use core::fmt;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    EmptyFrame,
    InvalidStride {
        stride: usize,
        minimum: usize,
    },
    BufferTooSmall {
        actual: usize,
        required: usize,
    },
    DimensionOverflow,
    InvalidCellSize,
    InvalidTargetSize,
    Unsupported(String),
    Io {
        kind: std::io::ErrorKind,
        message: String,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyFrame => write!(f, "frame dimensions must be non-zero"),
            Self::InvalidStride { stride, minimum } => {
                write!(f, "stride {stride} is smaller than row size {minimum}")
            }
            Self::BufferTooSmall { actual, required } => {
                write!(
                    f,
                    "frame buffer has {actual} bytes, but {required} are required"
                )
            }
            Self::DimensionOverflow => write!(f, "frame dimensions overflow usize"),
            Self::InvalidCellSize => write!(f, "cell dimensions must be non-zero"),
            Self::InvalidTargetSize => write!(f, "target dimensions must be non-zero"),
            Self::Unsupported(message) => write!(f, "unsupported: {message}"),
            Self::Io { kind, message } => write!(f, "{kind}: {message}"),
        }
    }
}

impl std::error::Error for Error {}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::Io {
            kind: value.kind(),
            message: value.to_string(),
        }
    }
}

pub type Result<T> = core::result::Result<T, Error>;