m10_sdk/account/error.rs
1//! Account index and account ID processing errors.
2
3use std::{convert::Infallible, fmt};
4
5/// Error returned by [`TryFrom`] conversions from [`AccountIndex`] to a specific account index type
6/// ([`RootAccountIndex`], [`IssuanceAccountIndex`], or [`LeafAccountIndex`]).
7///
8/// [`TryFrom`]: https://doc.rust-lang.org/std/convert/trait.TryFrom.html
9/// [`AccountIndex`]: ../index/enum.AccountIndex.html
10/// [`RootAccountIndex`]: ../index/struct.RootAccountIndex.html
11/// [`IssuanceAccountIndex`]: ../index/struct.IssuanceAccountIndex.html
12/// [`LeafAccountIndex`]: ../index/struct.LeafAccountIndex.html
13#[derive(Debug)]
14pub struct TryFromAccountIndexError;
15
16impl fmt::Display for TryFromAccountIndexError {
17 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18 write!(
19 f,
20 "`AccountIndex` does not contain an index of the target type"
21 )
22 }
23}
24
25impl std::error::Error for TryFromAccountIndexError {}
26
27/// Possible [`AccountId`] construction errors.
28///
29/// [`AccountId`]: ../id/struct.AccountId.html
30#[derive(Debug, Copy, Clone, PartialEq, Eq)]
31pub enum AccountIdError {
32 /// Missing root account ID/index.
33 MissingRoot,
34
35 /// Account chain length exceeds the maximum supported account ID depth.
36 InvalidDepth,
37
38 /// Account index exceeds the supported range for the account type.
39 IndexRange,
40
41 /// Raw account ID value has invalid non-index bit values (i.e. bits reserved for identifying
42 /// issuance account indexes and unused bits within the ID).
43 InvalidSpecialBits,
44
45 /// An issuance account ID was expected as the `self` argument for a [`child_id`] call, but a
46 /// non-issuance (leaf) account was provided.
47 ///
48 /// [`child_id`]: ../id/struct.AccountId.html#method.child_id
49 NotIssuance,
50
51 /// A leaf account ID was expected as the `self` argument for a [`sibling_id`] call, but a
52 /// non-leaf (issuance) account was provided.
53 ///
54 /// [`sibling_id`]: ../id/struct.AccountId.html#method.sibling_id
55 NotLeaf,
56
57 /// The input slice does not have the correct length for [`try_from_be_slice`] conversions.
58 ///
59 /// [`try_from_be_slice`]: ../id/struct.AccountId.html#method.try_from_be_slice
60 InvalidLen,
61}
62
63impl fmt::Display for AccountIdError {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 match self {
66 Self::MissingRoot => write!(f, "missing root account ID/index"),
67 Self::InvalidDepth => write!(f, "maximum account ID depth exceeded"),
68 Self::IndexRange => write!(
69 f,
70 "account index exceeds supported range for the account type"
71 ),
72 Self::InvalidSpecialBits => {
73 write!(f, "raw account ID value has invalid special bit values")
74 }
75 Self::NotIssuance => write!(f, "expected issuance account for the base account ID"),
76 Self::NotLeaf => write!(f, "expected leaf account for the base account ID"),
77 Self::InvalidLen => write!(f, "invalid length for raw ID byte slice"),
78 }
79 }
80}
81
82impl std::error::Error for AccountIdError {}
83
84impl From<Infallible> for AccountIdError {
85 #[inline]
86 fn from(_error: Infallible) -> Self {
87 unreachable!()
88 }
89}