nucleus-sdk 0.1.0

Rust SDK for Nucleus vault management
Documentation
use thiserror::Error;

/// Base error type for all SDK errors
#[derive(Error, Debug)]
pub enum SdkError {
    #[error("{0}")]
    User(#[from] UserError),
    
    #[error("{0}")]
    Api(#[from] APIError),

    #[error("{0}")]
    Transaction(String),
    
    #[error("{0}")]
    Protocol(#[from] ProtocolError),
}

/// Error type for user-related errors
#[derive(Error, Debug)]
pub enum UserError {
    #[error("{0}")]
    InvalidInputs(String),
    
    #[error("{0}")]
    Other(String),
}

/// Error type for API-returned errors
#[derive(Error, Debug)]
pub struct APIError {
    message: String,
    status_code: i32,
}

impl APIError {
    pub fn new(message: String, status_code: i32) -> Self {
        Self {
            message,
            status_code,
        }
    }
}

impl std::fmt::Display for APIError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "API Error ({}): {}", self.status_code, self.message)
    }
}

/// Error type for protocol-related errors
#[derive(Error, Debug)]
pub struct ProtocolError {
    message: String,
}

impl ProtocolError {
    pub fn new(message: String) -> Self {
        Self { message }
    }
}

impl std::fmt::Display for ProtocolError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Protocol Error: {}", self.message)
    }
}

/// Represents errors that can occur during transaction operations
#[derive(Debug)]
pub struct Transaction(pub String);

impl std::fmt::Display for Transaction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Transaction error: {}", self.0)
    }
}

impl std::error::Error for Transaction {}

impl From<Transaction> for SdkError {
    fn from(err: Transaction) -> Self {
        SdkError::Transaction(err.0)
    }
}

pub type Result<T> = std::result::Result<T, SdkError>;
pub use UserError::InvalidInputs as InvalidInputsError;