ctb_formats_utilities/
encoding.generated.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Character encoding definitions and settings for table-driven single-byte encodings.
21//! @generated by ctb-build-support::encoding_codegen from format category data tables.
22//! Do not edit by hand.
23
24#[allow(
25unused_imports,
26clippy::wildcard_imports,
27reason = "Standard workspace module prelude"
28)]
29use crate::utilities::*;
30use ctb_utilities::FormatId;
31use ctb_utilities::dc_char::DcChar;
32
33/// Mode for handling low character codes (0x00..=0x1F) in single-byte encodings.
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
35pub enum LowArea {
36/// Graphical symbols (e.g. Neo graphical symbols, CP437 dingbats).
37#[default]
38Graphical,
39/// Control characters (standard C0 control codes).
40Control,
41}
42
43/// Regional character layout for Neo encodings.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
45pub enum NeoRegion {
46/// United States layout.
47#[default]
48Us,
49/// Ukrainian Macintosh layout.
50UaMac,
51/// Ukrainian PC layout.
52UaPc,
53}
54
55impl NeoRegion {
56/// Returns the dedicated `FormatId` for this Neo regional variant and low-area mode.
57#[must_use]
58pub const fn to_format_id(self, low_area: LowArea) -> FormatId {
59match (self, low_area) {
60(Self::Us, LowArea::Graphical) => FormatId::AlphaSmartNeoLowGrUs,
61(Self::UaMac, LowArea::Graphical) => FormatId::AlphaSmartNeoLowGrUaMac,
62(Self::UaPc, LowArea::Graphical) => FormatId::AlphaSmartNeoLowGrUaPc,
63(Self::Us, LowArea::Control) => FormatId::AlphaSmartNeoLowCtlUs,
64(Self::UaMac, LowArea::Control) => FormatId::AlphaSmartNeoLowCtlUaMac,
65(Self::UaPc, LowArea::Control) => FormatId::AlphaSmartNeoLowCtlUaPc,
66}
67}
68
69/// Returns the dedicated `DcChar` constant for this Neo variant if known.
70#[must_use]
71pub const fn dc_char(self, low_area: LowArea) -> Option<DcChar> {
72self.to_format_id(low_area).dc_char()
73}
74}
75
76/// Line ending delimiter pattern.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
78pub enum LineEndingKind {
79    /// POSIX / Unix newline (`\n`, LF, 0x0A).
80    #[default]
81    Lf,
82    /// Classic Macintosh newline (`\r`, CR, 0x0D).
83    Cr,
84    /// Windows / DOS newline (`\r\n`, CRLF, 0x0D 0x0A).
85    CrLf,
86    /// Acorn / RISC OS newline (`\n\r`, LFCR, 0x0A 0x0D).
87    LfCr,
88    /// QNX traditional Record Separator (`\x1E`, RS, 0x1E).
89    Rs,
90    /// IBM / EBCDIC Next Line (`\u{0085}`, NEL).
91    Nl,
92}
93
94impl LineEndingKind {
95/// Returns the string representation of this line ending delimiter.
96#[must_use]
97pub const fn as_str(self) -> &'static str {
98match self {
99                    Self::Lf => "\n",
100                    Self::Cr => "\r",
101                    Self::CrLf => "\r\n",
102                    Self::LfCr => "\n\r",
103                    Self::Rs => "\x1E",
104                    Self::Nl => "\u{0085}",
105                }
106}
107
108/// Returns the byte sequence for this line ending in UTF-8.
109#[must_use]
110pub const fn as_bytes(self) -> &'static [u8] {
111self.as_str().as_bytes()
112}
113
114/// Creates a `LineEndingFormat` with terminated mode for this delimiter kind.
115#[must_use]
116pub const fn terminated(self) -> LineEndingFormat {
117LineEndingFormat::terminated(self)
118}
119
120/// Creates a `LineEndingFormat` with separated mode for this delimiter kind.
121#[must_use]
122pub const fn separated(self) -> LineEndingFormat {
123LineEndingFormat::separated(self)
124}
125
126/// Maps this terminated line ending kind to its corresponding `FormatId`.
127#[must_use]
128pub const fn to_format_id(self) -> FormatId {
129self.terminated().to_format_id()
130}
131
132/// Returns the dedicated `DcChar` for this line ending in terminated mode.
133#[must_use]
134pub const fn dc_char(self) -> Option<DcChar> {
135self.to_format_id().dc_char()
136}
137}
138
139/// Mode defining whether newlines terminate every line or only separate lines.
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
141pub enum TerminationMode {
142/// Every line including the last line is terminated by the newline sequence.
143#[default]
144Terminated,
145/// Newline sequences only appear between lines; no trailing terminator on final line.
146Separated,
147}
148
149/// Full specification of line ending style and termination mode.
150#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
151pub struct LineEndingFormat {
152/// The line ending delimiter pattern.
153pub kind: LineEndingKind,
154/// Whether the delimiter terminates all lines or only separates them.
155pub mode: TerminationMode,
156}
157
158impl LineEndingFormat {
159/// Creates a new `LineEndingFormat` with terminated mode.
160#[must_use]
161pub const fn terminated(kind: LineEndingKind) -> Self {
162Self {
163kind,
164mode: TerminationMode::Terminated,
165}
166}
167
168/// Creates a new `LineEndingFormat` with separated mode.
169#[must_use]
170pub const fn separated(kind: LineEndingKind) -> Self {
171Self {
172kind,
173mode: TerminationMode::Separated,
174}
175}
176
177/// Maps this line ending format to its canonical `FormatId`.
178#[must_use]
179pub const fn to_format_id(self) -> FormatId {
180match (self.mode, self.kind) {
181                    (TerminationMode::Terminated, LineEndingKind::Lf) => FormatId::LineEndingLf,
182                    (TerminationMode::Terminated, LineEndingKind::Cr) => FormatId::LineEndingCr,
183                    (TerminationMode::Terminated, LineEndingKind::CrLf) => FormatId::LineEndingCrLf,
184                    (TerminationMode::Terminated, LineEndingKind::LfCr) => FormatId::LineEndingLfCr,
185                    (TerminationMode::Terminated, LineEndingKind::Rs) => FormatId::LineEndingRs,
186                    (TerminationMode::Terminated, LineEndingKind::Nl) => FormatId::LineEndingNl,
187                    (TerminationMode::Separated, LineEndingKind::Lf) => FormatId::LineSeparatorLf,
188                    (TerminationMode::Separated, LineEndingKind::Cr) => FormatId::LineSeparatorCr,
189                    (TerminationMode::Separated, LineEndingKind::CrLf) => FormatId::LineSeparatorCrLf,
190                    (TerminationMode::Separated, LineEndingKind::LfCr) => FormatId::LineSeparatorLfCr,
191                    (TerminationMode::Separated, LineEndingKind::Rs) => FormatId::LineSeparatorRs,
192                    (TerminationMode::Separated, LineEndingKind::Nl) => FormatId::LineSeparatorNl,
193                }
194}
195
196/// Converts a `FormatId` to the corresponding `LineEndingFormat` if it represents a line ending.
197#[must_use]
198pub const fn from_format_id(id: FormatId) -> Option<Self> {
199match id {
200                    FormatId::LineEndingLf => Some(Self::terminated(LineEndingKind::Lf)),
201                    FormatId::LineEndingCr => Some(Self::terminated(LineEndingKind::Cr)),
202                    FormatId::LineEndingCrLf => Some(Self::terminated(LineEndingKind::CrLf)),
203                    FormatId::LineEndingLfCr => Some(Self::terminated(LineEndingKind::LfCr)),
204                    FormatId::LineEndingRs => Some(Self::terminated(LineEndingKind::Rs)),
205                    FormatId::LineEndingNl => Some(Self::terminated(LineEndingKind::Nl)),
206                    FormatId::LineSeparatorLf => Some(Self::separated(LineEndingKind::Lf)),
207                    FormatId::LineSeparatorCr => Some(Self::separated(LineEndingKind::Cr)),
208                    FormatId::LineSeparatorCrLf => Some(Self::separated(LineEndingKind::CrLf)),
209                    FormatId::LineSeparatorLfCr => Some(Self::separated(LineEndingKind::LfCr)),
210                    FormatId::LineSeparatorRs => Some(Self::separated(LineEndingKind::Rs)),
211                    FormatId::LineSeparatorNl => Some(Self::separated(LineEndingKind::Nl)),
212                    _ => None,
213}
214}
215
216/// Returns the dedicated `DcChar` for this line ending format if known.
217#[must_use]
218pub const fn dc_char(self) -> Option<DcChar> {
219self.to_format_id().dc_char()
220}
221}
222
223/// Option controlling line ending conversion during encoding, decoding, and transcoding.
224#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
225pub enum LineEndingOption {
226/// Keep line endings as they are (no conversion / pure character conversion).
227#[default]
228Preserve,
229/// Convert to the idiomatic line ending for the target character encoding.
230EncodingDefault,
231/// Convert to a specific line ending format.
232Specific(LineEndingFormat),
233}
234
235/// Structured single-byte character encoding settings.
236#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
237pub enum CharEncoding {
238/// Code Page 437 (DOS Latin US).
239Cp437 {
240/// Low-area mode (graphical dingbats vs control codes).
241low_area: LowArea,
242/// Whether alternative Unicode variants are included in reverse encoding.
243include_variants: bool,
244},
245/// Neo character encoding.
246Neo {
247/// Regional layout variant.
248region: NeoRegion,
249/// Low-area mode.
250low_area: LowArea,
251},
252            /// ASCII.
253            Ascii,
254            /// MacRoman.
255            MacRoman,
256            /// Windows-1252 ("ANSI").
257            Windows1252,
258            /// ISO 8859-1.
259            Iso88591,
260        }
261
262impl CharEncoding {
263/// Default CP437 encoding with graphical dingbats and variant aliases.
264#[must_use]
265pub const fn cp437() -> Self {
266Self::Cp437 {
267low_area: LowArea::Graphical,
268include_variants: true,
269}
270}
271
272/// CP437 encoding with control characters in low area.
273#[must_use]
274pub const fn cp437_control() -> Self {
275Self::Cp437 {
276low_area: LowArea::Control,
277include_variants: true,
278}
279}
280
281/// Standard Neo US layout with graphical low area.
282#[must_use]
283pub const fn neo_us() -> Self {
284Self::Neo {
285region: NeoRegion::Us,
286low_area: LowArea::Graphical,
287}
288}
289
290/// Neo encoding with custom region and low area.
291#[must_use]
292pub const fn neo(region: NeoRegion, low_area: LowArea) -> Self {
293Self::Neo { region, low_area }
294}
295
296            /// ASCII.
297#[must_use]
298pub const fn ascii() -> Self {
299Self::Ascii
300}
301
302            /// MacRoman.
303#[must_use]
304pub const fn mac_roman() -> Self {
305Self::MacRoman
306}
307
308            /// Windows-1252 ("ANSI").
309#[must_use]
310pub const fn windows_1252() -> Self {
311Self::Windows1252
312}
313
314            /// Windows-1252 ("ANSI") encoding alias.
315#[must_use]
316pub const fn win1252() -> Self {
317Self::Windows1252
318}
319
320            /// ISO 8859-1.
321#[must_use]
322pub const fn iso_88591() -> Self {
323Self::Iso88591
324}
325
326            /// ISO 8859-1 encoding alias.
327#[must_use]
328pub const fn iso_8859_1() -> Self {
329Self::Iso88591
330}
331
332            /// Maps this character encoding to its canonical `FormatId`.
333#[must_use]
334pub const fn to_format_id(self) -> FormatId {
335match self {
336Self::Cp437 { .. } => FormatId::Cp437,
337Self::Neo { region, low_area } => region.to_format_id(low_area),
338                    Self::Ascii => FormatId::Ascii,
339                    Self::MacRoman => FormatId::MacRoman,
340                    Self::Windows1252 => FormatId::Win1252,
341                    Self::Iso88591 => FormatId::Iso88591,
342                }
343}
344
345/// Converts a `FormatId` to the corresponding `CharEncoding` if it represents an encoding.
346#[must_use]
347pub const fn from_format_id(id: FormatId) -> Option<Self> {
348match id {
349FormatId::Cp437 => Some(Self::cp437()),
350FormatId::AlphaSmartNeo => Some(Self::neo_us()),
351FormatId::AlphaSmartNeoLowGrUs => Some(Self::neo(NeoRegion::Us, LowArea::Graphical)),
352FormatId::AlphaSmartNeoLowGrUaMac => Some(Self::neo(NeoRegion::UaMac, LowArea::Graphical)),
353FormatId::AlphaSmartNeoLowGrUaPc => Some(Self::neo(NeoRegion::UaPc, LowArea::Graphical)),
354FormatId::AlphaSmartNeoLowCtlUs => Some(Self::neo(NeoRegion::Us, LowArea::Control)),
355FormatId::AlphaSmartNeoLowCtlUaMac => Some(Self::neo(NeoRegion::UaMac, LowArea::Control)),
356FormatId::AlphaSmartNeoLowCtlUaPc => Some(Self::neo(NeoRegion::UaPc, LowArea::Control)),
357                    FormatId::Ascii => Some(Self::Ascii),
358                    FormatId::MacRoman => Some(Self::MacRoman),
359                    FormatId::Win1252 => Some(Self::Windows1252),
360                    FormatId::Iso88591 => Some(Self::Iso88591),
361                    _ => None,
362}
363}
364
365/// Returns the dedicated `DcChar` constant for this character encoding if known.
366#[must_use]
367pub const fn dc_char(self) -> Option<DcChar> {
368self.to_format_id().dc_char()
369}
370
371/// Returns the idiomatic / natural default line ending for this character encoding.
372#[must_use]
373pub const fn default_line_ending(self) -> LineEndingKind {
374match self {
375                    Self::Cp437 { .. } => LineEndingKind::CrLf,
376                    Self::Neo { .. } => LineEndingKind::Cr,
377                    Self::Ascii => LineEndingKind::Lf,
378                    Self::MacRoman => LineEndingKind::Cr,
379                    Self::Windows1252 => LineEndingKind::CrLf,
380                    Self::Iso88591 => LineEndingKind::Lf,
381                }
382}
383
384/// Checks whether the specified line ending can be encoded in this character encoding.
385#[must_use]
386pub const fn supports_line_ending(self, ending: LineEndingKind) -> bool {
387match ending {
388LineEndingKind::Lf
389| LineEndingKind::Cr
390| LineEndingKind::CrLf
391| LineEndingKind::LfCr => true,
392LineEndingKind::Rs => match self {
393                        Self::Ascii | Self::MacRoman | Self::Windows1252 | Self::Iso88591 => true,
394                        Self::Cp437 { low_area, .. } | Self::Neo { low_area, .. } => {
395matches!(low_area, LowArea::Control)
396}
397},
398LineEndingKind::Nl => false,
399}
400}
401}
402
403impl Default for CharEncoding {
404fn default() -> Self {
405Self::cp437()
406}
407}