uguid/error.rs
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use core::fmt::{self, Display, Formatter};
10
11/// Error type for [`Guid::try_parse`] and [`Guid::from_str`].
12///
13/// [`Guid::from_str`]: core::str::FromStr::from_str
14/// [`Guid::try_parse`]: crate::Guid::try_parse
15#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
16pub enum GuidFromStrError {
17 /// Input has the wrong length, expected 36 bytes.
18 Length,
19
20 /// Input is missing a separator (`-`) at this byte index.
21 Separator(u8),
22
23 /// Input contains invalid ASCII hex at this byte index.
24 Hex(u8),
25}
26
27impl Default for GuidFromStrError {
28 fn default() -> Self {
29 Self::Length
30 }
31}
32
33impl Display for GuidFromStrError {
34 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
35 match self {
36 Self::Length => {
37 f.write_str("GUID string has wrong length (expected 36 bytes)")
38 }
39 Self::Separator(index) => write!(
40 f,
41 "GUID string is missing a separator (`-`) at index {index}",
42 ),
43 Self::Hex(index) => {
44 write!(
45 f,
46 "GUID string contains invalid ASCII hex at index {index}",
47 )
48 }
49 }
50 }
51}
52
53impl core::error::Error for GuidFromStrError {}