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
use reflect;
use std::error::Error as StdError;
use std::fmt::{self, Display};
#[derive(Debug)]
pub struct DummyError(());
impl Display for DummyError {
fn fmt(&self, _: &mut fmt::Formatter) -> Result<(), fmt::Error> { unreachable!() }
}
impl StdError for DummyError {
fn description(&self) -> &str { unreachable!() }
}
#[derive(Debug)]
pub enum Error<'a, Key: 'a> {
NotFound{ key: &'a Key },
Poisoned{ key: &'a Key },
WouldBlock{ key: &'a Key },
MismatchedType{ key: &'a Key, expected: &'static str, found: &'static str },
CreationError{ key: &'a Key, error: Box<StdError> }
}
impl<'a, Key> Display for Error<'a, Key>
where Key: reflect::Key
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let desc = self.description();
match self {
&Error::NotFound{ key }
| &Error::Poisoned{ key }
| &Error::WouldBlock{ key } => {
fmt.write_fmt(format_args!("[{:?}] {}.", key, desc))
}
&Error::MismatchedType{ key, expected, found } => {
fmt.write_fmt(format_args!("[{:?}] {}: Expected '{}' found '{}'.", key, desc, expected, found))
}
&Error::CreationError{ key, ref error } => {
fmt.write_fmt(format_args!("[{:?}] {}: {}.", key, desc, error))
}
}
}
}
impl<'a, Key> StdError for Error<'a, Key>
where Key: reflect::Key
{
fn description(&self) -> &str {
match self {
&Error::NotFound{ .. } => "Service could not be found",
&Error::Poisoned{ .. } => "Service could not be aquired, mutex was poisoned",
&Error::WouldBlock{ .. } => "Service could not be aquired, mutex would block",
&Error::MismatchedType{ .. } => "Service is of wrong type",
&Error::CreationError{ .. } => "Factory failed to create object",
}
}
}