casper_types/account/
error.rs

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