qubit-codec 0.17.4

Core codec traits and buffer conversion primitives for Rust
Documentation
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Invalid-decode actions returned by buffered decoder policy hooks.

use core::num::NonZeroUsize;

/// Action selected after a codec reports invalid encoded input.
///
/// While the stream is open, incomplete input is not an invalid-input policy
/// action. Codecs report it with [`crate::DecodeFailure::Incomplete`], and the
/// decode engine converts it into [`crate::TranscodeStatus::NeedInput`]. After
/// upstream EOF, [`crate::Transcoder::transcode_eof`] asks the codec to resolve
/// a trailing incomplete sequence through [`crate::Codec::decode_eof`]; if it
/// remains incomplete, the engine calls
/// [`crate::engine::DecodeIncompleteAction`] through
/// [`crate::engine::TranscodeDecodeHooks::handle_incomplete_decode`].
///
/// # Type Parameters
///
/// - `Value`: Decoded output value type.
///
/// # Examples
///
/// ```
/// use core::num::NonZeroUsize;
///
/// use qubit_codec::engine::DecodeInvalidAction;
///
/// let action = DecodeInvalidAction::<u8>::Skip {
///     consumed: NonZeroUsize::new(1).unwrap(),
/// };
/// assert!(matches!(action, DecodeInvalidAction::Skip { .. }));
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
#[must_use]
pub enum DecodeInvalidAction<Value> {
    /// Reject the current invalid input.
    ///
    /// The decode engine reports the original codec decode error as
    /// [`crate::TranscodeDecodeError::Domain`] at the current input index.
    Reject,

    /// Consume invalid input without producing output.
    ///
    /// `consumed` must not exceed the hook context's available input count.
    /// Over-consuming is a hook contract violation and panics in the engine.
    Skip {
        /// Source units to consume.
        consumed: NonZeroUsize,
    },

    /// Produce one replacement value and consume source units.
    ///
    /// `consumed` must not exceed the hook context's available input count.
    /// Over-consuming is a hook contract violation and panics in the engine.
    Emit {
        /// Value to write to the output buffer.
        value: Value,
        /// Source units to consume.
        consumed: NonZeroUsize,
    },
}

impl<Value> DecodeInvalidAction<Value> {
    /// Validates a policy-reported consumed source-unit count against available
    /// input.
    ///
    /// # Parameters
    ///
    /// - `consumed`: Source units requested by the concrete policy.
    /// - `available`: Source units visible from the current decode cursor.
    ///
    /// # Returns
    ///
    /// Returns the validated non-zero consumed count.
    ///
    /// # Panics
    ///
    /// Panics when `available == 0` or when `consumed > available`.
    #[inline]
    #[must_use]
    pub(super) fn bound_consumed(consumed: NonZeroUsize, available: usize) -> NonZeroUsize {
        assert!(available > 0, "DecodeInvalidAction cannot consume empty input",);
        assert!(
            consumed.get() <= available,
            "DecodeInvalidAction consumed units must not exceed available input",
        );
        consumed
    }
}