Skip to main content

rotex_vulkan/error/
mod.rs

1use std::fmt::{Display, Formatter};
2
3use ash::vk;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Severity {
7    Info,
8    Warning,
9    Recoverable,
10    Fatal,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum ErrorKind {
15    Vulkan(vk::Result),
16    Unsupported(&'static str),
17    NoCompatibleDevice,
18}
19
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Error {
22    pub kind: ErrorKind,
23    pub severity: Severity,
24}
25
26impl Error {
27    pub fn fatal(kind: ErrorKind) -> Self {
28        Self {
29            kind,
30            severity: Severity::Fatal,
31        }
32    }
33
34    pub fn vk_result_code(&self) -> Option<i32> {
35        match self.kind {
36            ErrorKind::Vulkan(code) => Some(code.as_raw()),
37            _ => None,
38        }
39    }
40}
41
42impl Display for Error {
43    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44        match self.kind {
45            ErrorKind::Vulkan(code) => write!(f, "Vulkan error: {code:?} ({})", code.as_raw()),
46            ErrorKind::Unsupported(message) => write!(f, "Unsupported: {message}"),
47            ErrorKind::NoCompatibleDevice => write!(f, "No compatible Vulkan device found"),
48        }
49    }
50}
51
52impl std::error::Error for Error {}
53
54pub fn vk_error(result: vk::Result) -> Error {
55    Error::fatal(ErrorKind::Vulkan(result))
56}