Skip to main content

qubit_codec/transcode/engine/
encode_unencodable_action.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Unencodable-value actions returned by buffered encoder policy hooks.
9
10use crate::Codec;
11
12/// Unencodable-value action for a codec-backed encode hook.
13pub type EncodeUnencodableActionOf<C> = EncodeUnencodableAction<<C as Codec>::Value>;
14
15/// Action selected after a codec reports an unencodable input value.
16///
17/// Normal encodable values are handled by the encode engine itself. Hook
18/// implementations return this action only for values outside the codec's
19/// encodable domain.
20///
21/// # Type Parameters
22///
23/// - `Value`: Logical input value type accepted by the codec.
24#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
25#[non_exhaustive]
26pub enum EncodeUnencodableAction<Value> {
27    /// Reject the current input value.
28    ///
29    /// The encode engine reports [`crate::TranscodeEncodeError::Unencodable`]
30    /// at the current input index.
31    Reject,
32
33    /// Consume the current input value without producing output.
34    Skip,
35
36    /// Encode a replacement value and consume the current input value.
37    ///
38    /// The replacement must be encodable by the same codec. Returning an
39    /// unencodable replacement is a hook contract violation and panics in the
40    /// encode engine.
41    Replace {
42        /// Replacement value to encode.
43        value: Value,
44    },
45}
46
47impl<Value> EncodeUnencodableAction<Value> {
48    /// Creates a reject action.
49    ///
50    /// # Returns
51    ///
52    /// Returns [`Self::Reject`].
53    #[inline(always)]
54    #[must_use]
55    pub const fn reject() -> Self {
56        Self::Reject
57    }
58
59    /// Creates a replacement action.
60    ///
61    /// # Parameters
62    ///
63    /// - `value`: Replacement value to encode.
64    ///
65    /// # Returns
66    ///
67    /// Returns [`Self::Replace`] carrying `value`.
68    #[inline(always)]
69    #[must_use]
70    pub const fn replace(value: Value) -> Self {
71        Self::Replace { value }
72    }
73}