bluetooth_core 0.0.1

Cross-platform Bluetooth LE (btleplug wrapper) with a small C ABI.
Documentation
//! Typed error enum for BLE session operations.

use std::fmt;

/// Errors that can occur during BLE session operations.
#[derive(Debug)]
pub enum Error {
    /// No Bluetooth adapter was found on the system.
    NoAdapter,
    /// An operation timed out.
    Timeout(String),
    /// The peripheral is not currently connected.
    NotConnected,
    /// A requested resource (peripheral, characteristic, etc.) was not found.
    NotFound(String),
    /// An error originating from btleplug.
    Btleplug(String),
    /// JSON serialization or deserialization failed.
    Json(serde_json::Error),
    /// A UUID string could not be parsed.
    InvalidUuid(String),
    /// Catch-all for other errors.
    Internal(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::NoAdapter => write!(f, "no Bluetooth adapter found"),
            Error::Timeout(msg) => write!(f, "{msg}"),
            Error::NotConnected => write!(f, "peripheral is not connected"),
            Error::NotFound(what) => write!(f, "{what}"),
            Error::Btleplug(msg) => write!(f, "{msg}"),
            Error::Json(e) => write!(f, "JSON error: {e}"),
            Error::InvalidUuid(msg) => write!(f, "invalid UUID: {msg}"),
            Error::Internal(msg) => write!(f, "{msg}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Json(e) => Some(e),
            _ => None,
        }
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Error::Json(e)
    }
}