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/// If the `std` feature is enabled, this type implements the [`Error`]
14/// trait.
15///
16/// [`Error`]: std::error::Error
17/// [`Guid::from_str`]: core::str::FromStr::from_str
18/// [`Guid::try_parse`]: crate::Guid::try_parse
19#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
20pub enum GuidFromStrError {
21 /// Input has the wrong length, expected 36 bytes.
22 Length,
23
24 /// Input is missing a separator (`-`) at this byte index.
25 Separator(u8),
26
27 /// Input contains invalid ASCII hex at this byte index.
28 Hex(u8),
29}
30
31impl Default for GuidFromStrError {
32 fn default() -> Self {
33 Self::Length
34 }
35}
36
37impl Display for GuidFromStrError {
38 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39 match self {
40 Self::Length => {
41 f.write_str("GUID string has wrong length (expected 36 bytes)")
42 }
43 Self::Separator(index) => write!(
44 f,
45 "GUID string is missing a separator (`-`) at index {index}",
46 ),
47 Self::Hex(index) => {
48 write!(
49 f,
50 "GUID string contains invalid ASCII hex at index {index}",
51 )
52 }
53 }
54 }
55}