//! Error types for the sync layer.

use thiserror::Error;

/// Errors that can occur during sync operations.
#[derive(Debug, Error)]
pub enum SyncError {
    /// Storage operation failed.
    #[error("Storage error: {0}")]
    Storage(String),

    /// RPC communication failed.
    #[error("RPC error: {0}")]
    Rpc(#[from] stemedb_rpc::RpcError),

    /// Merkle tree operation failed.
    #[error("Merkle error: {0}")]
    Merkle(String),

    /// Serialization/deserialization failed.
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Configuration error.
    #[error("Configuration error: {0}")]
    Config(String),

    /// Internal consistency error.
    #[error("Internal error: {0}")]
    Internal(String),
}

impl From<stemedb_storage::error::StorageError> for SyncError {
    fn from(err: stemedb_storage::error::StorageError) -> Self {
        SyncError::Storage(err.to_string())
    }
}

impl From<stemedb_merkle::TreeError> for SyncError {
    fn from(err: stemedb_merkle::TreeError) -> Self {
        SyncError::Merkle(err.to_string())
    }
}

impl From<stemedb_merkle::SerializeError> for SyncError {
    fn from(err: stemedb_merkle::SerializeError) -> Self {
        SyncError::Serialization(err.to_string())
    }
}

/// Result type for sync operations.
pub type Result<T> = std::result::Result<T, SyncError>;
