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
//! The module with the Error type that is uses within the public API `Layouter`.
use std::fmt;
use std::io::Error;

#[derive(Debug)]
pub struct LayouterError {
    pub description: String,
    pub io_error: Option<Error>,
}

impl fmt::Display for LayouterError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(err) = &self.io_error {
            write!(f, "{}", err)
        } else {
            write!(f, "{}", self.description)
        }
    }
}

impl std::error::Error for LayouterError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        if let Some(err) = &self.io_error {
            Some(err)
        } else {
            None
        }
    }
}

impl LayouterError {
    pub fn from_description(description: String) -> Self {
        Self {
            description,
            io_error: None,
        }
    }
    pub fn from_io_error(io_error: Error) -> Self {
        Self {
            description: "IoError".to_string(),
            io_error: Some(io_error),
        }
    }
}

pub type Result<T> = std::result::Result<T, LayouterError>;