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.

109 line
2.4 KiB

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