use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
InvalidArgument(&'static str),
#[non_exhaustive]
BufferTooSmall {
needed: usize,
provided: usize,
},
InvalidPacket(&'static str),
InvalidStream(&'static str),
Io(std::io::Error),
Internal(&'static str),
}
impl Error {
pub(crate) fn buffer_too_small(needed: usize, provided: usize) -> Self {
Error::BufferTooSmall { needed, provided }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidArgument(what) => write!(f, "invalid argument: {what}"),
Error::BufferTooSmall { needed, provided } => {
write!(f, "buffer too small: need {needed}, got {provided}")
}
Error::InvalidPacket(what) => write!(f, "invalid packet: {what}"),
Error::InvalidStream(what) => write!(f, "invalid ogg stream: {what}"),
Error::Io(e) => write!(f, "io error: {e}"),
Error::Internal(what) => write!(f, "internal codec error: {what}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;