ctb_formats_eite/encoding/ascii.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//! ASCII and StageL character encoding conversions for EITE.
21
22#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use anyhow::{Result, ensure};
30
31pub fn byte_from_stagel_char(s: &str) -> Result<u8> {
32 let c = s
33 .chars()
34 .next()
35 .context("empty string for byte_from_char")?;
36 let code = u32::from(c);
37 ensure!((32..127).contains(&code), "Non-ASCII supported range");
38 u8::try_from(code).context("Failed to convert code to u8")
39}
40
41pub fn stagel_char_from_byte(b: u8) -> Result<String> {
42 ensure!((0x20..=0x7E).contains(&b), "Out of visible ASCII range");
43 Ok((char::from(b)).to_string())
44}