summary refs log tree commit diff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..39d52b8
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,84 @@
+#![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())
+    }
+  }
+}
+
+pub fn ignore_errors(mut body: impl FnMut() -> Result<()>) -> () {
+  if let Err(e) = body() {
+    eprintln!("Error: {}", e);
+  }
+}
+