summary refs log tree commit diff
path: root/src/error.rs
blob: 304ed74a5580502c1e62086f807adb6f29549a89 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![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)
      }
    }
  }
}

impl From<std::io::Error> for Error {
  fn from(e: std::io::Error) -> Self {
    Error {
      message: format!("System I/O error: {}", e),
    }
  }
}

impl From<png::DecodingError> for Error {
  fn from(e: png::DecodingError) -> Self {
    match e {
      png::DecodingError::IoError(e) => Self::from(e),
      png::DecodingError::Format(_) => Error {
        message: "Texture image is not a valid PNG.".to_string(),
      },
      png::DecodingError::Parameter(e) => Error {
        message: format!("PNG library misusage: {}", e),
      },
      png::DecodingError::LimitsExceeded => Error {
        message: "PNG needed too much memory to decode.".to_string(),
      }
    }
  }
}

impl From<tobj::LoadError> for Error {
  fn from(e: tobj::LoadError) -> Self {
    Error {
      message: format!("While loading model from .obj: {}",
                       match e
      {
        tobj::LoadError::OpenFileFailed => "Can't open file.",
        tobj::LoadError::ReadError => "Can't read from file.",
        tobj::LoadError::UnrecognizedCharacter =>
            "Syntax error: Invalid character.",
        tobj::LoadError::PositionParseError =>
            "Syntax error describing a spatial position.",
        tobj::LoadError::NormalParseError =>
            "Syntax error describing a normal vector.",
        tobj::LoadError::TexcoordParseError =>
            "Syntax error describing texture coordinates.",
        tobj::LoadError::FaceParseError =>
            "Syntax error describing which vertices form a face.",
        tobj::LoadError::MaterialParseError =>
            "Syntax error describing a rendering material.",
        tobj::LoadError::InvalidObjectName =>
            "Syntax error in the name of an object.",
        tobj::LoadError::InvalidPolygon =>
            "Geometric error with an alleged polygon.",
        tobj::LoadError::FaceVertexOutOfBounds =>
            "Geometric error with a vertex's location.",
        tobj::LoadError::FaceTexCoordOutOfBounds =>
            "Geometric error with texture coordinates.",
        tobj::LoadError::FaceNormalOutOfBounds =>
            "Geometric error with a normal vector.",
        tobj::LoadError::FaceColorOutOfBounds =>
            "Geometric error with a vertex color (yes).",
        tobj::LoadError::InvalidLoadOptionConfig =>
            "Invoked incorrectly by Surreality.",
        tobj::LoadError::GenericFailure =>
            "The library's author didn't choose to tell us why.",
      })
    }
  }
}


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 }),
    }
  }
}