summary refs log tree commit diff
path: root/src/error.rs
blob: f03468360fb5370194bd0dd8c8dac4704c950929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#![forbid(unsafe_code)]

#[derive(Debug)]
pub struct Error {
  pub message: String,
}
pub type Result<T> = std::result::Result<T, Error>;

impl std::fmt::Display for Error {
  fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>)
      -> std::result::Result<(), std::fmt::Error>
  {
    fmt.write_str(&self.message)
  }
}

impl From<winit::error::EventLoopError> for Error {
  fn from(e: winit::error::EventLoopError) -> Self {
    match e {
      winit::error::EventLoopError::NotSupported(e) => Self::from(e),
      winit::error::EventLoopError::Os(e) => Self::from(e),
      winit::error::EventLoopError::RecreationAttempt => Error {
        message:
            "There may only ever be a single winit event loop.".to_string()
      },
      winit::error::EventLoopError::ExitFailure(code) => Error {
        message:
            format!("Clean unhappy exit with code {} via winit event loop",
                    code)
      }
    }
  }
}

impl From<winit::error::NotSupportedError> for Error {
  fn from(e: winit::error::NotSupportedError) -> Self {
    Error {
      message:
          format!("The winit backend does not support an operation: {}",
                  e.to_string())
    }
  }
}

impl From<winit::error::OsError> for Error {
  fn from(e: winit::error::OsError) -> Self {
    Error {
      message: format!("The OS told winit about an error: {}", e.to_string())
    }
  }
}

impl From<libloading::Error> for Error {
  fn from(e: libloading::Error) -> Self {
    Error {
      message: format!("The dynamic object loader reported an error: {}",
                       e.to_string())
    }
  }
}

impl From<Box<dyn vulkanalia::loader::LoaderError>> for Error {
  fn from(e: Box<dyn vulkanalia::loader::LoaderError>) -> Self {
    Error {
      message: format!("The Vulkan loader reported an error: {}",
                       e.to_string())
    }
  }
}

impl From<vulkanalia::vk::ErrorCode> for Error {
  fn from(e: vulkanalia::vk::ErrorCode) -> Self {
    Error {
      message: format!("Vulkan gave an error code: {}", e.to_string())
    }
  }
}

impl From<vulkanalia::bytecode::BytecodeError> for Error {
  fn from(e: vulkanalia::bytecode::BytecodeError) -> Self {
    Error {
      message: match e {
        vulkanalia::bytecode::BytecodeError::Alloc =>
            "Unable to allocate buffer for SPIR-V bytecode.".to_string(),
        vulkanalia::bytecode::BytecodeError::Length(length) =>
            format!("Compiled SPIR-V bytecode has length {}, \
                    which means it's corrupt.", length)
      }
    }
  }
}

pub fn ignore_errors(mut body: impl FnMut() -> Result<()>) -> () {
  if let Err(e) = body() {
    eprintln!("Error: {}", e);
  }
}


pub enum Acceptable<T> {
  Accepted(T),
  Rejected(String),
}

impl<T> Acceptable<T> {
  #[allow(unused)]
  pub fn is_accepted(&self) -> bool {
    if let Acceptable::Accepted(_) = self { true } else { false }
  }

  #[allow(unused)]
  pub fn is_rejected(&self) -> bool {
    if let Acceptable::Rejected(_) = self { true } else { false }
  }

  #[allow(unused)]
  pub fn unwrap(self) -> T {
    if let Acceptable::Accepted(result) = self {
      result
    } else {
      panic!("Unwrapped a rejected Acceptable.");
    }
  }

  pub fn require(self) -> Result<T> {
    match self {
      Acceptable::Accepted(value) => Ok(value),
      Acceptable::Rejected(message) => Err(Error { message }),
    }
  }
}