Skip to content

Commit 281f26f

Browse files
authored
Merge pull request #838 from uuid-rs/chore/time-conversion
Wrap the error type used in time conversions
2 parents 87a4359 + 2d67ab2 commit 281f26f

File tree

4 files changed

+65
-60
lines changed

4 files changed

+65
-60
lines changed

src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl Uuid {
286286
/// ```
287287
pub fn from_slice(b: &[u8]) -> Result<Uuid, Error> {
288288
if b.len() != 16 {
289-
return Err(Error(ErrorKind::ByteLength { len: b.len() }));
289+
return Err(Error(ErrorKind::ParseByteLength { len: b.len() }));
290290
}
291291

292292
let mut bytes: Bytes = [0; 16];
@@ -327,7 +327,7 @@ impl Uuid {
327327
/// ```
328328
pub fn from_slice_le(b: &[u8]) -> Result<Uuid, Error> {
329329
if b.len() != 16 {
330-
return Err(Error(ErrorKind::ByteLength { len: b.len() }));
330+
return Err(Error(ErrorKind::ParseByteLength { len: b.len() }));
331331
}
332332

333333
let mut bytes: Bytes = [0; 16];

src/error.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,34 @@ pub(crate) enum ErrorKind {
99
/// Invalid character in the [`Uuid`] string.
1010
///
1111
/// [`Uuid`]: ../struct.Uuid.html
12-
Char { character: char, index: usize },
12+
ParseChar { character: char, index: usize },
1313
/// A simple [`Uuid`] didn't contain 32 characters.
1414
///
1515
/// [`Uuid`]: ../struct.Uuid.html
16-
SimpleLength { len: usize },
16+
ParseSimpleLength { len: usize },
1717
/// A byte array didn't contain 16 bytes
18-
ByteLength { len: usize },
18+
ParseByteLength { len: usize },
1919
/// A hyphenated [`Uuid`] didn't contain 5 groups
2020
///
2121
/// [`Uuid`]: ../struct.Uuid.html
22-
GroupCount { count: usize },
22+
ParseGroupCount { count: usize },
2323
/// A hyphenated [`Uuid`] had a group that wasn't the right length
2424
///
2525
/// [`Uuid`]: ../struct.Uuid.html
26-
GroupLength {
26+
ParseGroupLength {
2727
group: usize,
2828
len: usize,
2929
index: usize,
3030
},
3131
/// The input was not a valid UTF8 string
32-
InvalidUTF8,
32+
ParseInvalidUTF8,
33+
/// Some other parsing error occurred.
34+
ParseOther,
3335
/// The UUID is nil.
3436
Nil,
35-
/// Some other error occurred.
36-
Other,
37+
/// A system time was invalid.
38+
#[cfg(feature = "std")]
39+
InvalidSystemTime(&'static str),
3740
}
3841

3942
/// A string that is guaranteed to fail to parse to a [`Uuid`].
@@ -52,7 +55,7 @@ impl<'a> InvalidUuid<'a> {
5255
// Check whether or not the input was ever actually a valid UTF8 string
5356
let input_str = match std::str::from_utf8(self.0) {
5457
Ok(s) => s,
55-
Err(_) => return Error(ErrorKind::InvalidUTF8),
58+
Err(_) => return Error(ErrorKind::ParseInvalidUTF8),
5659
};
5760

5861
let (uuid_str, offset, simple) = match input_str.as_bytes() {
@@ -74,7 +77,7 @@ impl<'a> InvalidUuid<'a> {
7477
let byte = character as u8;
7578
if character as u32 - byte as u32 > 0 {
7679
// Multibyte char
77-
return Error(ErrorKind::Char {
80+
return Error(ErrorKind::ParseChar {
7881
character,
7982
index: index + offset + 1,
8083
});
@@ -86,7 +89,7 @@ impl<'a> InvalidUuid<'a> {
8689
hyphen_count += 1;
8790
} else if !byte.is_ascii_hexdigit() {
8891
// Non-hex char
89-
return Error(ErrorKind::Char {
92+
return Error(ErrorKind::ParseChar {
9093
character: byte as char,
9194
index: index + offset + 1,
9295
});
@@ -97,21 +100,21 @@ impl<'a> InvalidUuid<'a> {
97100
// This means that we tried and failed to parse a simple uuid.
98101
// Since we verified that all the characters are valid, this means
99102
// that it MUST have an invalid length.
100-
Error(ErrorKind::SimpleLength {
103+
Error(ErrorKind::ParseSimpleLength {
101104
len: input_str.len(),
102105
})
103106
} else if hyphen_count != 4 {
104107
// We tried to parse a hyphenated variant, but there weren't
105108
// 5 groups (4 hyphen splits).
106-
Error(ErrorKind::GroupCount {
109+
Error(ErrorKind::ParseGroupCount {
107110
count: hyphen_count + 1,
108111
})
109112
} else {
110113
// There are 5 groups, one of them has an incorrect length
111114
const BLOCK_STARTS: [usize; 5] = [0, 9, 14, 19, 24];
112115
for i in 0..4 {
113116
if group_bounds[i] != BLOCK_STARTS[i + 1] - 1 {
114-
return Error(ErrorKind::GroupLength {
117+
return Error(ErrorKind::ParseGroupLength {
115118
group: i,
116119
len: group_bounds[i] - BLOCK_STARTS[i],
117120
index: offset + BLOCK_STARTS[i] + 1,
@@ -120,7 +123,7 @@ impl<'a> InvalidUuid<'a> {
120123
}
121124

122125
// The last group must be too long
123-
Error(ErrorKind::GroupLength {
126+
Error(ErrorKind::ParseGroupLength {
124127
group: 4,
125128
len: input_str.len() - BLOCK_STARTS[4],
126129
index: offset + BLOCK_STARTS[4] + 1,
@@ -133,35 +136,37 @@ impl<'a> InvalidUuid<'a> {
133136
impl fmt::Display for Error {
134137
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135138
match self.0 {
136-
ErrorKind::Char {
139+
ErrorKind::ParseChar {
137140
character, index, ..
138141
} => {
139142
write!(f, "invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-fA-F-], found `{}` at {}", character, index)
140143
}
141-
ErrorKind::SimpleLength { len } => {
144+
ErrorKind::ParseSimpleLength { len } => {
142145
write!(
143146
f,
144147
"invalid length: expected length 32 for simple format, found {}",
145148
len
146149
)
147150
}
148-
ErrorKind::ByteLength { len } => {
151+
ErrorKind::ParseByteLength { len } => {
149152
write!(f, "invalid length: expected 16 bytes, found {}", len)
150153
}
151-
ErrorKind::GroupCount { count } => {
154+
ErrorKind::ParseGroupCount { count } => {
152155
write!(f, "invalid group count: expected 5, found {}", count)
153156
}
154-
ErrorKind::GroupLength { group, len, .. } => {
157+
ErrorKind::ParseGroupLength { group, len, .. } => {
155158
let expected = [8, 4, 4, 4, 12][group];
156159
write!(
157160
f,
158161
"invalid group length in group {}: expected {}, found {}",
159162
group, expected, len
160163
)
161164
}
162-
ErrorKind::InvalidUTF8 => write!(f, "non-UTF8 input"),
165+
ErrorKind::ParseInvalidUTF8 => write!(f, "non-UTF8 input"),
163166
ErrorKind::Nil => write!(f, "the UUID is nil"),
164-
ErrorKind::Other => write!(f, "failed to parse a UUID"),
167+
ErrorKind::ParseOther => write!(f, "failed to parse a UUID"),
168+
#[cfg(feature = "std")]
169+
ErrorKind::InvalidSystemTime(ref e) => write!(f, "the system timestamp is invalid: {e}"),
165170
}
166171
}
167172
}
@@ -171,5 +176,5 @@ mod std_support {
171176
use super::*;
172177
use crate::std::error;
173178

174-
impl error::Error for Error {}
179+
impl error::Error for Error { }
175180
}

src/parser.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Uuid {
139139
Ok(bytes) => Ok(Uuid::from_bytes(bytes)),
140140
// If parsing fails then we don't know exactly what went wrong
141141
// In this case, we just return a generic error
142-
Err(_) => Err(Error(ErrorKind::Other)),
142+
Err(_) => Err(Error(ErrorKind::ParseOther)),
143143
}
144144
}
145145
}
@@ -341,20 +341,20 @@ mod tests {
341341
// Invalid
342342
assert_eq!(
343343
Uuid::parse_str(""),
344-
Err(Error(ErrorKind::SimpleLength { len: 0 }))
344+
Err(Error(ErrorKind::ParseSimpleLength { len: 0 }))
345345
);
346346

347347
assert_eq!(
348348
Uuid::parse_str("!"),
349-
Err(Error(ErrorKind::Char {
349+
Err(Error(ErrorKind::ParseChar {
350350
character: '!',
351351
index: 1,
352352
}))
353353
);
354354

355355
assert_eq!(
356356
Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E45"),
357-
Err(Error(ErrorKind::GroupLength {
357+
Err(Error(ErrorKind::ParseGroupLength {
358358
group: 4,
359359
len: 13,
360360
index: 25,
@@ -363,7 +363,7 @@ mod tests {
363363

364364
assert_eq!(
365365
Uuid::parse_str("F9168C5E-CEB2-4faa-BBF-329BF39FA1E4"),
366-
Err(Error(ErrorKind::GroupLength {
366+
Err(Error(ErrorKind::ParseGroupLength {
367367
group: 3,
368368
len: 3,
369369
index: 20,
@@ -372,56 +372,56 @@ mod tests {
372372

373373
assert_eq!(
374374
Uuid::parse_str("F9168C5E-CEB2-4faa-BGBF-329BF39FA1E4"),
375-
Err(Error(ErrorKind::Char {
375+
Err(Error(ErrorKind::ParseChar {
376376
character: 'G',
377377
index: 21,
378378
}))
379379
);
380380

381381
assert_eq!(
382382
Uuid::parse_str("F9168C5E-CEB2F4faaFB6BFF329BF39FA1E4"),
383-
Err(Error(ErrorKind::GroupCount { count: 2 }))
383+
Err(Error(ErrorKind::ParseGroupCount { count: 2 }))
384384
);
385385

386386
assert_eq!(
387387
Uuid::parse_str("F9168C5E-CEB2-4faaFB6BFF329BF39FA1E4"),
388-
Err(Error(ErrorKind::GroupCount { count: 3 }))
388+
Err(Error(ErrorKind::ParseGroupCount { count: 3 }))
389389
);
390390

391391
assert_eq!(
392392
Uuid::parse_str("F9168C5E-CEB2-4faa-B6BFF329BF39FA1E4"),
393-
Err(Error(ErrorKind::GroupCount { count: 4 }))
393+
Err(Error(ErrorKind::ParseGroupCount { count: 4 }))
394394
);
395395

396396
assert_eq!(
397397
Uuid::parse_str("F9168C5E-CEB2-4faa"),
398-
Err(Error(ErrorKind::GroupCount { count: 3 }))
398+
Err(Error(ErrorKind::ParseGroupCount { count: 3 }))
399399
);
400400

401401
assert_eq!(
402402
Uuid::parse_str("F9168C5E-CEB2-4faaXB6BFF329BF39FA1E4"),
403-
Err(Error(ErrorKind::Char {
403+
Err(Error(ErrorKind::ParseChar {
404404
character: 'X',
405405
index: 19,
406406
}))
407407
);
408408

409409
assert_eq!(
410410
Uuid::parse_str("{F9168C5E-CEB2-4faa9B6BFF329BF39FA1E41"),
411-
Err(Error(ErrorKind::Char {
411+
Err(Error(ErrorKind::ParseChar {
412412
character: '{',
413413
index: 1,
414414
}))
415415
);
416416

417417
assert_eq!(
418418
Uuid::parse_str("{F9168C5E-CEB2-4faa9B6BFF329BF39FA1E41}"),
419-
Err(Error(ErrorKind::GroupCount { count: 3 }))
419+
Err(Error(ErrorKind::ParseGroupCount { count: 3 }))
420420
);
421421

422422
assert_eq!(
423423
Uuid::parse_str("F9168C5E-CEB-24fa-eB6BFF32-BF39FA1E4"),
424-
Err(Error(ErrorKind::GroupLength {
424+
Err(Error(ErrorKind::ParseGroupLength {
425425
group: 1,
426426
len: 3,
427427
index: 10,
@@ -432,7 +432,7 @@ mod tests {
432432
// //
433433
assert_eq!(
434434
Uuid::parse_str("01020304-1112-2122-3132-41424344"),
435-
Err(Error(ErrorKind::GroupLength {
435+
Err(Error(ErrorKind::ParseGroupLength {
436436
group: 4,
437437
len: 8,
438438
index: 25,
@@ -441,61 +441,61 @@ mod tests {
441441

442442
assert_eq!(
443443
Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c"),
444-
Err(Error(ErrorKind::SimpleLength { len: 31 }))
444+
Err(Error(ErrorKind::ParseSimpleLength { len: 31 }))
445445
);
446446

447447
assert_eq!(
448448
Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c88"),
449-
Err(Error(ErrorKind::SimpleLength { len: 33 }))
449+
Err(Error(ErrorKind::ParseSimpleLength { len: 33 }))
450450
);
451451

452452
assert_eq!(
453453
Uuid::parse_str("67e5504410b1426f9247bb680e5fe0cg8"),
454-
Err(Error(ErrorKind::Char {
454+
Err(Error(ErrorKind::ParseChar {
455455
character: 'g',
456456
index: 32,
457457
}))
458458
);
459459

460460
assert_eq!(
461461
Uuid::parse_str("67e5504410b1426%9247bb680e5fe0c8"),
462-
Err(Error(ErrorKind::Char {
462+
Err(Error(ErrorKind::ParseChar {
463463
character: '%',
464464
index: 16,
465465
}))
466466
);
467467

468468
assert_eq!(
469469
Uuid::parse_str("231231212212423424324323477343246663"),
470-
Err(Error(ErrorKind::SimpleLength { len: 36 }))
470+
Err(Error(ErrorKind::ParseSimpleLength { len: 36 }))
471471
);
472472

473473
assert_eq!(
474474
Uuid::parse_str("{00000000000000000000000000000000}"),
475-
Err(Error(ErrorKind::GroupCount { count: 1 }))
475+
Err(Error(ErrorKind::ParseGroupCount { count: 1 }))
476476
);
477477

478478
assert_eq!(
479479
Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c"),
480-
Err(Error(ErrorKind::SimpleLength { len: 31 }))
480+
Err(Error(ErrorKind::ParseSimpleLength { len: 31 }))
481481
);
482482

483483
assert_eq!(
484484
Uuid::parse_str("67e550X410b1426f9247bb680e5fe0cd"),
485-
Err(Error(ErrorKind::Char {
485+
Err(Error(ErrorKind::ParseChar {
486486
character: 'X',
487487
index: 7,
488488
}))
489489
);
490490

491491
assert_eq!(
492492
Uuid::parse_str("67e550-4105b1426f9247bb680e5fe0c"),
493-
Err(Error(ErrorKind::GroupCount { count: 2 }))
493+
Err(Error(ErrorKind::ParseGroupCount { count: 2 }))
494494
);
495495

496496
assert_eq!(
497497
Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF1-02BF39FA1E4"),
498-
Err(Error(ErrorKind::GroupLength {
498+
Err(Error(ErrorKind::ParseGroupLength {
499499
group: 3,
500500
len: 5,
501501
index: 20,
@@ -504,7 +504,7 @@ mod tests {
504504

505505
assert_eq!(
506506
Uuid::parse_str("\u{bcf3c}"),
507-
Err(Error(ErrorKind::Char {
507+
Err(Error(ErrorKind::ParseChar {
508508
character: '\u{bcf3c}',
509509
index: 1
510510
}))

0 commit comments

Comments
 (0)