unc_account_id/errors.rs
1use std::fmt;
2use std::fmt::Write;
3
4/// An error which can be returned when parsing a Utility Account ID.
5#[derive(Eq, Clone, Debug, PartialEq)]
6pub struct ParseAccountError {
7 pub(crate) kind: ParseErrorKind,
8 pub(crate) char: Option<(usize, char)>,
9}
10
11impl ParseAccountError {
12 /// Returns the specific cause why parsing the Account ID failed.
13 pub fn kind(&self) -> &ParseErrorKind {
14 &self.kind
15 }
16}
17
18impl std::error::Error for ParseAccountError {}
19impl fmt::Display for ParseAccountError {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 let mut buf = self.kind.to_string();
22 if let Some((idx, char)) = self.char {
23 write!(buf, " {:?} at index {}", char, idx)?
24 }
25 buf.fmt(f)
26 }
27}
28
29/// A list of errors that occur when parsing an invalid Account ID.
30///
31/// Also see [Error kind precedence](crate::AccountId#error-kind-precedence).
32#[non_exhaustive]
33#[derive(Eq, Clone, Debug, PartialEq)]
34pub enum ParseErrorKind {
35 /// The Account ID is too long.
36 ///
37 /// Returned if the `AccountId` is longer than [`AccountId::MAX_LEN`](crate::AccountId::MAX_LEN).
38 TooLong,
39 /// The Account ID is too short.
40 ///
41 /// Returned if the `AccountId` is shorter than [`AccountId::MIN_LEN`](crate::AccountId::MIN_LEN).
42 TooShort,
43 /// The Account ID has a redundant separator.
44 ///
45 /// This variant would be returned if the Account ID either begins with,
46 /// ends with or has separators immediately following each other.
47 ///
48 /// Cases: `jane.`, `angela__moss`, `tyrell..wellick`
49 RedundantSeparator,
50 /// The Account ID contains an invalid character.
51 ///
52 /// This variant would be returned if the Account ID contains an upper-case character, non-separating symbol or space.
53 ///
54 /// Cases: `Ć’elicia.unc`, `user@app.com`, `Emily.unc`.
55 InvalidChar,
56}
57
58impl fmt::Display for ParseErrorKind {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 match self {
61 ParseErrorKind::TooLong => "the Account ID is too long".fmt(f),
62 ParseErrorKind::TooShort => "the Account ID is too short".fmt(f),
63 ParseErrorKind::RedundantSeparator => "the Account ID has a redundant separator".fmt(f),
64 ParseErrorKind::InvalidChar => "the Account ID contains an invalid character".fmt(f),
65 }
66 }
67}