ctb_formats_eite/formats/
elad.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//! ELAD (`.elad` / `.emd`) short-Dc ASCII document conversions.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use anyhow::Result;
30
31use crate::dc::DC_REPLACEMENT_UNAVAIL_DC;
32use crate::formats::FormatLog;
33use crate::formats::ascii::dca_from_ascii;
34
35/// Parses an ELAD (`.elad` / `.emd`) byte slice into a short-Dc `u32` array:
36/// printable ASCII characters other than `@` (`0x40`) map via [`dca_from_ascii`],
37/// while `@<decimal_short_dc> ` (terminated by a single space `0x20`) decodes to
38/// the specified short Dc ID.
39pub fn dca_from_elad(content: &[u8]) -> Result<(Vec<u32>, FormatLog)> {
40    let mut out = Vec::with_capacity(content.len());
41    let mut log = FormatLog::default();
42    let mut i = 0usize;
43    while let Some(&b) = content.get(i) {
44        if b == b'@' {
45            let digits_start = i.saturating_add(1);
46            let mut j = digits_start;
47            while content.get(j).is_some_and(u8::is_ascii_digit) {
48                j = j.saturating_add(1);
49            }
50            if j > digits_start && content.get(j) == Some(&b' ') {
51                let digit_slice = content
52                    .get(digits_start..j)
53                    .context("ELAD short Dc digit span out of bounds")?;
54                let digit_str = std::str::from_utf8(digit_slice)?;
55                match digit_str.parse::<u32>() {
56                    Ok(short_id) if short_id <= 1_114_111 => {
57                        out.push(short_id);
58                        i = j.saturating_add(1);
59                        continue;
60                    }
61                    _ => {
62                        log.error(&format!(
63                            "Invalid ELAD short Dc tag '@{digit_str} ' at byte {i}"
64                        ));
65                    }
66                }
67            } else {
68                log.error(&format!(
69                    "Malformed ELAD '@' escape at byte {i}: expected '@<short_dc> '"
70                ));
71            }
72            out.push(DC_REPLACEMENT_UNAVAIL_DC.to_short()?);
73            i = i.saturating_add(1);
74            continue;
75        }
76        let (chunk_dca, chunk_log) = dca_from_ascii(&[b])?;
77        log.merge(&chunk_log);
78        out.extend(chunk_dca);
79        i = i.saturating_add(1);
80    }
81    Ok((out, log))
82}
83
84/// Convert to “Elad” – placeholder (returns empty per original stub).
85pub fn dca_to_elad(_dc_array: &[u32]) -> Result<Vec<u8>> {
86    // Original had:
87    // intArrayRes = [];
88    // assertByteArray(intArrayRes);
89    Ok(Vec::new())
90}