Skip to main content

ferroui_base/
render_target_corrupted_exception.rs

1use std::error::Error;
2use std::fmt;
3use std::rc::Rc;
4
5/// The error a render target reports when it can no longer be drawn to and has to be created anew.
6#[derive(Clone, Debug)]
7pub struct RenderTargetCorruptedException {
8    message: Option<String>,
9    inner_exception: Option<Rc<dyn Error>>,
10}
11
12impl RenderTargetCorruptedException {
13    /// Creates the exception without a message.
14    pub fn new() -> Self {
15        Self { message: None, inner_exception: None }
16    }
17
18    /// Creates the exception with the given message.
19    pub fn new_with_message(message: impl Into<String>) -> Self {
20        Self { message: Some(message.into()), inner_exception: None }
21    }
22
23    /// Creates the exception for the error that caused it.
24    pub fn new_with_inner_exception(inner_exception: Rc<dyn Error>) -> Self {
25        Self { message: None, inner_exception: Some(inner_exception) }
26    }
27
28    /// Creates the exception with the given message, for the error that
29    /// caused it.
30    pub fn new_with_message_and_inner_exception(message: impl Into<String>, inner_exception: Rc<dyn Error>) -> Self {
31        Self { message: Some(message.into()), inner_exception: Some(inner_exception) }
32    }
33
34    /// The message that describes the error: the given one, or the default
35    /// message of the exception.
36    pub fn message(&self) -> &str {
37        self.message.as_deref().unwrap_or("Exception of type 'FerroUI.RenderTargetCorruptedException' was thrown.")
38    }
39
40    /// The error that caused the exception, if any.
41    pub fn inner_exception(&self) -> Option<&Rc<dyn Error>> {
42        self.inner_exception.as_ref()
43    }
44}
45
46impl Default for RenderTargetCorruptedException {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl fmt::Display for RenderTargetCorruptedException {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.write_str(self.message())
55    }
56}
57
58impl Error for RenderTargetCorruptedException {
59    fn source(&self) -> Option<&(dyn Error + 'static)> {
60        self.inner_exception.as_deref()
61    }
62}