wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Outbound helper that splits logical messages into transport fragments.
//!
//! [`Fragmenter`] exposes a small API for chunking serialized messages into
//! fixed-size fragments, tagging each fragment with a [`FragmentHeader`]. The
//! struct tracks unique [`MessageId`] values internally so callers can request
//! chunking without worrying about identifier collisions.

use std::{
    num::NonZeroUsize,
    sync::atomic::{AtomicU64, Ordering},
};

use super::{FragmentHeader, FragmentIndex, FragmentationError, MessageId};
use crate::message::Message;

/// Splits logical messages into fragment-sized frames.
#[derive(Debug)]
pub struct Fragmenter {
    /// Maximum number of payload bytes carried by one transport fragment.
    max_fragment_size: NonZeroUsize,
    /// Atomically allocated id for the next logical message.
    next_message_id: AtomicU64,
}

/// Cursor used while walking a payload into sequential fragment slices.
///
/// Keeping the byte offset and protocol index together prevents a fragment
/// from being labelled with a position that does not match its payload slice.
#[derive(Debug, Clone, Copy)]
pub(crate) struct FragmentCursor {
    /// Byte offset at which the next fragment starts.
    offset: usize,
    /// Protocol index assigned to the next fragment.
    index: FragmentIndex,
}

impl FragmentCursor {
    /// Pair a payload offset with its corresponding fragment index.
    pub(crate) const fn new(offset: usize, index: FragmentIndex) -> Self { Self { offset, index } }
}

impl Fragmenter {
    /// Create a new fragmenter that caps fragment payloads at `max_fragment_size` bytes.
    #[must_use]
    pub const fn new(max_fragment_size: NonZeroUsize) -> Self {
        Self::with_starting_id(max_fragment_size, MessageId::new(0))
    }

    /// Create a new fragmenter starting from a specific [`MessageId`].
    #[must_use]
    pub const fn with_starting_id(max_fragment_size: NonZeroUsize, start_at: MessageId) -> Self {
        Self {
            max_fragment_size,
            next_message_id: AtomicU64::new(start_at.get()),
        }
    }

    /// Return the maximum fragment payload size in bytes.
    #[must_use]
    pub const fn max_fragment_size(&self) -> NonZeroUsize { self.max_fragment_size }

    /// Generate and return the next [`MessageId`].
    ///
    /// # Panics
    ///
    /// Panics if the identifier counter reaches `u64::MAX` and overflows. Callers
    /// should provisionally treat `MessageId` values as unique for the lifetime of
    /// the fragmenter.
    #[must_use]
    pub fn next_message_id(&self) -> MessageId {
        match self
            .next_message_id
            .try_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
                current.checked_add(1)
            }) {
            Ok(previous) => MessageId::new(previous),
            Err(current) => panic!("message id counter exhausted at {current}"),
        }
    }

    /// Serialize `message` and split it into fragments.
    ///
    /// # Errors
    ///
    /// Returns [`FragmentationError::Encode`] if serialization fails, or
    /// [`FragmentationError::IndexOverflow`] if the fragment index would
    /// overflow `u32`. Slice calculations that exceed payload bounds return
    /// [`FragmentationError::SliceBounds`].
    pub fn fragment_message<M: Message>(
        &self,
        message: &M,
    ) -> Result<FragmentBatch, FragmentationError> {
        let bytes = message.to_bytes()?;
        self.fragment_bytes(bytes)
    }

    /// Split `payload` into fragments, generating a fresh [`MessageId`].
    ///
    /// # Errors
    ///
    /// Returns [`FragmentationError::IndexOverflow`] if more than
    /// `u32::MAX + 1` fragments are required, or
    /// [`FragmentationError::SliceBounds`] if slice calculation exceeds payload
    /// bounds.
    pub fn fragment_bytes(
        &self,
        payload: impl AsRef<[u8]>,
    ) -> Result<FragmentBatch, FragmentationError> {
        let message_id = self.next_message_id();
        self.fragment_with_id(message_id, payload.as_ref())
    }

    /// Split `payload` into fragments, tagging them with `message_id`.
    ///
    /// # Errors
    ///
    /// Returns [`FragmentationError::IndexOverflow`] if more than
    /// `u32::MAX + 1` fragments are required, or
    /// [`FragmentationError::SliceBounds`] if slice calculation exceeds payload
    /// bounds.
    pub fn fragment_with_id(
        &self,
        message_id: MessageId,
        payload: impl AsRef<[u8]>,
    ) -> Result<FragmentBatch, FragmentationError> {
        let fragments = self.build_fragments(message_id, payload.as_ref())?;
        Ok(FragmentBatch::new(message_id, fragments))
    }

    /// Build all fragments starting at the first protocol index.
    fn build_fragments(
        &self,
        message_id: MessageId,
        payload: &[u8],
    ) -> Result<Vec<FragmentFrame>, FragmentationError> {
        self.build_fragments_from(
            message_id,
            payload,
            FragmentCursor::new(0, FragmentIndex::zero()),
        )
    }

    /// Continue fragment construction from a cursor, validating each slice.
    fn build_fragments_from(
        &self,
        message_id: MessageId,
        payload: &[u8],
        mut cursor: FragmentCursor,
    ) -> Result<Vec<FragmentFrame>, FragmentationError> {
        let max = self.max_fragment_size.get();
        if payload.is_empty() {
            let header = FragmentHeader::new(message_id, FragmentIndex::zero(), true);
            return Ok(vec![FragmentFrame::new(header, Vec::new())]);
        }

        let total = payload.len();
        if cursor.offset > total {
            return Err(FragmentationError::SliceBounds {
                offset: cursor.offset,
                end: cursor.offset,
                total,
            });
        }
        let mut fragments = Vec::with_capacity(div_ceil(total, max));

        while cursor.offset < total {
            let end = (cursor.offset + max).min(total);
            let is_last = end == total;
            let chunk = if let Some(slice) = payload.get(cursor.offset..end) {
                slice.to_vec()
            } else {
                return Err(FragmentationError::SliceBounds {
                    offset: cursor.offset,
                    end,
                    total,
                });
            };
            fragments.push(FragmentFrame::new(
                FragmentHeader::new(message_id, cursor.index, is_last),
                chunk,
            ));

            if is_last {
                break;
            }

            cursor.offset = end;
            cursor.index = cursor
                .index
                .checked_increment()
                .ok_or(FragmentationError::IndexOverflow { last: cursor.index })?;
        }

        Ok(fragments)
    }
}

#[cfg(test)]
impl Fragmenter {
    pub(crate) fn build_fragments_from_for_tests(
        &self,
        message_id: MessageId,
        payload: &[u8],
        cursor: FragmentCursor,
    ) -> Result<Vec<FragmentFrame>, FragmentationError> {
        self.build_fragments_from(message_id, payload, cursor)
    }
}

/// Metadata and payload for a single outbound fragment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FragmentFrame {
    /// Header carrying identity, order, and completion state on the wire.
    header: FragmentHeader,
    /// Bytes belonging to this fragment, excluding the encoded header.
    payload: Vec<u8>,
}

impl FragmentFrame {
    /// Construct a new fragment frame.
    #[must_use]
    pub const fn new(header: FragmentHeader, payload: Vec<u8>) -> Self { Self { header, payload } }

    /// Return the fragment header.
    #[must_use]
    pub const fn header(&self) -> &FragmentHeader { &self.header }

    /// Return the fragment payload bytes.
    #[must_use]
    pub const fn payload(&self) -> &[u8] { self.payload.as_slice() }

    /// Consume the frame, returning its components.
    #[must_use]
    pub fn into_parts(self) -> (FragmentHeader, Vec<u8>) { (self.header, self.payload) }
}

/// Collection of fragments produced for a single logical message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FragmentBatch {
    /// Identifier shared by every frame in this batch.
    message_id: MessageId,
    /// Frames in ascending fragment-index order.
    fragments: Vec<FragmentFrame>,
}

impl FragmentBatch {
    /// Construct a non-empty batch produced for one logical message.
    fn new(message_id: MessageId, fragments: Vec<FragmentFrame>) -> Self {
        debug_assert!(!fragments.is_empty(), "fragment batches must not be empty");
        Self {
            message_id,
            fragments,
        }
    }

    /// Return the [`MessageId`] shared by all fragments.
    #[must_use]
    pub const fn message_id(&self) -> MessageId { self.message_id }

    /// Return the fragments as a slice.
    #[must_use]
    pub const fn fragments(&self) -> &[FragmentFrame] { self.fragments.as_slice() }

    /// Number of fragments in the batch.
    #[must_use]
    pub const fn len(&self) -> usize { self.fragments.len() }

    /// Return whether the batch contains no fragments.
    ///
    /// Batches produced by [`Fragmenter`] are non-empty, but this accessor
    /// reports the stored collection's actual state alongside [`Self::len`].
    #[must_use]
    pub const fn is_empty(&self) -> bool { self.fragments.is_empty() }

    /// Whether the logical message required more than one fragment.
    #[must_use]
    pub const fn is_fragmented(&self) -> bool { self.len() > 1 }

    /// Consume the batch, returning all fragments.
    #[must_use]
    pub fn into_fragments(self) -> Vec<FragmentFrame> { self.fragments }
}

impl IntoIterator for FragmentBatch {
    type Item = FragmentFrame;
    type IntoIter = std::vec::IntoIter<FragmentFrame>;

    fn into_iter(self) -> Self::IntoIter { self.fragments.into_iter() }
}

/// Compute the number of fixed-width chunks needed for a payload.
const fn div_ceil(numerator: usize, denominator: usize) -> usize { numerator.div_ceil(denominator) }

#[cfg(test)]
mod tests {
    //! White-box checks for fragment-batch collection state.

    use super::{FragmentBatch, FragmentFrame};
    use crate::fragment::{FragmentHeader, FragmentIndex, MessageId};

    #[test]
    fn is_empty_reports_the_stored_fragment_collection() {
        // Fragmenter guarantees non-empty batches; this white-box value checks
        // what the accessor reports for an empty stored collection.
        let empty = FragmentBatch {
            message_id: MessageId::new(1),
            fragments: Vec::new(),
        };
        let non_empty = FragmentBatch {
            message_id: MessageId::new(2),
            fragments: vec![FragmentFrame::new(
                FragmentHeader::new(MessageId::new(2), FragmentIndex::zero(), true),
                Vec::new(),
            )],
        };

        assert!(empty.is_empty(), "assertion failed: empty.is_empty()");
        assert!(
            !non_empty.is_empty(),
            "assertion failed: !non_empty.is_empty()"
        );
    }
}