1use crate::std::fmt;
2
3#[derive(Clone, Debug, Eq, Hash, PartialEq)]
5pub struct Error(pub(crate) ErrorKind);
6
7#[derive(Clone, Debug, Eq, Hash, PartialEq)]
8pub(crate) enum ErrorKind {
9 Char { character: char, index: usize },
13 SimpleLength { len: usize },
17 ByteLength { len: usize },
19 GroupCount { count: usize },
23 GroupLength {
27 group: usize,
28 len: usize,
29 index: usize,
30 },
31 InvalidUTF8,
33 Nil,
35 Other,
37}
38
39#[derive(Clone, Debug, Eq, Hash, PartialEq)]
47pub struct InvalidUuid<'a>(pub(crate) &'a [u8]);
48
49impl<'a> InvalidUuid<'a> {
50 pub fn into_err(self) -> Error {
52 let input_str = match std::str::from_utf8(self.0) {
54 Ok(s) => s,
55 Err(_) => return Error(ErrorKind::InvalidUTF8),
56 };
57
58 let (uuid_str, offset, simple) = match input_str.as_bytes() {
59 [b'{', s @ .., b'}'] => (s, 1, false),
60 [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..] => {
61 (s, "urn:uuid:".len(), false)
62 }
63 s => (s, 0, true),
64 };
65
66 let mut hyphen_count = 0;
67 let mut group_bounds = [0; 4];
68
69 let uuid_str = unsafe { std::str::from_utf8_unchecked(uuid_str) };
72
73 for (index, character) in uuid_str.char_indices() {
74 let byte = character as u8;
75 if character as u32 - byte as u32 > 0 {
76 return Error(ErrorKind::Char {
78 character,
79 index: index + offset + 1,
80 });
81 } else if byte == b'-' {
82 if hyphen_count < 4 {
84 group_bounds[hyphen_count] = index;
85 }
86 hyphen_count += 1;
87 } else if !byte.is_ascii_hexdigit() {
88 return Error(ErrorKind::Char {
90 character: byte as char,
91 index: index + offset + 1,
92 });
93 }
94 }
95
96 if hyphen_count == 0 && simple {
97 Error(ErrorKind::SimpleLength {
101 len: input_str.len(),
102 })
103 } else if hyphen_count != 4 {
104 Error(ErrorKind::GroupCount {
107 count: hyphen_count + 1,
108 })
109 } else {
110 const BLOCK_STARTS: [usize; 5] = [0, 9, 14, 19, 24];
112 for i in 0..4 {
113 if group_bounds[i] != BLOCK_STARTS[i + 1] - 1 {
114 return Error(ErrorKind::GroupLength {
115 group: i,
116 len: group_bounds[i] - BLOCK_STARTS[i],
117 index: offset + BLOCK_STARTS[i] + 1,
118 });
119 }
120 }
121
122 Error(ErrorKind::GroupLength {
124 group: 4,
125 len: input_str.len() - BLOCK_STARTS[4],
126 index: offset + BLOCK_STARTS[4] + 1,
127 })
128 }
129 }
130}
131
132impl fmt::Display for Error {
134 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135 match self.0 {
136 ErrorKind::Char {
137 character, index, ..
138 } => {
139 write!(f, "invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-fA-F-], found `{}` at {}", character, index)
140 }
141 ErrorKind::SimpleLength { len } => {
142 write!(
143 f,
144 "invalid length: expected length 32 for simple format, found {}",
145 len
146 )
147 }
148 ErrorKind::ByteLength { len } => {
149 write!(f, "invalid length: expected 16 bytes, found {}", len)
150 }
151 ErrorKind::GroupCount { count } => {
152 write!(f, "invalid group count: expected 5, found {}", count)
153 }
154 ErrorKind::GroupLength { group, len, .. } => {
155 let expected = [8, 4, 4, 4, 12][group];
156 write!(
157 f,
158 "invalid group length in group {}: expected {}, found {}",
159 group, expected, len
160 )
161 }
162 ErrorKind::InvalidUTF8 => write!(f, "non-UTF8 input"),
163 ErrorKind::Nil => write!(f, "the UUID is nil"),
164 ErrorKind::Other => write!(f, "failed to parse a UUID"),
165 }
166 }
167}
168
169#[cfg(feature = "std")]
170mod std_support {
171 use super::*;
172 use crate::std::error;
173
174 impl error::Error for Error {}
175}