teter-common 26.11.6

Shared types and protocol definitions for teter
Documentation
//! Error types for the teter protocol.

use thiserror::Error;

/// Errors that can occur during message serialization or deserialization.
#[derive(Debug, Error)]
pub enum ProtocolError {
    /// Failed to serialize a message to JSON.
    #[error("failed to serialize message: {0}")]
    Serialization(serde_json::Error),

    /// Received text is not a message: not JSON, not a JSON object, or a
    /// `type` that is not a string.
    #[error("failed to decode message: {0}")]
    Deserialization(serde_json::Error),

    /// Message type field is missing or empty.
    #[error("missing or empty message type")]
    MissingType,

    /// Unknown message type received.
    #[error("unknown message type: {0}")]
    UnknownType(String),

    /// Payload is present but cannot be deserialized for the given type.
    #[error("failed to deserialize payload for message type '{type_}': {source}")]
    InvalidPayload {
        /// The message type whose payload failed.
        type_: String,
        /// Why it failed.
        source: serde_json::Error,
    },

    /// The `session` field is present but is not a session ID (a UUID string).
    #[error("invalid session ID in message: {source}")]
    InvalidSession {
        /// Why the field is not a session ID.
        source: serde_json::Error,
    },

    /// The `channel` field is present but is not a channel ID: 1 to 64 ASCII
    /// letters or digits.
    #[error("invalid channel in message")]
    InvalidChannel,

    /// Session ID is missing from a message that requires one.
    #[error("missing session ID in message")]
    MissingSession,

    /// Connection was closed unexpectedly.
    #[error("connection closed unexpectedly")]
    ConnectionClosed,

    /// Authentication failed.
    #[error("authentication failed: {reason}")]
    AuthFailed {
        /// Why, as the relay says.
        reason: String,
    },

    /// Session not found.
    #[error("session not found: {session_id}")]
    SessionNotFound {
        /// The session asked for.
        session_id: String,
    },

    /// Session is full (maximum frontend connections reached).
    #[error("session is full, maximum frontend connections reached")]
    SessionFull,

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

/// Result type alias for protocol operations.
pub type ProtocolResult<T> = Result<T, ProtocolError>;