casper_types/
transfer_result.rs

1use core::fmt::Debug;
2
3use crate::ApiError;
4
5/// The result of an attempt to transfer between purses.
6pub type TransferResult = Result<TransferredTo, ApiError>;
7
8/// The result of a successful transfer between purses.
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
10#[repr(i32)]
11pub enum TransferredTo {
12    /// The destination account already existed.
13    ExistingAccount = 0,
14    /// The destination account was created.
15    NewAccount = 1,
16}
17
18impl TransferredTo {
19    /// Converts an `i32` to a [`TransferResult`], where:
20    /// * `0` represents `Ok(TransferredTo::ExistingAccount)`,
21    /// * `1` represents `Ok(TransferredTo::NewAccount)`,
22    /// * all other inputs are mapped to `Err(ApiError::Transfer)`.
23    pub fn result_from(value: i32) -> TransferResult {
24        match value {
25            x if x == TransferredTo::ExistingAccount as i32 => Ok(TransferredTo::ExistingAccount),
26            x if x == TransferredTo::NewAccount as i32 => Ok(TransferredTo::NewAccount),
27            _ => Err(ApiError::Transfer),
28        }
29    }
30
31    // This conversion is not intended to be used by third party crates.
32    #[doc(hidden)]
33    pub fn i32_from(result: TransferResult) -> i32 {
34        match result {
35            Ok(transferred_to) => transferred_to as i32,
36            Err(_) => 2,
37        }
38    }
39}