ctb_formats_eite/
transform.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//! Transformations for EITE documents.
21
22use crate::utilities::*;
23
24use anyhow::{Result, bail};
25
26use crate::dc::data::dc_data_filter_by_value;
27use crate::formats::{DcOutputLanguage, PrefilterSettings};
28
29pub enum DocumentTransformation {
30    SemanticToText { lang: DcOutputLanguage },
31    CodeToText { lang: DcOutputLanguage },
32}
33
34impl DocumentTransformation {
35    pub fn apply(&self, dc_array_in: &[u32]) -> Result<Vec<u32>> {
36        apply_document_transformation(self, dc_array_in)
37    }
38
39    pub fn from_str(transform: &str, lang: DcOutputLanguage) -> Result<Self> {
40        match transform {
41            "semanticToText" => {
42                Ok(DocumentTransformation::SemanticToText { lang })
43            }
44            "codeToText" => Ok(DocumentTransformation::CodeToText { lang }),
45            _ => bail!("Unknown document transformation: {transform}"),
46        }
47    }
48
49    pub fn semantic_to_text_default() -> Self {
50        DocumentTransformation::SemanticToText {
51            lang: DcOutputLanguage::default(),
52        }
53    }
54
55    pub fn code_to_text_default() -> Self {
56        DocumentTransformation::CodeToText {
57            lang: DcOutputLanguage::default(),
58        }
59    }
60}
61
62impl std::fmt::Display for DocumentTransformation {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        match self {
65            DocumentTransformation::SemanticToText { lang } => {
66                writeln!(f, "Semantic to Text ({lang}):")?;
67            }
68            DocumentTransformation::CodeToText { lang } => {
69                writeln!(f, "Code to Text ({lang}):")?;
70            }
71        }
72        Ok(())
73    }
74}
75
76pub fn list_document_transformations() -> Result<Vec<String>> {
77    dc_data_filter_by_value("formats", 6, "transformation", 1)
78}
79
80/// Supported document transformations (static list as per original).
81pub fn is_supported_document_transformation(transform: &str) -> bool {
82    matches!(transform, "semanticToText" | "codeToText")
83}
84
85/// Assert the document transformation is supported.
86pub fn assert_supported_document_transformation(transform: &str) -> Result<()> {
87    if !is_supported_document_transformation(transform) {
88        bail!("Unsupported document transformation: {transform}");
89    }
90    Ok(())
91}
92
93/// Apply a document transformation producing a new Dc array.
94/// semanticToText -> `dct_semantic_to_text`
95/// codeToText     -> `dct_code_to_text`
96pub fn apply_document_transformation(
97    transform: &DocumentTransformation,
98    dc_array_in: &[u32],
99) -> Result<Vec<u32>> {
100    match transform {
101        DocumentTransformation::SemanticToText { lang } => {
102            dct_semantic_to_text(dc_array_in, lang)
103        }
104        DocumentTransformation::CodeToText { lang } => {
105            dct_code_to_text(dc_array_in, lang)
106        }
107    }
108}
109
110/// Preprocess (prefilters) for an output format by optionally applying:
111///  - Semantic to Text conversion if setting '`prefilter_semantic`' present.
112///  - Code to Text conversion if setting '`prefilter_code`' present.
113/// Mirrors original dcPreprocessForFormat.
114pub fn apply_prefilters(
115    dc_array_in: &[u32],
116    settings: &PrefilterSettings,
117) -> Result<Vec<u32>> {
118    settings.apply(dc_array_in)
119}
120
121/// Convert semantic Dc sequence to text Dc sequence (placeholder).
122pub fn dct_semantic_to_text(
123    dc_array: &[u32],
124    _output_language: &DcOutputLanguage,
125) -> Result<Vec<u32>> {
126    // Placeholder pass-through (replace with actual logic if already implemented elsewhere).
127    Ok(dc_array.to_vec())
128}
129
130/// Convert code Dc sequence to text Dc sequence (placeholder).
131pub fn dct_code_to_text(
132    dc_array: &[u32],
133    _output_language: &DcOutputLanguage,
134) -> Result<Vec<u32>> {
135    // Placeholder pass-through (replace with actual logic if already implemented elsewhere).
136    Ok(dc_array.to_vec())
137}