use std::ffi::CString;
use core::{
ffi::FromBytesWithNulError,
fmt::{Display, self},
error::Error,
};
use crate::{op, ParseError, Literal};
use super::*;
#[derive(Debug)]
pub enum ReflectError {
FromBytesWithNulError(FromBytesWithNulError),
EntryPointNotFound(CString, op::ExecutionModel),
EntryPointNotSet,
Parse(ParseError),
InvalidTypeId(Id),
InvalidConstantId(Id),
NonIntegerLiteral(Literal),
ExpectedConstantLiteral {
found: String
},
ExpectedScalarType {
found: op::Code,
},
ExpectedVectorType {
found: op::Code,
},
MissingRequiredDecoration(&'static str),
InvalidRuntimeArray,
}
impl Display for ReflectError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FromBytesWithNulError(_) => write!(f, "ffi string conversion error"),
Self::EntryPointNotFound(name, model) =>
write!(f, "entry point with name {name:?} and execution model {model} not found"),
Self::EntryPointNotSet => write!(f, "entry point not set"),
Self::Parse(_) => write!(f, "invalid spirv"),
Self::InvalidTypeId(id) => write!(f, "invalid type id {id}"),
Self::InvalidConstantId(id) => write!(f, "invalid constant id {id}"),
Self::NonIntegerLiteral(literal) => write!(f, "non-integer litral {literal:?}"),
Self::ExpectedConstantLiteral { found } => write!(f, "expected literal constant, found {found}"),
Self::ExpectedScalarType { found } => write!(f, "expected scalar type, found {found}"),
Self::ExpectedVectorType { found } => write!(f, "expected vector type, found {found}"),
Self::MissingRequiredDecoration(dec) => write!(f, "missing required decoration {dec}"),
Self::InvalidRuntimeArray
=> write!(f, "invalid runtime array, runtime arrays must be the last member of a struct")
}
}
}
impl From<FromBytesWithNulError> for ReflectError {
#[inline]
fn from(value: FromBytesWithNulError) -> Self {
Self::FromBytesWithNulError(value)
}
}
impl From<ParseError> for ReflectError {
#[inline]
fn from(value: ParseError) -> Self {
Self::Parse(value)
}
}
impl Error for ReflectError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::FromBytesWithNulError(err) => Some(err),
Self::Parse(err) => Some(err),
_ => None,
}
}
}
pub type ReflectResult<T> = Result<T, ReflectError>;