1 use crate::std::fmt; 2 use crate::{builder, parser}; 3 4 /// A general error that can occur when working with UUIDs. 5 // TODO: improve the doc 6 // BODY: This detail should be fine for initial merge 7 #[derive(Clone, Debug, Eq, Hash, PartialEq)] 8 pub struct Error(Inner); 9 10 // TODO: write tests for Error 11 // BODY: not immediately blocking, but should be covered for 1.0 12 #[derive(Clone, Debug, Eq, Hash, PartialEq)] 13 enum Inner { 14 /// An error occurred while handling [`Uuid`] bytes. 15 /// 16 /// See [`BytesError`] 17 /// 18 /// [`BytesError`]: struct.BytesError.html 19 /// [`Uuid`]: struct.Uuid.html 20 Build(builder::Error), 21 22 /// An error occurred while parsing a [`Uuid`] string. 23 /// 24 /// See [`parser::ParseError`] 25 /// 26 /// [`parser::ParseError`]: parser/enum.ParseError.html 27 /// [`Uuid`]: struct.Uuid.html 28 Parser(parser::Error), 29 } 30 31 impl From<builder::Error> for Error { from(err: builder::Error) -> Self32 fn from(err: builder::Error) -> Self { 33 Error(Inner::Build(err)) 34 } 35 } 36 37 impl From<parser::Error> for Error { from(err: parser::Error) -> Self38 fn from(err: parser::Error) -> Self { 39 Error(Inner::Parser(err)) 40 } 41 } 42 43 impl fmt::Display for Error { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 45 match self.0 { 46 Inner::Build(ref err) => fmt::Display::fmt(&err, f), 47 Inner::Parser(ref err) => fmt::Display::fmt(&err, f), 48 } 49 } 50 } 51 52 #[cfg(feature = "std")] 53 mod std_support { 54 use super::*; 55 use crate::std::error; 56 57 impl error::Error for Error { source(&self) -> Option<&(dyn error::Error + 'static)>58 fn source(&self) -> Option<&(dyn error::Error + 'static)> { 59 match self.0 { 60 Inner::Build(ref err) => Some(err), 61 Inner::Parser(ref err) => Some(err), 62 } 63 } 64 } 65 } 66 67 #[cfg(test)] 68 mod test_util { 69 use super::*; 70 71 impl Error { expect_parser(self) -> parser::Error72 pub(crate) fn expect_parser(self) -> parser::Error { 73 match self.0 { 74 Inner::Parser(err) => err, 75 _ => panic!("expected a `parser::Error` variant"), 76 } 77 } 78 } 79 } 80