Skip to main content

qubit_codec_misc/
hex_codec_config.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//! Configuration shared by hexadecimal byte codecs.
9
10/// Configuration shared by [`crate::HexCodec`] encoding and decoding
11/// operations.
12///
13/// The configuration is intentionally separate from the codec value so the
14/// same formatting and decoding policy can be reused by multiple codecs.
15///
16/// # Examples
17///
18/// ```
19/// use qubit_codec_misc::HexCodec;
20/// use qubit_codec_misc::HexCodecConfig;
21///
22/// let config = HexCodecConfig::new()
23///     .with_uppercase(true)
24///     .with_prefix("0x");
25/// let codec = HexCodec::from_config(config);
26/// assert_eq!(codec.encode(&[0x1f, 0x8b]), "0x1F8B");
27/// ```
28#[must_use]
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct HexCodecConfig {
31    /// Whether to use uppercase hexadecimal digits while encoding.
32    pub(crate) uppercase: bool,
33    /// The prefix to use before the whole encoded string.
34    pub(crate) prefix: Option<String>,
35    /// The prefix to use before each encoded byte.
36    pub(crate) byte_prefix: Option<String>,
37    /// The separator to use between bytes in the encoded string.
38    pub(crate) separator: Option<String>,
39    /// Whether to ignore ASCII whitespace while decoding.
40    pub(crate) ignore_ascii_whitespace: bool,
41    /// Whether to ignore ASCII case when matching configured prefixes.
42    pub(crate) ignore_prefix_case: bool,
43}
44
45impl HexCodecConfig {
46    /// Creates a lowercase configuration without prefixes or separators.
47    #[inline]
48    pub fn new() -> Self {
49        Self {
50            uppercase: false,
51            prefix: None,
52            byte_prefix: None,
53            separator: None,
54            ignore_ascii_whitespace: false,
55            ignore_prefix_case: false,
56        }
57    }
58
59    /// Returns whether encoded digits use uppercase characters.
60    #[must_use]
61    #[inline]
62    pub fn is_uppercase(&self) -> bool {
63        self.uppercase
64    }
65
66    /// Returns the configured whole-output prefix.
67    ///
68    /// # Returns
69    /// `Some` contains the prefix written before the encoded bytes; `None`
70    /// means no whole-output prefix is configured.
71    #[inline]
72    pub fn prefix(&self) -> Option<&str> {
73        self.prefix.as_deref()
74    }
75
76    /// Returns the configured per-byte prefix.
77    ///
78    /// # Returns
79    /// `Some` contains the prefix written before each encoded byte; `None`
80    /// means no per-byte prefix is configured.
81    #[inline]
82    pub fn byte_prefix(&self) -> Option<&str> {
83        self.byte_prefix.as_deref()
84    }
85
86    /// Returns the configured separator.
87    ///
88    /// # Returns
89    /// `Some` contains the separator written between bytes; `None` means no
90    /// separator is configured.
91    #[inline]
92    pub fn separator(&self) -> Option<&str> {
93        self.separator.as_deref()
94    }
95
96    /// Returns whether ASCII whitespace is ignored while decoding.
97    #[must_use]
98    #[inline]
99    pub fn ignores_ascii_whitespace(&self) -> bool {
100        self.ignore_ascii_whitespace
101    }
102
103    /// Returns whether configured prefixes are matched case-insensitively.
104    #[must_use]
105    #[inline]
106    pub fn ignores_prefix_case(&self) -> bool {
107        self.ignore_prefix_case
108    }
109
110    /// Sets whether encoded digits should be uppercase.
111    ///
112    /// # Parameters
113    /// - `uppercase`: Whether encoding uses uppercase hexadecimal digits.
114    ///
115    /// # Returns
116    /// The updated configuration.
117    #[inline]
118    pub fn with_uppercase(mut self, uppercase: bool) -> Self {
119        self.uppercase = uppercase;
120        self
121    }
122
123    /// Sets a whole-output prefix.
124    ///
125    /// # Parameters
126    /// - `prefix`: Text written once before all encoded bytes.
127    ///
128    /// # Returns
129    /// The updated configuration.
130    #[inline]
131    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
132        self.prefix = Some(prefix.into());
133        self
134    }
135
136    /// Sets a per-byte prefix.
137    ///
138    /// # Parameters
139    /// - `prefix`: Text written before each encoded byte.
140    ///
141    /// # Returns
142    /// The updated configuration.
143    #[inline]
144    pub fn with_byte_prefix(mut self, prefix: impl Into<String>) -> Self {
145        self.byte_prefix = Some(prefix.into());
146        self
147    }
148
149    /// Sets a separator between encoded bytes.
150    ///
151    /// With a non-empty separator, the two hexadecimal digits of each byte
152    /// must be contiguous. Ignored ASCII whitespace may surround complete
153    /// byte tokens and configured separators, but does not replace a required
154    /// prefix or separator. Without a non-empty separator, ignored whitespace
155    /// may also occur between the two digits of a byte. A complete structural
156    /// token is matched before skipping additional whitespace. Non-whitespace
157    /// separators are matched exactly; a whitespace-only separator still
158    /// requires whitespace between bytes, and ignored ASCII whitespace may
159    /// consume additional adjacent whitespace. Prefix case folding does not
160    /// apply to separators.
161    ///
162    /// # Parameters
163    /// - `separator`: Text written between adjacent encoded bytes.
164    ///
165    /// # Returns
166    /// The updated configuration.
167    #[inline]
168    pub fn with_separator(mut self, separator: impl Into<String>) -> Self {
169        self.separator = Some(separator.into());
170        self
171    }
172
173    /// Sets whether ASCII whitespace is ignored while decoding.
174    ///
175    /// With a non-empty separator, the two hexadecimal digits of each byte
176    /// must be contiguous. Ignored ASCII whitespace may surround complete
177    /// byte tokens and configured separators, but does not replace a required
178    /// prefix or separator. Without a non-empty separator, ignored whitespace
179    /// may also occur between the two digits of a byte. A complete structural
180    /// token is matched before skipping additional whitespace. Non-whitespace
181    /// separators are matched exactly; a whitespace-only separator still
182    /// requires whitespace between bytes, and ignored ASCII whitespace may
183    /// consume additional adjacent whitespace. Prefix case folding does not
184    /// apply to separators.
185    ///
186    /// # Parameters
187    /// - `ignore`: Whether ASCII whitespace is skipped during decoding.
188    ///
189    /// # Returns
190    /// The updated configuration.
191    #[inline]
192    pub fn with_ignored_ascii_whitespace(mut self, ignore: bool) -> Self {
193        self.ignore_ascii_whitespace = ignore;
194        self
195    }
196
197    /// Sets whether configured prefixes are matched case-insensitively.
198    ///
199    /// # Parameters
200    /// - `ignore`: Whether prefix matching ignores ASCII letter case.
201    ///
202    /// # Returns
203    /// The updated configuration.
204    #[inline]
205    pub fn with_ignore_prefix_case(mut self, ignore: bool) -> Self {
206        self.ignore_prefix_case = ignore;
207        self
208    }
209}
210
211impl Default for HexCodecConfig {
212    #[inline]
213    fn default() -> Self {
214        Self::new()
215    }
216}