Skip to main content

nox_spirv/reflect/
error.rs

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