1use crate::std::fmt;
2
3#[derive(#[automatically_derived]
impl ::core::clone::Clone for Error {
#[inline]
fn clone(&self) -> Error { Error(::core::clone::Clone::clone(&self.0)) }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Error {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Error",
&&self.0)
}
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for Error {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ErrorKind>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Error {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for Error {
#[inline]
fn eq(&self, other: &Error) -> bool { self.0 == other.0 }
}PartialEq)]
5pub struct Error(pub(crate) ErrorKind);
6
7#[derive(#[automatically_derived]
impl ::core::clone::Clone for ErrorKind {
#[inline]
fn clone(&self) -> ErrorKind {
match self {
ErrorKind::ParseChar { character: __self_0, index: __self_1 } =>
ErrorKind::ParseChar {
character: ::core::clone::Clone::clone(__self_0),
index: ::core::clone::Clone::clone(__self_1),
},
ErrorKind::ParseByteLength { len: __self_0 } =>
ErrorKind::ParseByteLength {
len: ::core::clone::Clone::clone(__self_0),
},
ErrorKind::ParseGroupCount { count: __self_0 } =>
ErrorKind::ParseGroupCount {
count: ::core::clone::Clone::clone(__self_0),
},
ErrorKind::ParseGroupLength {
group: __self_0, len: __self_1, index: __self_2 } =>
ErrorKind::ParseGroupLength {
group: ::core::clone::Clone::clone(__self_0),
len: ::core::clone::Clone::clone(__self_1),
index: ::core::clone::Clone::clone(__self_2),
},
ErrorKind::ParseInvalidUTF8 => ErrorKind::ParseInvalidUTF8,
ErrorKind::ParseLength { len: __self_0 } =>
ErrorKind::ParseLength {
len: ::core::clone::Clone::clone(__self_0),
},
ErrorKind::ParseOther => ErrorKind::ParseOther,
ErrorKind::Nil => ErrorKind::Nil,
ErrorKind::InvalidSystemTime(__self_0) =>
ErrorKind::InvalidSystemTime(::core::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ErrorKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ErrorKind::ParseChar { character: __self_0, index: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ParseChar", "character", __self_0, "index", &__self_1),
ErrorKind::ParseByteLength { len: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"ParseByteLength", "len", &__self_0),
ErrorKind::ParseGroupCount { count: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"ParseGroupCount", "count", &__self_0),
ErrorKind::ParseGroupLength {
group: __self_0, len: __self_1, index: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"ParseGroupLength", "group", __self_0, "len", __self_1,
"index", &__self_2),
ErrorKind::ParseInvalidUTF8 =>
::core::fmt::Formatter::write_str(f, "ParseInvalidUTF8"),
ErrorKind::ParseLength { len: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"ParseLength", "len", &__self_0),
ErrorKind::ParseOther =>
::core::fmt::Formatter::write_str(f, "ParseOther"),
ErrorKind::Nil => ::core::fmt::Formatter::write_str(f, "Nil"),
ErrorKind::InvalidSystemTime(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InvalidSystemTime", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for ErrorKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<char>;
let _: ::core::cmp::AssertParamIsEq<usize>;
let _: ::core::cmp::AssertParamIsEq<&'static str>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ErrorKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
ErrorKind::ParseChar { character: __self_0, index: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
ErrorKind::ParseByteLength { len: __self_0 } =>
::core::hash::Hash::hash(__self_0, state),
ErrorKind::ParseGroupCount { count: __self_0 } =>
::core::hash::Hash::hash(__self_0, state),
ErrorKind::ParseGroupLength {
group: __self_0, len: __self_1, index: __self_2 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state);
::core::hash::Hash::hash(__self_2, state)
}
ErrorKind::ParseLength { len: __self_0 } =>
::core::hash::Hash::hash(__self_0, state),
ErrorKind::InvalidSystemTime(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for ErrorKind {
#[inline]
fn eq(&self, other: &ErrorKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(ErrorKind::ParseChar { character: __self_0, index: __self_1
}, ErrorKind::ParseChar {
character: __arg1_0, index: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(ErrorKind::ParseByteLength { len: __self_0 },
ErrorKind::ParseByteLength { len: __arg1_0 }) =>
__self_0 == __arg1_0,
(ErrorKind::ParseGroupCount { count: __self_0 },
ErrorKind::ParseGroupCount { count: __arg1_0 }) =>
__self_0 == __arg1_0,
(ErrorKind::ParseGroupLength {
group: __self_0, len: __self_1, index: __self_2 },
ErrorKind::ParseGroupLength {
group: __arg1_0, len: __arg1_1, index: __arg1_2 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1 &&
__self_2 == __arg1_2,
(ErrorKind::ParseLength { len: __self_0 },
ErrorKind::ParseLength { len: __arg1_0 }) =>
__self_0 == __arg1_0,
(ErrorKind::InvalidSystemTime(__self_0),
ErrorKind::InvalidSystemTime(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq)]
8pub(crate) enum ErrorKind {
9 ParseChar { character: char, index: usize },
13 ParseByteLength { len: usize },
15 ParseGroupCount { count: usize },
19 ParseGroupLength {
23 group: usize,
24 len: usize,
25 index: usize,
26 },
27 ParseInvalidUTF8,
29 ParseLength { len: usize },
31 ParseOther,
33 Nil,
35 #[cfg(feature = "std")]
37 InvalidSystemTime(&'static str),
38}
39
40#[derive(#[automatically_derived]
impl<'a> ::core::clone::Clone for InvalidUuid<'a> {
#[inline]
fn clone(&self) -> InvalidUuid<'a> {
let _: ::core::clone::AssertParamIsClone<&'a [u8]>;
let _: ::core::clone::AssertParamIsClone<RequestedUuid>;
*self
}
}Clone, #[automatically_derived]
impl<'a> ::core::marker::Copy for InvalidUuid<'a> { }Copy, #[automatically_derived]
impl<'a> ::core::fmt::Debug for InvalidUuid<'a> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field2_finish(f, "InvalidUuid",
&self.0, &&self.1)
}
}Debug, #[automatically_derived]
impl<'a> ::core::cmp::Eq for InvalidUuid<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<&'a [u8]>;
let _: ::core::cmp::AssertParamIsEq<RequestedUuid>;
}
}Eq, #[automatically_derived]
impl<'a> ::core::hash::Hash for InvalidUuid<'a> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state);
::core::hash::Hash::hash(&self.1, state)
}
}Hash, #[automatically_derived]
impl<'a> ::core::cmp::PartialEq for InvalidUuid<'a> {
#[inline]
fn eq(&self, other: &InvalidUuid<'a>) -> bool {
self.0 == other.0 && self.1 == other.1
}
}PartialEq)]
48pub(crate) struct InvalidUuid<'a>(pub(crate) &'a [u8], pub(crate) RequestedUuid);
49
50#[derive(#[automatically_derived]
impl ::core::clone::Clone for RequestedUuid {
#[inline]
fn clone(&self) -> RequestedUuid { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for RequestedUuid { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for RequestedUuid {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
RequestedUuid::Any => "Any",
RequestedUuid::Simple => "Simple",
RequestedUuid::Hyphenated => "Hyphenated",
RequestedUuid::Braced => "Braced",
RequestedUuid::Urn => "Urn",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for RequestedUuid {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for RequestedUuid {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for RequestedUuid {
#[inline]
fn eq(&self, other: &RequestedUuid) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
51pub(crate) enum RequestedUuid {
52 Any,
53 Simple,
54 Hyphenated,
55 Braced,
56 Urn,
57}
58
59impl<'a> InvalidUuid<'a> {
60 pub fn into_err(self) -> Error {
62 if self.0.len() == 0 || self.0.len() > 45 {
63 return Error(ErrorKind::ParseLength { len: self.0.len() });
65 }
66
67 let input_str = match std::str::from_utf8(self.0) {
69 Ok(s) => s,
70 Err(_) => return Error(ErrorKind::ParseInvalidUTF8),
71 };
72
73 let (bounds, mut format) = match (self.1, self.0) {
74 (RequestedUuid::Any | RequestedUuid::Braced, [b'{', s @ .., b'}']) => {
75 (1..s.len() - 1, RequestedUuid::Braced)
76 }
77 (RequestedUuid::Braced, _) => {
78 if self.0[0] != b'{' {
79 let (index, character) = input_str.char_indices().next().unwrap();
81
82 return Error(ErrorKind::ParseChar { character, index });
83 } else {
84 let (index, character) = input_str.char_indices().last().unwrap();
86
87 return Error(ErrorKind::ParseChar { character, index });
88 }
89 }
90 (
91 RequestedUuid::Any | RequestedUuid::Urn,
92 [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..],
93 ) => ("urn:uuid:".len()..s.len(), RequestedUuid::Urn),
94 (RequestedUuid::Urn, _) => {
95 return Error(ErrorKind::ParseChar {
96 character: input_str.chars().next().unwrap(),
97 index: 0,
98 })
99 }
100 (r, s) => (0..s.len(), r),
101 };
102
103 let mut hyphen_count = 0;
104 let mut group_bounds = [0; 4];
105
106 let uuid_str = unsafe { std::str::from_utf8_unchecked(self.0) };
109
110 for (index, character) in uuid_str[bounds.clone()].char_indices() {
111 let byte = character as u8;
112
113 match (format, byte.to_ascii_lowercase()) {
114 (_, b'0'..=b'9' | b'a'..=b'f') => (),
115 (RequestedUuid::Simple, b'-') => {
116 return Error(ErrorKind::ParseChar {
117 character: '-',
118 index: index + bounds.start,
119 })
120 }
121 (_, b'-') => {
122 if format == RequestedUuid::Any {
123 format = RequestedUuid::Hyphenated;
124 }
125
126 if hyphen_count < 4 {
127 group_bounds[hyphen_count] = index;
129 }
130 hyphen_count += 1;
131 }
132 _ => {
133 return Error(ErrorKind::ParseChar {
134 character,
135 index: index + bounds.start,
136 })
137 }
138 }
139 }
140
141 if format == RequestedUuid::Any || format == RequestedUuid::Simple {
142 Error(ErrorKind::ParseLength {
146 len: input_str.len(),
147 })
148 } else if hyphen_count != 4 {
149 Error(ErrorKind::ParseGroupCount {
152 count: hyphen_count + 1,
153 })
154 } else {
155 const BLOCK_STARTS: [usize; 5] = [0, 9, 14, 19, 24];
157 for i in 0..4 {
158 if group_bounds[i] != BLOCK_STARTS[i + 1] - 1 {
159 return Error(ErrorKind::ParseGroupLength {
160 group: i,
161 len: group_bounds[i] - BLOCK_STARTS[i],
162 index: bounds.start + BLOCK_STARTS[i] + 1,
163 });
164 }
165 }
166
167 Error(ErrorKind::ParseGroupLength {
169 group: 4,
170 len: input_str.len() - BLOCK_STARTS[4],
171 index: bounds.start + BLOCK_STARTS[4] + 1,
172 })
173 }
174 }
175}
176
177impl fmt::Display for Error {
179 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180 match self.0 {
181 ErrorKind::ParseChar {
182 character, index, ..
183 } => {
184 f.write_fmt(format_args!("invalid character: found `{0}` at {1}", character,
index))write!(f, "invalid character: found `{}` at {}", character, index)
185 }
186 ErrorKind::ParseByteLength { len } => {
187 f.write_fmt(format_args!("invalid length: expected 16 bytes, found {0}", len))write!(f, "invalid length: expected 16 bytes, found {}", len)
188 }
189 ErrorKind::ParseGroupCount { count } => {
190 f.write_fmt(format_args!("invalid group count: expected 5, found {0}", count))write!(f, "invalid group count: expected 5, found {}", count)
191 }
192 ErrorKind::ParseGroupLength { group, len, .. } => {
193 let expected = [8, 4, 4, 4, 12][group];
194 f.write_fmt(format_args!("invalid group length in group {0}: expected {1}, found {2}",
group, expected, len))write!(
195 f,
196 "invalid group length in group {}: expected {}, found {}",
197 group, expected, len
198 )
199 }
200 ErrorKind::ParseInvalidUTF8 => f.write_fmt(format_args!("non-UTF8 input"))write!(f, "non-UTF8 input"),
201 ErrorKind::Nil => f.write_fmt(format_args!("the UUID is nil"))write!(f, "the UUID is nil"),
202 ErrorKind::ParseLength { len } => f.write_fmt(format_args!("invalid length: found {0}", len))write!(f, "invalid length: found {}", len),
203 ErrorKind::ParseOther => f.write_fmt(format_args!("failed to parse a UUID"))write!(f, "failed to parse a UUID"),
204 #[cfg(feature = "std")]
205 ErrorKind::InvalidSystemTime(ref e) => {
206 f.write_fmt(format_args!("the system timestamp is invalid: {0}", e))write!(f, "the system timestamp is invalid: {e}")
207 }
208 }
209 }
210}
211
212impl crate::std::error::Error for Error {}