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
use std::error::Error;
use std::fmt;

pub trait Builder {
    type product;

    fn new() -> Self;
    fn build(self) -> Result<Self::product, BuilderError>;
}

#[derive(Debug)]
pub struct BuilderError {
    error_message: &'static str,
}

impl BuilderError {
    pub fn new(error_message: &'static str) -> BuilderError {
        BuilderError { error_message }
    }
}

impl fmt::Display for BuilderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "BUILD ERROR: {}", self.error_message)
    }
}

impl Error for BuilderError {
    fn description(&self) -> &str {
        "An error building an object occured."
    }

    // add side stuff in future
    fn cause(&self) -> Option<&dyn Error> {
        None
    }
}