wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Shared message-assembly expectations and matching logic.

use crate::message_assembler::{
    FrameSequence,
    MessageAssemblyError,
    MessageKey,
    MessageSeriesError,
};

/// Expected message-assembly error shape.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MessageAssemblyErrorExpectation {
    /// Expect a sequence mismatch.
    SequenceMismatch {
        /// Sequence the series expected next.
        expected: FrameSequence,
        /// Sequence carried by the received frame.
        found: FrameSequence,
    },
    /// Expect a duplicate continuation frame.
    DuplicateFrame {
        /// Message key the duplicate belongs to.
        key: MessageKey,
        /// Sequence number that was duplicated.
        sequence: FrameSequence,
    },
    /// Expect a missing-first-frame error.
    MissingFirstFrame {
        /// Message key that has no in-progress assembly.
        key: MessageKey,
    },
    /// Expect a duplicate first frame.
    DuplicateFirstFrame {
        /// Message key that already had an in-progress assembly.
        key: MessageKey,
    },
    /// Expect an over-limit assembled message.
    MessageTooLarge {
        /// Message key that exceeded the size cap.
        key: MessageKey,
    },
    /// Expect a connection-budget breach.
    ConnectionBudgetExceeded {
        /// Message key whose frame triggered the budget guard.
        key: MessageKey,
    },
    /// Expect an in-flight budget breach.
    InFlightBudgetExceeded {
        /// Message key whose frame triggered the budget guard.
        key: MessageKey,
    },
    /// Expect a declaration smaller than the first frame's own body.
    DeclaredTotalTooSmall {
        /// Message key whose declaration is unsatisfiable.
        key: MessageKey,
    },
    /// Expect a continuation that would overshoot the declared total.
    DeclaredTotalExceeded {
        /// Message key whose declaration was exceeded.
        key: MessageKey,
    },
    /// Expect a completed body that differs from the declared total.
    DeclaredTotalMismatch {
        /// Message key whose declaration was not honoured.
        key: MessageKey,
    },
}

/// Match a structured assembly error against the expected protocol failure.
pub(super) fn matches_message_error(
    err: &MessageAssemblyError,
    expected: MessageAssemblyErrorExpectation,
) -> bool {
    match expected {
        MessageAssemblyErrorExpectation::SequenceMismatch {
            expected: expected_sequence,
            found,
        } => matches_sequence_mismatch(err, expected_sequence, found),
        MessageAssemblyErrorExpectation::DuplicateFrame { key, sequence } => {
            matches_duplicate_frame(err, key, sequence)
        }
        MessageAssemblyErrorExpectation::MissingFirstFrame { key } => {
            matches_missing_first_frame(err, key)
        }
        MessageAssemblyErrorExpectation::DuplicateFirstFrame { key } => {
            matches_duplicate_first_frame(err, key)
        }
        MessageAssemblyErrorExpectation::MessageTooLarge { key } => {
            matches_message_too_large(err, key)
        }
        MessageAssemblyErrorExpectation::ConnectionBudgetExceeded { key } => {
            matches_connection_budget_exceeded(err, key)
        }
        MessageAssemblyErrorExpectation::InFlightBudgetExceeded { key } => {
            matches_in_flight_budget_exceeded(err, key)
        }
        MessageAssemblyErrorExpectation::DeclaredTotalTooSmall { key } => {
            matches_declared_total_too_small(err, key)
        }
        MessageAssemblyErrorExpectation::DeclaredTotalExceeded { key } => {
            matches_declared_total_exceeded(err, key)
        }
        MessageAssemblyErrorExpectation::DeclaredTotalMismatch { key } => {
            matches_declared_total_mismatch(err, key)
        }
    }
}

/// Match a sequence gap while retaining both expected and received indexes.
fn matches_sequence_mismatch(
    err: &MessageAssemblyError,
    expected: FrameSequence,
    found: FrameSequence,
) -> bool {
    matches!(
        err,
        MessageAssemblyError::Series(MessageSeriesError::SequenceMismatch {
            expected: actual_expected,
            found: actual_found,
        }) if *actual_expected == expected && *actual_found == found
    )
}

/// Match a repeated continuation frame for one message key.
fn matches_duplicate_frame(
    err: &MessageAssemblyError,
    key: MessageKey,
    sequence: FrameSequence,
) -> bool {
    matches!(
        err,
        MessageAssemblyError::Series(MessageSeriesError::DuplicateFrame {
            key: actual_key,
            sequence: actual_sequence,
        }) if *actual_key == key && *actual_sequence == sequence
    )
}

/// Match a continuation that arrived without a registered first frame.
fn matches_missing_first_frame(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::Series(MessageSeriesError::MissingFirstFrame {
            key: actual_key,
        }) if *actual_key == key
    )
}

/// Match a second first frame for an already active message key.
fn matches_duplicate_first_frame(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::DuplicateFirstFrame { key: actual_key }
            if *actual_key == key
    )
}

/// Match a per-message size-limit rejection.
fn matches_message_too_large(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::MessageTooLarge { key: actual_key, .. }
            if *actual_key == key
    )
}

/// Match a connection-wide aggregate budget rejection.
fn matches_connection_budget_exceeded(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::ConnectionBudgetExceeded { key: actual_key, .. }
            if *actual_key == key
    )
}

/// Match an in-flight aggregate budget rejection.
fn matches_in_flight_budget_exceeded(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::InFlightBudgetExceeded { key: actual_key, .. }
            if *actual_key == key
    )
}

/// Match a first-frame declaration smaller than the frame's own body.
fn matches_declared_total_too_small(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::DeclaredTotalTooSmall { key: actual_key, .. }
            if *actual_key == key
    )
}

/// Match a continuation that would overshoot the declared total.
fn matches_declared_total_exceeded(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::DeclaredTotalExceeded { key: actual_key, .. }
            if *actual_key == key
    )
}

/// Match a completed body whose length differs from the declared total.
fn matches_declared_total_mismatch(err: &MessageAssemblyError, key: MessageKey) -> bool {
    matches!(
        err,
        MessageAssemblyError::DeclaredTotalMismatch { key: actual_key, .. }
            if *actual_key == key
    )
}