liminal-protocol 0.13.1

Shared participant-lifecycle protocol types for liminal
Documentation
//! Pins the server-layer reason codes' wire values and their place in the band map.

use super::{
    CHANNEL_NOT_REGISTERED_CODE, CHANNEL_QUIESCED_CODE, CONSOLE_NAMESPACE_REFUSED_CODE,
    CONSOLE_TAP_LIMIT_REFUSED_CODE, CONSOLE_TAP_TARGET_REFUSED_CODE, PUBLISH_UNRESOLVED_CODE,
};

/// Ceiling of the protocol layer's band, the neighbour immediately below.
const PROTOCOL_BAND_CEILING: u16 = 0x00FF;
/// First value of the server layer's reserved band.
const SERVER_BAND_FIRST: u16 = 0x0100;
/// Last value of the server layer's reserved band.
const SERVER_BAND_LAST: u16 = 0x01FF;
/// The server's undifferentiated error, the far neighbour.
const UNDIFFERENTIATED_SERVER_ERROR: u16 = 0xFFFF;

#[test]
fn roster_reason_codes_hold_their_wire_values() {
    assert_eq!(CHANNEL_NOT_REGISTERED_CODE, 0x0101);
    assert_eq!(CHANNEL_QUIESCED_CODE, 0x0102);
    assert_ne!(CHANNEL_NOT_REGISTERED_CODE, CHANNEL_QUIESCED_CODE);
}

#[test]
fn roster_reason_codes_lie_inside_the_reserved_server_band() {
    for code in [CHANNEL_NOT_REGISTERED_CODE, CHANNEL_QUIESCED_CODE] {
        assert!((SERVER_BAND_FIRST..=SERVER_BAND_LAST).contains(&code));
    }
}

#[test]
fn roster_reason_codes_collide_with_neither_neighbouring_band() {
    for code in [CHANNEL_NOT_REGISTERED_CODE, CHANNEL_QUIESCED_CODE] {
        assert!(code > PROTOCOL_BAND_CEILING);
        assert_ne!(code, UNDIFFERENTIATED_SERVER_ERROR);
    }
}

#[test]
fn publish_unresolved_code_is_the_next_free_server_band_value() {
    assert_eq!(PUBLISH_UNRESOLVED_CODE, 0x0103);
    assert!((SERVER_BAND_FIRST..=SERVER_BAND_LAST).contains(&PUBLISH_UNRESOLVED_CODE));
    assert_ne!(PUBLISH_UNRESOLVED_CODE, CHANNEL_NOT_REGISTERED_CODE);
    assert_ne!(PUBLISH_UNRESOLVED_CODE, CHANNEL_QUIESCED_CODE);
    assert_ne!(PUBLISH_UNRESOLVED_CODE, UNDIFFERENTIATED_SERVER_ERROR);
}

#[test]
fn console_namespace_code_is_the_next_free_server_band_value() {
    assert_eq!(CONSOLE_NAMESPACE_REFUSED_CODE, 0x0104);
    assert!((SERVER_BAND_FIRST..=SERVER_BAND_LAST).contains(&CONSOLE_NAMESPACE_REFUSED_CODE));
    for other in [
        CHANNEL_NOT_REGISTERED_CODE,
        CHANNEL_QUIESCED_CODE,
        PUBLISH_UNRESOLVED_CODE,
        UNDIFFERENTIATED_SERVER_ERROR,
    ] {
        assert_ne!(CONSOLE_NAMESPACE_REFUSED_CODE, other);
    }
}

#[test]
fn the_console_tap_codes_are_the_next_free_server_band_values() {
    assert_eq!(CONSOLE_TAP_TARGET_REFUSED_CODE, 0x0105);
    assert_eq!(CONSOLE_TAP_LIMIT_REFUSED_CODE, 0x0106);
    let minted = [
        CHANNEL_NOT_REGISTERED_CODE,
        CHANNEL_QUIESCED_CODE,
        PUBLISH_UNRESOLVED_CODE,
        CONSOLE_NAMESPACE_REFUSED_CODE,
        CONSOLE_TAP_TARGET_REFUSED_CODE,
        CONSOLE_TAP_LIMIT_REFUSED_CODE,
    ];
    for (index, code) in minted.iter().enumerate() {
        assert!((SERVER_BAND_FIRST..=SERVER_BAND_LAST).contains(code));
        assert_ne!(*code, UNDIFFERENTIATED_SERVER_ERROR);
        assert!(
            minted[index + 1..].iter().all(|other| other != code),
            "{code:#06x} is minted twice"
        );
    }
}