symqtt 0.1.1

Synchronous MQTT 3.1.1/5.0 client — blocking API, no async runtime
Documentation
//! Client-level error types for MQTT 5.0 operations

use symqtt_codec::QoS;
use symqtt_codec::v5::ReasonCode;

use crate::v5::SessionError;

/// Errors produced by [`Client`](super::Client) operations
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Broker rejected the CONNECT request
    #[error("connection refused: {0:?} {1:?}")]
    ConnectRefused(ReasonCode, Option<String>),
    /// Transport-level I/O failure
    #[error("transport I/O: {0}")]
    Io(#[from] std::io::Error),
    /// Failed to decode an inbound packet
    #[error("decode: {0}")]
    Decode(#[from] symqtt_codec::DecodeError),
    /// Failed to encode an outbound packet
    #[error("encode: {0}")]
    Encode(#[from] symqtt_codec::EncodeError),
    /// All packet IDs in the inflight window are occupied
    #[error("inflight window full (receive_maximum={0})")]
    WindowFull(u16),
    /// Operation timed out waiting for a response
    #[error("ack timeout")]
    Timeout,
    /// Connection is closed or was never established
    #[error("disconnected")]
    Disconnected,
    /// Broker did not respond to PINGREQ within the configured timeout
    #[error("ping timeout")]
    PingTimeout,
    /// Session state machine encountered an invalid transition
    #[error("session: {0}")]
    Session(#[from] SessionError),
    /// Topic alias is invalid (0 or exceeds maximum)
    #[error("topic alias invalid: {0}")]
    TopicAliasInvalid(u16),
    /// Packet exceeds server's maximum packet size
    #[error("packet too large: size={size} max={max}")]
    PacketTooLarge {
        /// Encoded packet size
        size: usize,
        /// Server maximum packet size
        max: u32,
    },
    /// Server sent a DISCONNECT packet
    #[error("server disconnect: {0:?}")]
    ServerDisconnect(ReasonCode),
    /// Requested QoS exceeds server's maximum
    #[error("qos not supported: requested {requested:?} max {max:?}")]
    QosNotSupported {
        /// QoS requested by the client
        requested: QoS,
        /// Maximum QoS advertised by the server
        max: QoS,
    },
    /// Server does not support retained messages
    #[error("retain not supported")]
    RetainNotSupported,
    /// Enhanced authentication failed
    #[error("auth failed: {0:?}")]
    AuthFailed(ReasonCode),
    /// Server does not support subscription identifiers
    #[error("subscription identifiers not supported")]
    SubscriptionIdNotSupported,
}

impl Clone for Error {
    fn clone(&self) -> Self {
        match self {
            Self::ConnectRefused(c, s) => Self::ConnectRefused(*c, s.clone()),
            Self::Io(e) => Self::Io(std::io::Error::new(e.kind(), e.to_string())),
            Self::Decode(e) => Self::Decode(e.clone()),
            Self::Encode(e) => Self::Encode(e.clone()),
            Self::WindowFull(n) => Self::WindowFull(*n),
            Self::Timeout => Self::Timeout,
            Self::Disconnected => Self::Disconnected,
            Self::PingTimeout => Self::PingTimeout,
            Self::Session(e) => Self::Session(e.clone()),
            Self::TopicAliasInvalid(a) => Self::TopicAliasInvalid(*a),
            Self::PacketTooLarge { size, max } => Self::PacketTooLarge { size: *size, max: *max },
            Self::ServerDisconnect(c) => Self::ServerDisconnect(*c),
            Self::QosNotSupported { requested, max } => Self::QosNotSupported {
                requested: *requested,
                max: *max,
            },
            Self::RetainNotSupported => Self::RetainNotSupported,
            Self::AuthFailed(c) => Self::AuthFailed(*c),
            Self::SubscriptionIdNotSupported => Self::SubscriptionIdNotSupported,
        }
    }
}