ctb_formats_eite/
transform.rs1use 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
80pub fn is_supported_document_transformation(transform: &str) -> bool {
82 matches!(transform, "semanticToText" | "codeToText")
83}
84
85pub 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
93pub 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
110pub fn apply_prefilters(
115 dc_array_in: &[u32],
116 settings: &PrefilterSettings,
117) -> Result<Vec<u32>> {
118 settings.apply(dc_array_in)
119}
120
121pub fn dct_semantic_to_text(
123 dc_array: &[u32],
124 _output_language: &DcOutputLanguage,
125) -> Result<Vec<u32>> {
126 Ok(dc_array.to_vec())
128}
129
130pub fn dct_code_to_text(
132 dc_array: &[u32],
133 _output_language: &DcOutputLanguage,
134) -> Result<Vec<u32>> {
135 Ok(dc_array.to_vec())
137}