logo
 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
use crate::std::fmt;
use crate::{builder, parser};

/// A general error that can occur when working with UUIDs.
// TODO: improve the doc
// BODY: This detail should be fine for initial merge
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Error(Inner);

// TODO: write tests for Error
// BODY: not immediately blocking, but should be covered for 1.0
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum Inner {
    /// An error occurred while handling [`Uuid`] bytes.
    ///
    /// See [`BytesError`]
    ///
    /// [`BytesError`]: struct.BytesError.html
    /// [`Uuid`]: struct.Uuid.html
    Build(builder::Error),

    /// An error occurred while parsing a [`Uuid`] string.
    ///
    /// See [`parser::ParseError`]
    ///
    /// [`parser::ParseError`]: parser/enum.ParseError.html
    /// [`Uuid`]: struct.Uuid.html
    Parser(parser::Error),
}

impl From<builder::Error> for Error {
    fn from(err: builder::Error) -> Self {
        Error(Inner::Build(err))
    }
}

impl From<parser::Error> for Error {
    fn from(err: parser::Error) -> Self {
        Error(Inner::Parser(err))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Inner::Build(ref err) => fmt::Display::fmt(&err, f),
            Inner::Parser(ref err) => fmt::Display::fmt(&err, f),
        }
    }
}

#[cfg(feature = "std")]
mod std_support {
    use super::*;
    use crate::std::error;

    impl error::Error for Error {
        fn source(&self) -> Option<&(dyn error::Error + 'static)> {
            match self.0 {
                Inner::Build(ref err) => Some(err),
                Inner::Parser(ref err) => Some(err),
            }
        }
    }
}

#[cfg(test)]
mod test_util {
    use super::*;

    impl Error {
        pub(crate) fn expect_parser(self) -> parser::Error {
            match self.0 {
                Inner::Parser(err) => err,
                _ => panic!("expected a `parser::Error` variant"),
            }
        }
    }
}