ctb_formats_dceutils/
tables.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 translation tables and lookup indices for DCE formats.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use std::collections::HashMap;
30use std::sync::OnceLock;
31
32pub struct Tables {
33    pub dc_map_unicode_lossy: HashMap<String, String>,
34    pub dc_map_send_unicode: HashMap<String, String>,
35    pub base64_to_dc: HashMap<String, String>,
36    pub dc_to_base64: HashMap<String, String>,
37    pub cdce_html_legacy: HashMap<String, String>,
38    pub dce_versions: Vec<String>,
39    pub dc_map_send_dce3_0a: HashMap<String, String>,
40    pub dc_map_dce3_0a_core: HashMap<String, String>,
41    pub dce3_0a_core: Vec<String>,
42    pub dc_map_send_dce3_01a_all: HashMap<String, String>,
43    pub dc_map_dce3_01a_core: HashMap<String, String>,
44    pub dc_map_dce3_01a_mathematics: HashMap<String, String>,
45    pub dc_map_dce3_01a_punctuation_and_whitespace: HashMap<String, String>,
46    pub dc_map_dce3_01a_semantic_records: HashMap<String, String>,
47    pub dc_map_dce3_01a_variant_selectors: HashMap<String, String>,
48}
49
50fn parse_csv_map(csv_bytes: &[u8]) -> HashMap<String, String> {
51    let mut rdr = csv::ReaderBuilder::new()
52        .has_headers(false)
53        .from_reader(csv_bytes);
54    let mut map = HashMap::new();
55    for record in rdr.records().flatten() {
56        if record.len() >= 2 {
57            if let (Some(k), Some(v)) = (record.get(0), record.get(1)) {
58                let _ = map.insert(k.to_string(), v.to_string());
59            }
60        } else if record.len() == 1 {
61            if let Some(k) = record.get(0) {
62                let _ = map.insert(k.to_string(), String::new());
63            }
64        }
65    }
66    map
67}
68
69fn parse_csv_vec(csv_bytes: &[u8]) -> Vec<String> {
70    let mut rdr = csv::ReaderBuilder::new()
71        .has_headers(false)
72        .from_reader(csv_bytes);
73    let mut vec = vec![String::new(); 256];
74    for record in rdr.records().flatten() {
75        if record.len() >= 2 {
76            if let (Some(k), Some(v)) = (record.get(0), record.get(1)) {
77                if let Ok(idx) = k.parse::<usize>() {
78                    if let Some(slot) = vec.get_mut(idx) {
79                        *slot = v.to_string();
80                    }
81                }
82            }
83        }
84    }
85    vec
86}
87
88pub static TABLES: OnceLock<Tables> = OnceLock::new();
89
90pub fn get_tables() -> &'static Tables {
91    TABLES.get_or_init(|| {
92        Tables {
93            dc_map_unicode_lossy: parse_csv_map(include_bytes!("data/csv/dceutils_data/dceutils_data.php-DcMap_Unicode_Lossy.csv")),
94            dc_map_send_unicode: parse_csv_map(include_bytes!("data/csv/dceutils_data_dc/dceutils_data_dc.php-DcMapSend_Unicode.csv")),
95            base64_to_dc: parse_csv_map(include_bytes!("data/csv/dceutils_data/dceutils_data.php-Base64_to_Dc.csv")),
96            dc_to_base64: parse_csv_map(include_bytes!("data/csv/dceutils_data/dceutils_data.php-Dc_to_Base64.csv")),
97            cdce_html_legacy: parse_csv_map(include_bytes!("data/csv/dceutils_data/dceutils_data.php-cdce_html_legacy.csv")),
98            dce_versions: parse_csv_vec(include_bytes!("data/csv/dceutils_data/dceutils_data.php-dce_versions.csv")),
99            dc_map_send_dce3_0a: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_0a/dceutils_data_3_0a.php-DcMapSend_dce3_0a.csv")),
100            dc_map_dce3_0a_core: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_0a/dceutils_data_3_0a.php-DcMap_dce3_0a_Core.csv")),
101            dce3_0a_core: parse_csv_vec(include_bytes!("data/csv/dceutils_data_3_0a/dceutils_data_3_0a.php-dce3_0a_core.csv")),
102            dc_map_send_dce3_01a_all: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_01a/dceutils_data_3_01a.php-DcMapSend_dce3_01a_All.csv")),
103            dc_map_dce3_01a_core: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_01a/dceutils_data_3_01a.php-DcMap_dce3_01a_Core.csv")),
104            dc_map_dce3_01a_mathematics: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_01a/dceutils_data_3_01a.php-DcMap_dce3_01a_Mathematics.csv")),
105            dc_map_dce3_01a_punctuation_and_whitespace: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_01a/dceutils_data_3_01a.php-DcMap_dce3_01a_Punctuation_and_Whitespace.csv")),
106            dc_map_dce3_01a_semantic_records: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_01a/dceutils_data_3_01a.php-DcMap_dce3_01a_Semantic_Records.csv")),
107            dc_map_dce3_01a_variant_selectors: parse_csv_map(include_bytes!("data/csv/dceutils_data_3_01a/dceutils_data_3_01a.php-DcMap_dce3_01a_Variant_Selectors.csv")),
108        }
109    })
110}