wirepact 0.1.0

A teaching protocol crate: protocols are contracts, not HTTP.
Documentation
//! WirePact — a teaching protocol where the contract is explicit.
//!
//! # The idea
//!
//! TCP gives you bytes. Application protocols turn bytes into messages.
//! WirePact demonstrates two common framing styles:
//!
//! - [`LineCodec`] — newline-delimited text (`nc`-friendly)
//! - [`FrameCodec`] — length-prefixed binary frames (production-style)
//!
//! Both encodings share the same semantic [`Message`] types.

mod error;
mod frame;
mod line;
mod message;

pub use error::{Result, WirePactError};
pub use frame::{FrameCodec, DEFAULT_MAX_FRAME_SIZE};
pub use line::LineCodec;
pub use message::Message;

/// Detect whether incoming bytes look like the binary frame protocol.
///
/// Binary frames always begin with a 4-byte big-endian length where the
/// high bit is unset for sane frame sizes. Text lines usually begin with
/// ASCII letters like `H`, `P`, `E`, `G`.
pub fn looks_like_binary(chunk: &[u8]) -> bool {
    if chunk.len() < 4 {
        return false;
    }

    let len = u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
    len > 0 && len <= DEFAULT_MAX_FRAME_SIZE && chunk.len() >= 4
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shared_responses() {
        assert_eq!(
            Message::hello("sam").respond(),
            Some(Message::welcome("sam"))
        );
        assert_eq!(Message::Ping.respond(), Some(Message::Pong));
    }
}