Skip to main content

qubit_codec/transcode/engine/
encode_context.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//! Read-only policy context for one buffered encode attempt.
9
10/// Read-only context supplied to an encode policy hook.
11///
12/// The engine retains ownership of the mutable output slice. Hooks can inspect
13/// the current value and cursor/capacity metadata, then return an
14/// [`crate::engine::EncodeUnencodableAction`] without being able to mutate
15/// output behind the engine's progress accounting.
16///
17/// In a codec-backed converter, values emitted by source decode reset or
18/// finish have no source-unit position and use placeholder index 0. Ordinary
19/// decoded values retain their index in the input slice supplied to the
20/// decode call that produced them. A pending value keeps that index across
21/// calls; it is not rebased to a later input slice and is not a whole-stream
22/// offset. The retained index may be outside the bounds of a later input
23/// slice.
24///
25/// # Type Parameters
26///
27/// - `Value`: Logical input value type.
28///
29/// # Examples
30///
31/// ```
32/// use qubit_codec::engine::EncodeContext;
33///
34/// let value = 7_u8;
35/// let context = EncodeContext::new(&value, 0, 0, 1);
36/// assert_eq!(context.input_value(), &7);
37/// ```
38#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
39pub struct EncodeContext<'a, Value> {
40    /// Value borrowed for the lifetime of this policy context.
41    input_value: &'a Value,
42    /// Input position of the borrowed value, or placeholder 0 for a converter
43    /// lifecycle-generated value.
44    input_index: usize,
45    /// Absolute output cursor at the start of the encode attempt.
46    output_index: usize,
47    /// Number of writable output units remaining at the cursor.
48    available_output: usize,
49}
50
51impl<'a, Value> EncodeContext<'a, Value> {
52    /// Creates an encode policy context.
53    ///
54    /// # Parameters
55    ///
56    /// - `input_value`: Borrowed input value being encoded.
57    /// - `input_index`: Input index of `input_value`; converter lifecycle-
58    ///   generated values use placeholder 0 because they have no source-unit
59    ///   position.
60    /// - `output_index`: Absolute output index where writing begins.
61    /// - `available_output`: Output units writable from `output_index`.
62    ///
63    /// # Returns
64    ///
65    /// Returns a read-only encode context.
66    #[must_use]
67    #[inline]
68    pub const fn new(input_value: &'a Value, input_index: usize, output_index: usize, available_output: usize) -> Self {
69        Self {
70            input_value,
71            input_index,
72            output_index,
73            available_output,
74        }
75    }
76
77    /// Returns the input value being encoded.
78    ///
79    /// # Returns
80    ///
81    /// The input value borrowed through this context without allocation.
82    #[must_use = "the borrowed input value should be inspected"]
83    #[inline]
84    pub const fn input_value(&self) -> &Value {
85        self.input_value
86    }
87
88    /// Returns the input index of the current value.
89    ///
90    /// In a codec-backed converter, lifecycle-generated values use placeholder
91    /// index 0 because they have no source-unit position. Ordinary decoded
92    /// values retain their index in the input slice supplied to the decode
93    /// call that produced them. A pending value keeps that index across calls;
94    /// it is not rebased to a later input slice and is not a whole-stream
95    /// offset. The retained index may be outside the bounds of a later input
96    /// slice.
97    ///
98    /// # Returns
99    ///
100    /// The value's input index, including placeholder 0 for converter
101    /// lifecycle-generated values.
102    #[must_use]
103    #[inline]
104    pub const fn input_index(&self) -> usize {
105        self.input_index
106    }
107
108    /// Returns the absolute output index where writing begins.
109    ///
110    /// # Returns
111    ///
112    /// The starting output cursor for this encode attempt.
113    #[must_use]
114    #[inline]
115    pub const fn output_index(&self) -> usize {
116        self.output_index
117    }
118
119    /// Returns output capacity visible to this encode attempt.
120    ///
121    /// # Returns
122    ///
123    /// The number of output units available from the starting cursor.
124    #[must_use]
125    #[inline]
126    pub const fn available_output(&self) -> usize {
127        self.available_output
128    }
129}