use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io;
use std::path::PathBuf;
use std::result;
use image::ImageError;
use lyon_tessellation::TessellationError;
#[cfg(feature = "audio")]
use rodio::decoder::DecoderError;
pub type Result<T = ()> = result::Result<T, TetraError>;
#[non_exhaustive]
#[derive(Debug)]
pub enum TetraError {
PlatformError(String),
FailedToLoadAsset {
reason: io::Error,
path: PathBuf,
},
InvalidColor,
InvalidTexture(ImageError),
InvalidShader(String),
InvalidFont,
#[cfg(feature = "audio")]
InvalidSound(DecoderError),
NotEnoughData {
expected: usize,
actual: usize,
},
NoAudioDevice,
FailedToChangeDisplayMode(String),
TessellationError(TessellationError),
}
impl Display for TetraError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
TetraError::PlatformError(msg) => {
write!(f, "An error was thrown by the platform: {}", msg)
}
TetraError::FailedToLoadAsset { path, .. } => {
write!(f, "Failed to load asset from {}", path.to_string_lossy())
}
TetraError::InvalidColor => write!(f, "Invalid color"),
TetraError::InvalidTexture(_) => write!(f, "Invalid texture data"),
TetraError::InvalidShader(msg) => write!(f, "Invalid shader source: {}", msg),
TetraError::InvalidFont => write!(f, "Invalid font data"),
#[cfg(feature = "audio")]
TetraError::InvalidSound(_) => write!(f, "Invalid sound data"),
TetraError::NotEnoughData { expected, actual } => write!(
f,
"Not enough data was provided to fill a buffer - expected {}, found {}.",
expected, actual
),
TetraError::FailedToChangeDisplayMode(msg) => {
write!(f, "Failed to change display mode: {}", msg)
}
TetraError::NoAudioDevice => write!(f, "No audio device available for playback"),
TetraError::TessellationError(_) => {
write!(f, "An error occurred while tessellating a shape")
}
}
}
}
impl Error for TetraError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
TetraError::PlatformError(_) => None,
TetraError::FailedToLoadAsset { reason, .. } => Some(reason),
TetraError::InvalidColor => None,
TetraError::InvalidTexture(reason) => Some(reason),
TetraError::InvalidShader(_) => None,
TetraError::InvalidFont => None,
#[cfg(feature = "audio")]
TetraError::InvalidSound(reason) => Some(reason),
TetraError::NotEnoughData { .. } => None,
TetraError::NoAudioDevice => None,
TetraError::FailedToChangeDisplayMode(_) => None,
TetraError::TessellationError(reason) => Some(reason),
}
}
}