1#[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
35pub enum LowArea {
36#[default]
38Graphical,
39Control,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
45pub enum NeoRegion {
46#[default]
48Us,
49UaMac,
51UaPc,
53}
54
55impl NeoRegion {
56#[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#[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
78pub enum LineEndingKind {
79 #[default]
81 Lf,
82 Cr,
84 CrLf,
86 LfCr,
88 Rs,
90 Nl,
92}
93
94impl LineEndingKind {
95#[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#[must_use]
110pub const fn as_bytes(self) -> &'static [u8] {
111self.as_str().as_bytes()
112}
113
114#[must_use]
116pub const fn terminated(self) -> LineEndingFormat {
117LineEndingFormat::terminated(self)
118}
119
120#[must_use]
122pub const fn separated(self) -> LineEndingFormat {
123LineEndingFormat::separated(self)
124}
125
126#[must_use]
128pub const fn to_format_id(self) -> FormatId {
129self.terminated().to_format_id()
130}
131
132#[must_use]
134pub const fn dc_char(self) -> Option<DcChar> {
135self.to_format_id().dc_char()
136}
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
141pub enum TerminationMode {
142#[default]
144Terminated,
145Separated,
147}
148
149#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
151pub struct LineEndingFormat {
152pub kind: LineEndingKind,
154pub mode: TerminationMode,
156}
157
158impl LineEndingFormat {
159#[must_use]
161pub const fn terminated(kind: LineEndingKind) -> Self {
162Self {
163kind,
164mode: TerminationMode::Terminated,
165}
166}
167
168#[must_use]
170pub const fn separated(kind: LineEndingKind) -> Self {
171Self {
172kind,
173mode: TerminationMode::Separated,
174}
175}
176
177#[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#[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#[must_use]
218pub const fn dc_char(self) -> Option<DcChar> {
219self.to_format_id().dc_char()
220}
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
225pub enum LineEndingOption {
226#[default]
228Preserve,
229EncodingDefault,
231Specific(LineEndingFormat),
233}
234
235#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
237pub enum CharEncoding {
238Cp437 {
240low_area: LowArea,
242include_variants: bool,
244},
245Neo {
247region: NeoRegion,
249low_area: LowArea,
251},
252 Ascii,
254 MacRoman,
256 Windows1252,
258 Iso88591,
260 }
261
262impl CharEncoding {
263#[must_use]
265pub const fn cp437() -> Self {
266Self::Cp437 {
267low_area: LowArea::Graphical,
268include_variants: true,
269}
270}
271
272#[must_use]
274pub const fn cp437_control() -> Self {
275Self::Cp437 {
276low_area: LowArea::Control,
277include_variants: true,
278}
279}
280
281#[must_use]
283pub const fn neo_us() -> Self {
284Self::Neo {
285region: NeoRegion::Us,
286low_area: LowArea::Graphical,
287}
288}
289
290#[must_use]
292pub const fn neo(region: NeoRegion, low_area: LowArea) -> Self {
293Self::Neo { region, low_area }
294}
295
296 #[must_use]
298pub const fn ascii() -> Self {
299Self::Ascii
300}
301
302 #[must_use]
304pub const fn mac_roman() -> Self {
305Self::MacRoman
306}
307
308 #[must_use]
310pub const fn windows_1252() -> Self {
311Self::Windows1252
312}
313
314 #[must_use]
316pub const fn win1252() -> Self {
317Self::Windows1252
318}
319
320 #[must_use]
322pub const fn iso_88591() -> Self {
323Self::Iso88591
324}
325
326 #[must_use]
328pub const fn iso_8859_1() -> Self {
329Self::Iso88591
330}
331
332 #[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#[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#[must_use]
367pub const fn dc_char(self) -> Option<DcChar> {
368self.to_format_id().dc_char()
369}
370
371#[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#[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}