1 use crate::std::fmt;
2 
3 /// An error that can occur while parsing a [`Uuid`] string.
4 ///
5 /// [`Uuid`]: ../struct.Uuid.html
6 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7 #[allow(clippy::enum_variant_names)]
8 pub(crate) enum Error {
9     /// Invalid character in the [`Uuid`] string.
10     ///
11     /// [`Uuid`]: ../struct.Uuid.html
12     InvalidCharacter {
13         /// The expected characters.
14         expected: &'static str,
15         /// The invalid character found.
16         found: char,
17         /// The invalid character position.
18         index: usize,
19         /// Indicates the [`Uuid`] starts with `urn:uuid:`.
20         ///
21         /// This is a special case for [`Urn`] adapter parsing.
22         ///
23         /// [`Uuid`]: ../Uuid.html
24         urn: UrnPrefix,
25     },
26     /// Invalid number of segments in the [`Uuid`] string.
27     ///
28     /// [`Uuid`]: ../struct.Uuid.html
29     InvalidGroupCount {
30         /// The expected number of segments.
31         // TODO: explain multiple segment count.
32         // BODY: Parsers can expect a range of Uuid segment count.
33         //       This needs to be expanded on.
34         expected: ExpectedLength,
35         /// The number of segments found.
36         found: usize,
37     },
38     /// Invalid length of a segment in a [`Uuid`] string.
39     ///
40     /// [`Uuid`]: ../struct.Uuid.html
41     InvalidGroupLength {
42         /// The expected length of the segment.
43         expected: ExpectedLength,
44         /// The length of segment found.
45         found: usize,
46         /// The segment with invalid length.
47         group: usize,
48     },
49     /// Invalid length of the [`Uuid`] string.
50     ///
51     /// [`Uuid`]: ../struct.Uuid.html
52     InvalidLength {
53         /// The expected length(s).
54         // TODO: explain multiple lengths.
55         // BODY: Parsers can expect a range of Uuid lenghts.
56         //       This needs to be expanded on.
57         expected: ExpectedLength,
58         /// The invalid length found.
59         found: usize,
60     },
61 }
62 
63 /// The expected length.
64 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
65 pub(crate) enum ExpectedLength {
66     /// Expected any one of the given values.
67     Any(&'static [usize]),
68     /// Expected the given value.
69     Exact(usize),
70 }
71 
72 /// Urn prefix value.
73 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
74 pub(crate) enum UrnPrefix {
75     /// The `urn:uuid:` prefix should optionally provided.
76     Optional,
77 }
78 
79 impl Error {
_description(&self) -> &str80     fn _description(&self) -> &str {
81         match *self {
82             Error::InvalidCharacter { .. } => "invalid character",
83             Error::InvalidGroupCount { .. } => "invalid number of groups",
84             Error::InvalidGroupLength { .. } => "invalid group length",
85             Error::InvalidLength { .. } => "invalid length",
86         }
87     }
88 }
89 
90 impl fmt::Display for ExpectedLength {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result91     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92         match *self {
93             ExpectedLength::Any(crits) => write!(f, "one of {:?}", crits),
94             ExpectedLength::Exact(crit) => write!(f, "{}", crit),
95         }
96     }
97 }
98 
99 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result100     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101         write!(f, "{}: ", self._description())?;
102 
103         match *self {
104             Error::InvalidCharacter {
105                 expected,
106                 found,
107                 index,
108                 urn,
109             } => {
110                 let urn_str = match urn {
111                     UrnPrefix::Optional => {
112                         " an optional prefix of `urn:uuid:` followed by"
113                     }
114                 };
115 
116                 write!(
117                     f,
118                     "expected{} {}, found {} at {}",
119                     urn_str, expected, found, index
120                 )
121             }
122             Error::InvalidGroupCount {
123                 ref expected,
124                 found,
125             } => write!(f, "expected {}, found {}", expected, found),
126             Error::InvalidGroupLength {
127                 ref expected,
128                 found,
129                 group,
130             } => write!(
131                 f,
132                 "expected {}, found {} in group {}",
133                 expected, found, group,
134             ),
135             Error::InvalidLength {
136                 ref expected,
137                 found,
138             } => write!(f, "expected {}, found {}", expected, found),
139         }
140     }
141 }
142 
143 #[cfg(feature = "std")]
144 mod std_support {
145     use super::*;
146     use crate::std::error;
147 
148     impl error::Error for Error {}
149 }
150