casper_types/account/
error.rs

1use core::{
2    array::TryFromSliceError,
3    convert::TryFrom,
4    fmt::{self, Display, Formatter},
5};
6
7// This error type is not intended to be used by third party crates.
8#[doc(hidden)]
9#[derive(Debug, Eq, PartialEq)]
10pub struct TryFromIntError(pub(super) ());
11
12/// Error returned when decoding an `AccountHash` from a formatted string.
13#[derive(Debug)]
14#[non_exhaustive]
15pub enum FromStrError {
16    /// The prefix is invalid.
17    InvalidPrefix,
18    /// The hash is not valid hex.
19    Hex(base16::DecodeError),
20    /// The hash is the wrong length.
21    Hash(TryFromSliceError),
22}
23
24impl From<base16::DecodeError> for FromStrError {
25    fn from(error: base16::DecodeError) -> Self {
26        FromStrError::Hex(error)
27    }
28}
29
30impl From<TryFromSliceError> for FromStrError {
31    fn from(error: TryFromSliceError) -> Self {
32        FromStrError::Hash(error)
33    }
34}
35
36impl Display for FromStrError {
37    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
38        match self {
39            FromStrError::InvalidPrefix => write!(f, "prefix is not 'account-hash-'"),
40            FromStrError::Hex(error) => {
41                write!(f, "failed to decode address portion from hex: {}", error)
42            }
43            FromStrError::Hash(error) => write!(f, "address portion is wrong length: {}", error),
44        }
45    }
46}
47
48/// Errors that can occur while changing action thresholds (i.e. the total
49/// [`Weight`](super::Weight)s of signing [`AccountHash`](super::AccountHash)s required to perform
50/// various actions) on an account.
51#[repr(i32)]
52#[derive(Debug, PartialEq, Eq, Copy, Clone)]
53#[non_exhaustive]
54pub enum SetThresholdFailure {
55    /// Setting the key-management threshold to a value lower than the deployment threshold is
56    /// disallowed.
57    KeyManagementThreshold = 1,
58    /// Setting the deployment threshold to a value greater than any other threshold is disallowed.
59    DeploymentThreshold = 2,
60    /// Caller doesn't have sufficient permissions to set new thresholds.
61    PermissionDeniedError = 3,
62    /// Setting a threshold to a value greater than the total weight of associated keys is
63    /// disallowed.
64    InsufficientTotalWeight = 4,
65}
66
67// This conversion is not intended to be used by third party crates.
68#[doc(hidden)]
69impl TryFrom<i32> for SetThresholdFailure {
70    type Error = TryFromIntError;
71
72    fn try_from(value: i32) -> Result<Self, Self::Error> {
73        match value {
74            d if d == SetThresholdFailure::KeyManagementThreshold as i32 => {
75                Ok(SetThresholdFailure::KeyManagementThreshold)
76            }
77            d if d == SetThresholdFailure::DeploymentThreshold as i32 => {
78                Ok(SetThresholdFailure::DeploymentThreshold)
79            }
80            d if d == SetThresholdFailure::PermissionDeniedError as i32 => {
81                Ok(SetThresholdFailure::PermissionDeniedError)
82            }
83            d if d == SetThresholdFailure::InsufficientTotalWeight as i32 => {
84                Ok(SetThresholdFailure::InsufficientTotalWeight)
85            }
86            _ => Err(TryFromIntError(())),
87        }
88    }
89}
90
91impl Display for SetThresholdFailure {
92    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
93        match self {
94            SetThresholdFailure::KeyManagementThreshold => formatter
95                .write_str("New threshold should be greater than or equal to deployment threshold"),
96            SetThresholdFailure::DeploymentThreshold => formatter.write_str(
97                "New threshold should be lower than or equal to key management threshold",
98            ),
99            SetThresholdFailure::PermissionDeniedError => formatter
100                .write_str("Unable to set action threshold due to insufficient permissions"),
101            SetThresholdFailure::InsufficientTotalWeight => formatter.write_str(
102                "New threshold should be lower or equal than total weight of associated keys",
103            ),
104        }
105    }
106}
107
108/// Associated error type of `TryFrom<&[u8]>` for [`AccountHash`](super::AccountHash).
109#[derive(Debug)]
110pub struct TryFromSliceForAccountHashError(());