useragent-coinpayment 0.1.0

RFC17 CoinPayment host runtime for user-agent owned purses, receivables, cheques, invoices, and clearing state
Documentation
use thiserror::Error;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CoinPaymentErrCode {
    BalanceLow,
    Denied,
    BadCoins,
    SnipedCoins,
    PurseNotFound,
    ReceivableNotFound,
    UnsupportedChannel,
    UserAgentCapabilityUnavailable,
    Internal,
}

impl CoinPaymentErrCode {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::BalanceLow => "balanceLow",
            Self::Denied => "denied",
            Self::BadCoins => "badCoins",
            Self::SnipedCoins => "snipedCoins",
            Self::PurseNotFound => "purseNotFound",
            Self::ReceivableNotFound => "receivableNotFound",
            Self::UnsupportedChannel => "unsupportedChannel",
            Self::UserAgentCapabilityUnavailable => "userAgentCapabilityUnavailable",
            Self::Internal => "internal",
        }
    }
}

#[derive(Debug, Error)]
#[error("{code:?}: {message}")]
pub struct CoinPaymentError {
    pub code: CoinPaymentErrCode,
    pub message: String,
}

impl CoinPaymentError {
    pub fn new(code: CoinPaymentErrCode, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
        }
    }

    pub fn internal(message: impl Into<String>) -> Self {
        Self::new(CoinPaymentErrCode::Internal, message)
    }
}

pub type Result<T> = std::result::Result<T, CoinPaymentError>;