You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.8 KiB

  1. use std::io::Error as IoError;
  2. use std::str::Utf8Error;
  3. use imagefmt::Error as ImageFmtError;
  4. use thiserror::Error;
  5. #[derive(Debug, Error)]
  6. pub enum Error {
  7. #[error("IO Error: {0}")]
  8. IoError(IoError),
  9. #[error("UTF-8 Error: {0}")]
  10. Utf8Error(Utf8Error),
  11. #[error("Image Format Error: {0}")]
  12. ImageFmtError(ImageFmtError),
  13. #[error("OpenGL Error: {0}")]
  14. GlError(gl::GLenum),
  15. #[error("Error while compiling shader object\n{code:}\n{error:}")]
  16. ShaderCompile { code: String, error: String },
  17. #[error("Error while linking shader program: {0}")]
  18. ShaderLink(String),
  19. #[error("Vertex Array: Index is already in use: {0}!")]
  20. VertexArrayIndexAlreadyInUse(gl::GLuint),
  21. #[error("Vertex Array: Expected pointer!")]
  22. VertexArrayExpectedPointer,
  23. #[error("Invalid Parameter!")]
  24. InvalidParameter,
  25. #[error("Texture Buffer is to small!")]
  26. TextureBufferOverflow,
  27. #[error("Texture Unsupported Format!")]
  28. TextureUnsupportedFormat,
  29. }
  30. impl Error {
  31. pub fn err_if<T>(err: &T, value: T) -> Result<T, Error>
  32. where
  33. T: PartialEq<T>,
  34. {
  35. if value.eq(err) {
  36. Err(Error::GlError(gl::get_error()))
  37. } else {
  38. Ok(value)
  39. }
  40. }
  41. pub fn checked<F, T>(f: F) -> Result<T, Self>
  42. where
  43. F: FnOnce() -> T,
  44. {
  45. gl::get_error();
  46. let ret = f();
  47. match gl::get_error() {
  48. 0 => Ok(ret),
  49. err => Err(Self::GlError(err)),
  50. }
  51. }
  52. }
  53. impl From<IoError> for Error {
  54. fn from(err: IoError) -> Self {
  55. Self::IoError(err)
  56. }
  57. }
  58. impl From<Utf8Error> for Error {
  59. fn from(err: Utf8Error) -> Self {
  60. Self::Utf8Error(err)
  61. }
  62. }
  63. impl From<ImageFmtError> for Error {
  64. fn from(err: ImageFmtError) -> Self {
  65. Self::ImageFmtError(err)
  66. }
  67. }