use super::Id;
use alloc::boxed::Box;
use core::fmt;
#[cfg(feature = "std")]
pub type Error = dyn std::error::Error;
#[cfg(not(feature = "std"))]
pub type Error = dyn core::any::Any;
#[derive(Debug, Default)]
pub struct ParseGltfError {
kind: ParseGltfErrorKind,
#[allow(unused)]
error: Option<Box<Error>>,
}
impl ParseGltfError {
#[inline]
pub fn new<E: Into<Box<Error>>>(kind: ParseGltfErrorKind, error: E) -> Self {
Self {
kind,
error: Some(error.into()),
}
}
}
impl fmt::Display for ParseGltfError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
ParseGltfErrorKind::InvalidHeader => write!(f, "invalid GLB header"),
ParseGltfErrorKind::UnsupportedVersion => write!(f, "unsupported glTF version"),
ParseGltfErrorKind::InvalidChunkHeader => write!(f, "invalid GLB chunk header"),
ParseGltfErrorKind::InvalidChunk => write!(f, "invalid GLB chunk data"),
ParseGltfErrorKind::MissingJson => write!(f, "missing glTF JSON content"),
_ => write!(f, "invalid GLB"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseGltfError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.error.as_ref()?.as_ref())
}
}
#[derive(Clone, Copy, Debug)]
pub enum ParseGltfErrorKind {
InvalidHeader,
UnsupportedVersion,
InvalidChunkHeader,
InvalidChunk,
InvalidJson,
MissingJson,
Other,
}
impl Default for ParseGltfErrorKind {
fn default() -> Self {
Self::Other
}
}
impl From<ParseGltfErrorKind> for ParseGltfError {
fn from(kind: ParseGltfErrorKind) -> Self {
Self { kind, error: None }
}
}
#[derive(Debug, Default)]
pub struct LoadGltfResourceError {
kind: LoadGltfResourceErrorKind,
#[allow(unused)]
error: Option<Box<Error>>,
}
impl LoadGltfResourceError {
#[inline]
pub fn new<E: Into<Box<Error>>>(kind: LoadGltfResourceErrorKind, error: E) -> Self {
Self {
kind,
error: Some(error.into()),
}
}
}
impl fmt::Display for LoadGltfResourceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
LoadGltfResourceErrorKind::LoadBufferError(id) => write!(f, "failed to load buffer {}", id),
LoadGltfResourceErrorKind::LoadImageError(id) => write!(f, "failed to load image {}", id),
LoadGltfResourceErrorKind::ParseGltfError => write!(f, "failed to parse glTF or GLB file"),
_ => write!(f, "failed to load resource"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for LoadGltfResourceError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.error.as_ref()?.as_ref())
}
}
#[derive(Clone, Copy, Debug)]
pub enum LoadGltfResourceErrorKind {
LoadImageError(Id),
LoadBufferError(Id),
LoadError,
ParseGltfError,
}
impl Default for LoadGltfResourceErrorKind {
fn default() -> Self {
Self::LoadError
}
}
impl From<LoadGltfResourceErrorKind> for LoadGltfResourceError {
fn from(kind: LoadGltfResourceErrorKind) -> Self {
Self { kind, error: None }
}
}