Skip to main content

nox_spirv/reflect/
error.rs

1use core::{
2    ffi::FromBytesWithNulError,
3    fmt::{Display, self},
4    error::Error,
5};
6
7use crate::{op, ParseError, Literal};
8use super::*;
9
10/// The main error type for when reflection fails.
11#[derive(Debug)]
12pub enum ReflectError {
13    /// A [`CStr`] error.
14    FromBytesWithNulError(FromBytesWithNulError),
15    /// A parsing error.
16    Parse(ParseError),
17    /// An error indicating no entry point is [`set`][1].
18    ///
19    /// [1]: Reflector::set_entry_point
20    NoEntryPointSet,
21    /// An error indicating that the [`specified`][1] entry point was not found.
22    ///
23    /// [1]: Reflector::set_entry_point
24    UnknownEntryPoint,
25    /// An error indicating that a type with [`Id`] was not found.
26    InvalidTypeId(Id),
27    /// An error indicating that a constant with [`Id`] was not found.
28    InvalidConstantId(Id),
29    /// An error indicating that an integer literal was expected, but a non-integer literal was
30    /// found.
31    NonIntegerLiteral(Literal),
32    /// An error indicating that a constant with a [`literal`][1] was expected, but another
33    /// type of constant was found.
34    ///
35    /// [1]: Literal
36    ExpectedConstantLiteral {
37        /// The constant found.
38        found: String
39    },
40    /// An error indicating that a scalar type instruction was expected, but another instruction
41    /// was found.
42    ExpectedScalarType {
43        /// The [`Code`][1] of the instruction found.
44        ///
45        /// [1]: op::Code
46        found: op::Code,
47    },
48    /// An error indicating that a vector type instruction was expected, but another instruction
49    /// was found.
50    ExpectedVectorType {
51        /// The [`Code`][1] of the instruction found.
52        ///
53        /// [1]: op::Code
54        found: op::Code,
55    },
56    /// An error indicating that a required decoration was expected but not found.
57    MissingRequiredDecoration(&'static str),
58    /// An error indicating that an invalid placement of a runtime array.
59    InvalidRuntimeArray,
60}
61
62impl Display for ReflectError {
63
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            Self::FromBytesWithNulError(_) => write!(f, "ffi string conversion error"),
67            Self::Parse(_) => write!(f, "invalid spirv"),
68            Self::NoEntryPointSet => write!(f, "no entry point set"),
69            Self::UnknownEntryPoint => write!(f, "unknown entry point"),
70            Self::InvalidTypeId(id) => write!(f, "invalid type id {id}"),
71            Self::InvalidConstantId(id) => write!(f, "invalid constant id {id}"),
72            Self::NonIntegerLiteral(literal) => write!(f, "non-integer litral {literal:?}"),
73            Self::ExpectedConstantLiteral { found } => write!(f, "expected literal constant, found {found}"),
74            Self::ExpectedScalarType { found } => write!(f, "expected scalar type, found {found}"),
75            Self::ExpectedVectorType { found } => write!(f, "expected vector type, found {found}"),
76            Self::MissingRequiredDecoration(dec) => write!(f, "missing required decoration {dec}"),
77            Self::InvalidRuntimeArray
78                => write!(f, "invalid runtime array, runtime arrays must be the last member of a struct")
79        }
80    }
81}
82
83impl From<FromBytesWithNulError> for ReflectError {
84
85    #[inline]
86    fn from(value: FromBytesWithNulError) -> Self {
87        Self::FromBytesWithNulError(value)
88    }
89}
90
91impl From<ParseError> for ReflectError {
92    
93    #[inline]
94    fn from(value: ParseError) -> Self {
95        Self::Parse(value)
96    }
97}
98
99impl Error for ReflectError {
100
101    fn source(&self) -> Option<&(dyn Error + 'static)> {
102        match self {
103            Self::FromBytesWithNulError(err) => Some(err),
104            Self::Parse(err) => Some(err),
105            _ => None,
106        }
107    }
108}
109
110/// The [`Result`] of a reflection operation.
111pub type ReflectResult<T> = Result<T, ReflectError>;