liminal-bus-protocol 0.2.0

The liminal bus's wire protocol: frames, their codec, envelopes, version and schema negotiation, and stream multiplexing
Documentation
use super::ProtocolError;
use crate::{Frame, PASS_EXPIRED_CODE, decode, encode, encoded_len};

// Red: neither the variant nor its allocated reason code exists.
#[test]
fn pass_expired_has_a_distinct_code_and_round_trips_both_frames()
-> Result<(), Box<dyn std::error::Error>> {
    let error = ProtocolError::PassExpired {
        message: Some("credential lapsed".into()),
    };
    assert_eq!(error.reason_code(), 0x000A);
    let ordinary = ProtocolError::AuthenticationFailure {
        message: Some("lys pass admission refused: pass_expired".into()),
    };
    assert_eq!(
        ordinary.reason_code(),
        ProtocolError::AUTHENTICATION_FAILURE_CODE
    );
    for code in [
        ProtocolError::INCOMPLETE_HEADER_CODE,
        ProtocolError::TRUNCATED_PAYLOAD_CODE,
        ProtocolError::UNKNOWN_FRAME_TYPE_CODE,
        ProtocolError::INVALID_STREAM_CODE,
        ProtocolError::INVALID_STATE_TRANSITION_CODE,
        ProtocolError::AUTHENTICATION_FAILURE_CODE,
        ProtocolError::VERSION_MISMATCH_CODE,
        ProtocolError::SCHEMA_INCOMPATIBLE_CODE,
        ProtocolError::CODEC_ERROR_CODE,
    ] {
        assert_ne!(PASS_EXPIRED_CODE, code);
    }
    for frame in [
        Frame::ConnectError {
            flags: 0,
            reason_code: PASS_EXPIRED_CODE,
            message: error.message().map(str::to_owned),
        },
        Frame::ConversationError {
            flags: 0,
            stream_id: 1,
            conversation_id: 7,
            reason_code: PASS_EXPIRED_CODE,
            message: error.message().map(str::to_owned),
        },
    ] {
        let mut bytes = vec![0; encoded_len(&frame)?];
        let size = encode(&frame, &mut bytes)?;
        let (decoded, consumed) = decode(&bytes[..size])?;
        assert_eq!(decoded, frame);
        assert_eq!(consumed, size);
    }
    Ok(())
}