ctb_formats/
formats.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//! Library providing parsers, encoders, and conversion tools for data formats.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace crate prelude"
26)]
27pub(crate) use ctb_utilities::*;
28
29#[allow(
30    unused_imports,
31    clippy::wildcard_imports,
32    reason = "Standard workspace prelude"
33)]
34pub use ctb_utilities::ipc::service_prelude::*;
35
36use ctb_formats_utilities::FormatLog;
37use std::collections::HashMap;
38use uuid::Uuid;
39
40pub use ctb_formats_alias as alias;
41pub use ctb_formats_apple_single_double as apple_single_double;
42pub use ctb_formats_applescript as applescript;
43pub use ctb_formats_archive as archive;
44pub use ctb_formats_base16b as base16b;
45pub use ctb_formats_base64 as base64;
46pub use ctb_formats_checksum as checksum;
47pub use ctb_formats_compression as compression;
48pub use ctb_formats_conversion as conversion;
49pub use ctb_formats_ctb_asset_bundle as ctb_asset_bundle;
50pub use ctb_formats_dcdata as dcdata;
51pub use ctb_formats_dceutils as dceutils;
52pub use ctb_formats_dcstring as dcstring;
53pub use ctb_formats_dct_el as dct_el;
54pub use ctb_formats_dctf as dctf;
55pub use ctb_formats_detection as detection;
56pub use ctb_formats_docker as docker;
57pub use ctb_formats_eite as eite;
58pub use ctb_formats_encoding as encoding;
59pub use ctb_formats_hexdump as hexdump;
60pub use ctb_formats_html as html;
61pub use ctb_formats_internetarchive as internetarchive;
62pub use ctb_formats_ipaddr as ipaddr;
63pub use ctb_formats_javascript as javascript;
64pub use ctb_formats_json as json;
65pub use ctb_formats_kaitai as kaitai;
66pub use ctb_formats_lnk as lnk;
67pub use ctb_formats_markdown as markdown;
68pub use ctb_formats_math as math;
69pub use ctb_formats_multipart as multipart;
70pub use ctb_formats_pan as pan;
71pub use ctb_formats_pdf as pdf;
72pub use ctb_formats_pem as pem;
73pub use ctb_formats_perl as perl;
74pub use ctb_formats_python as python;
75pub use ctb_formats_rust as rust;
76pub use ctb_formats_stagel as stagel;
77pub use ctb_formats_string as string;
78pub use ctb_formats_syndication as syndication;
79pub use ctb_formats_text_extraction as text_extraction;
80pub use ctb_formats_time as time;
81pub use ctb_formats_troff as troff;
82pub use ctb_formats_unicode as unicode;
83pub use ctb_formats_uri as uri;
84pub use ctb_formats_useragent as useragent;
85pub use ctb_formats_utf8 as utf8;
86pub use ctb_formats_warc as warc;
87pub use ctb_formats_wfscan as wfscan;
88pub use ctb_formats_wtf8 as wtf8;
89pub use ctb_formats_x86 as x86;
90
91pub fn string_result_with_log_to_vec(
92    result: Result<(String, FormatLog)>,
93) -> Result<(Vec<u8>, FormatLog)> {
94    result.map(|res| {
95        let result_bytes = res.0.into_bytes();
96        (result_bytes, res.1)
97    })
98}
99
100pub fn get_format_uuids<'a>() -> HashMap<Vec<u8>, Vec<u8>> {
101    HashMap::from([(
102        strtovec("9ba60c52-9cf8-41a7-b3ea-7a1e14f6c5d7"),
103        strtovec("html"),
104    )])
105}
106
107#[expect(
108    clippy::expect_used,
109    clippy::unwrap_in_result,
110    reason = "Slice bounds are guaranteed by preceding len checks"
111)]
112pub fn get_format_from_uuid(document: Vec<u8>) -> Option<Vec<u8>> {
113    let head = if document.len() < 36 {
114        document
115    } else {
116        document
117            .get(..36)
118            .expect("document length is >= 36")
119            .to_vec()
120    };
121    let uuid_val = get_uuid_from_document(head)?;
122    get_format_uuids().get(&uuid_val).cloned()
123}
124
125#[expect(
126    clippy::expect_used,
127    clippy::unwrap_in_result,
128    reason = "Slice bounds for 16-byte binary UUID are guaranteed by preceding len >= 16 check"
129)]
130pub fn get_uuid_from_document(document: Vec<u8>) -> Option<Vec<u8>> {
131    if document.len() < 16 {
132        return None;
133    }
134
135    let uuid_binary =
136        Uuid::from_slice(document.get(..16).expect("document length is >= 16"))
137            .ok()?
138            .hyphenated()
139            .to_string()
140            .into_bytes();
141    // Reason for fallback: document may be under 36 bytes, in which case empty slice safely prevents panic during textual UUID check.
142    let uuid_string =
143        String::from_utf8_lossy(document.get(..36).unwrap_or(&[]))
144            .to_string()
145            .into_bytes();
146
147    let formats = get_format_uuids();
148
149    if formats.contains_key(&uuid_binary) {
150        return Some(uuid_binary);
151    } else if formats.contains_key(&uuid_string) {
152        return Some(uuid_string);
153    }
154
155    None
156}
157
158pub fn convert_if_needed(document: Vec<u8>) -> Vec<u8> {
159    // TODO
160
161    document
162}
163
164pub fn convert_from(document: Vec<u8>, _filetype: Vec<u8>) -> Vec<u8> {
165    // TODO
166
167    document
168}
169
170#[cfg(test)]
171#[allow(
172    clippy::panic,
173    clippy::expect_used,
174    clippy::unwrap_used,
175    clippy::unwrap_in_result,
176    clippy::panic_in_result_fn,
177    clippy::indexing_slicing,
178    clippy::arithmetic_side_effects,
179    reason = "Standard repository test boilerplate"
180)]
181mod tests {
182    use super::*;
183
184    #[crate::ctb_test]
185    fn can_get_uuid_from_document() {
186        assert_eq!(
187            strtovec("9ba60c52-9cf8-41a7-b3ea-7a1e14f6c5d7"),
188            get_uuid_from_document(strtovec(
189                "9ba60c52-9cf8-41a7-b3ea-7a1e14f6c5d7<html>"
190            ))
191            .unwrap()
192        );
193    }
194
195    #[crate::ctb_test]
196    fn can_get_format_from_uuid() {
197        assert_eq!(
198            strtovec("html"),
199            get_format_from_uuid(strtovec(
200                "9ba60c52-9cf8-41a7-b3ea-7a1e14f6c5d7<html>"
201            ))
202            .unwrap()
203        );
204    }
205}