ctb_io_webui/controllers/rpc/
eite.rs1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use serde_json::Value;
30
31pub async fn handle_eite_call(
33 state: &mut ctb_formats_eite::eite_state::EiteState,
34 func: &str,
35 args: &[Value],
36) -> anyhow::Result<Value> {
37 match func {
38 "setupIfNeeded" => Ok(Value::Null),
39 "dcGetColumn" => {
40 let dataset = args
41 .first()
42 .and_then(Value::as_str)
43 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dataset"))?;
44 let col_idx_raw =
45 args.get(1).and_then(Value::as_u64).ok_or_else(|| {
46 anyhow::anyhow!("Missing arg 1: field_number")
47 })?;
48 let col_idx = usize::try_from(col_idx_raw)?;
49 let col = ctb_formats_eite::dc::dc_get_column(dataset, col_idx)?;
50 Ok(serde_json::to_value(col)?)
51 }
52 "dcDatasetLength" => {
53 let dataset = args
54 .first()
55 .and_then(Value::as_str)
56 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dataset"))?;
57 if dataset == "DcData" {
58 let len = ctb_formats_eite::dc::get_dc_count()?;
59 Ok(serde_json::to_value(len)?)
60 } else {
61 anyhow::bail!("Unknown dataset: {dataset}")
62 }
63 }
64 "listInputFormats" => {
65 let list = ctb_formats_eite::formats::list_input_formats()?;
66 Ok(serde_json::to_value(list)?)
67 }
68 "listOutputFormats" => {
69 let list = ctb_formats_eite::formats::list_output_formats()?;
70 Ok(serde_json::to_value(list)?)
71 }
72 "pushExportSettings" => {
73 let format_id_raw = args
74 .first()
75 .and_then(Value::as_u64)
76 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format_id"))?;
77 let format_id = usize::try_from(format_id_raw)?;
78 let settings = args
79 .get(1)
80 .and_then(Value::as_str)
81 .ok_or_else(|| anyhow::anyhow!("Missing arg 1: settings"))?;
82 ctb_formats_eite::settings::push_export_settings(
83 state, format_id, settings,
84 )?;
85 Ok(Value::Null)
86 }
87 "popExportSettings" => {
88 let format_id_raw = args
89 .first()
90 .and_then(Value::as_u64)
91 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format_id"))?;
92 let format_id = usize::try_from(format_id_raw)?;
93 ctb_formats_eite::settings::pop_export_settings(state, format_id)?;
94 Ok(Value::Null)
95 }
96 "pushImportSettings" => {
97 let format_id_raw = args
98 .first()
99 .and_then(Value::as_u64)
100 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format_id"))?;
101 let format_id = usize::try_from(format_id_raw)?;
102 let settings = args
103 .get(1)
104 .and_then(Value::as_str)
105 .ok_or_else(|| anyhow::anyhow!("Missing arg 1: settings"))?;
106 ctb_formats_eite::settings::push_import_settings(
107 state, format_id, settings,
108 )?;
109 Ok(Value::Null)
110 }
111 "popImportSettings" => {
112 let format_id_raw = args
113 .first()
114 .and_then(Value::as_u64)
115 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format_id"))?;
116 let format_id = usize::try_from(format_id_raw)?;
117 ctb_formats_eite::settings::pop_import_settings(state, format_id)?;
118 Ok(Value::Null)
119 }
120 "importAndExport" => {
121 let in_fmt_str = args
122 .first()
123 .and_then(Value::as_str)
124 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: in_format"))?;
125 let out_fmt_str = args
126 .get(1)
127 .and_then(Value::as_str)
128 .ok_or_else(|| anyhow::anyhow!("Missing arg 1: out_format"))?;
129 let content_val = args
130 .get(2)
131 .ok_or_else(|| anyhow::anyhow!("Missing arg 2: content"))?;
132 let content: Vec<u8> = serde_json::from_value(content_val.clone())?;
133
134 let in_fmt =
135 ctb_formats_eite::formats::Format::from_string(in_fmt_str)?;
136 let out_fmt =
137 ctb_formats_eite::formats::Format::from_string(out_fmt_str)?;
138
139 let (res, log) = ctb_formats_eite::import_and_export(
140 state,
141 &in_fmt,
142 &out_fmt,
143 &content,
144 &ctb_formats_eite::formats::PrefilterSettings::default(),
145 )?;
146 log.auto_log();
147 Ok(serde_json::to_value(res)?)
148 }
149 "strFromByteArray" => {
150 let bytes_val = args
151 .first()
152 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: bytes"))?;
153 let bytes: Vec<u8> = serde_json::from_value(bytes_val.clone())?;
154 let s = String::from_utf8(bytes)?;
155 Ok(Value::String(s))
156 }
157 "strToByteArray" => {
158 let s = args
159 .first()
160 .and_then(Value::as_str)
161 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: string"))?;
162 let bytes = s.as_bytes().to_vec();
163 Ok(serde_json::to_value(bytes)?)
164 }
165 "runDocument" => {
166 let dc_arr_val = args
167 .first()
168 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dc_array"))?;
169 let dc_array: Vec<u32> =
170 serde_json::from_value(dc_arr_val.clone())?;
171 ctb_formats_eite::runtime::run_document(state, &dc_array)?;
172 Ok(Value::Null)
173 }
174 "importDocument" => {
175 let fmt_str = args
176 .first()
177 .and_then(Value::as_str)
178 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format"))?;
179 let bytes_val = args
180 .get(1)
181 .ok_or_else(|| anyhow::anyhow!("Missing arg 1: bytes"))?;
182 let bytes: Vec<u8> = serde_json::from_value(bytes_val.clone())?;
183 let fmt = ctb_formats_eite::formats::Format::from_string(fmt_str)?;
184 let (dc_array, log) =
185 ctb_formats_eite::import_document(state, &fmt, &bytes)?;
186 log.auto_log();
187 Ok(serde_json::to_value(dc_array)?)
188 }
189 "getFormatId" => {
190 let fmt_str = args
191 .first()
192 .and_then(Value::as_str)
193 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format"))?;
194 let id = ctb_formats_eite::formats::get_format_id(fmt_str)?;
195 Ok(serde_json::to_value(id)?)
196 }
197 "isKnownDc" => {
198 let dc_raw = args
199 .first()
200 .and_then(Value::as_u64)
201 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dc"))?;
202 let dc = u32::try_from(dc_raw)?;
203 let b = ctb_formats_eite::dc::is_known_dc(dc)?;
204 Ok(Value::Bool(b))
205 }
206 "dcGetName" => {
207 let dc_raw = args
208 .first()
209 .and_then(Value::as_u64)
210 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dc"))?;
211 let dc = u32::try_from(dc_raw)?;
212 let name = ctb_formats_eite::dc::dc_get_name(dc)?;
213 Ok(Value::String(name))
214 }
215 "isSupportedInputFormat" => {
216 let fmt_str = args
217 .first()
218 .and_then(Value::as_str)
219 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format"))?;
220 let b =
221 ctb_formats_eite::formats::is_supported_input_format(fmt_str);
222 Ok(Value::Bool(b))
223 }
224 "isSupportedOutputFormat" => {
225 let fmt_str = args
226 .first()
227 .and_then(Value::as_str)
228 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format"))?;
229 let b =
230 ctb_formats_eite::formats::is_supported_output_format(fmt_str);
231 Ok(Value::Bool(b))
232 }
233 "getExportExtension" => {
234 let fmt_str = args
235 .first()
236 .and_then(Value::as_str)
237 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: format"))?;
238 let ext = ctb_formats_eite::formats::get_export_extension(fmt_str)?;
239 Ok(Value::String(ext))
240 }
241 "getFileFromPath" => {
242 let path = args
243 .first()
244 .and_then(Value::as_str)
245 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: path"))?;
246 let bytes = if path.starts_with("http://")
247 || path.starts_with("https://")
248 {
249 crate::utilities::https::get(path).await?
250 } else {
251 ctb_formats_eite::get_eite_data(path).ok_or_else(|| {
252 anyhow::anyhow!("EITE asset not found: {path}")
253 })?
254 };
255 Ok(serde_json::to_value(bytes)?)
256 }
257 "dcaToDcbnbFragmentUtf8" => {
258 let dc_arr_val = args
259 .first()
260 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dc_array"))?;
261 let dc_array: Vec<u32> =
262 serde_json::from_value(dc_arr_val.clone())?;
263 let (bytes, log) = ctb_formats_eite::formats::dcbasenb::dca_to_dcbnb_fragment_utf8(
264 &dc_array,
265 &ctb_formats_eite::formats::utf8::UTF8FormatSettings::default(),
266 )?;
267 log.auto_log();
268 Ok(serde_json::to_value(bytes)?)
269 }
270 "dcaFromDcbnbFragmentUtf8" => {
271 let bytes_val = args
272 .first()
273 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: bytes"))?;
274 let bytes: Vec<u8> = serde_json::from_value(bytes_val.clone())?;
275 let (dc_array, log) = ctb_formats_eite::formats::dcbasenb::dca_from_dcbnb_fragment_utf8(
276 &bytes,
277 &ctb_formats_eite::formats::utf8::UTF8FormatSettings::default(),
278 )?;
279 log.auto_log();
280 Ok(serde_json::to_value(dc_array)?)
281 }
282 "dcbnbGetFirstChar" => {
283 let bytes_val = args
284 .first()
285 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: bytes"))?;
286 let bytes: Vec<u8> = serde_json::from_value(bytes_val.clone())?;
287 let first =
288 ctb_formats_eite::formats::dcbasenb::dcbnb_get_first_char(
289 &bytes,
290 )?;
291 Ok(serde_json::to_value(first)?)
292 }
293 "dcbnbGetLastChar" => {
294 let bytes_val = args
295 .first()
296 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: bytes"))?;
297 let bytes: Vec<u8> = serde_json::from_value(bytes_val.clone())?;
298 let last =
299 ctb_formats_eite::formats::dcbasenb::dcbnb_get_last_char(
300 &bytes,
301 )?;
302 Ok(serde_json::to_value(last)?)
303 }
304 "printArr" => {
305 let dc_arr_val = args
306 .first()
307 .ok_or_else(|| anyhow::anyhow!("Missing arg 0: dc_array"))?;
308 let dc_array: Vec<u32> =
309 serde_json::from_value(dc_arr_val.clone())?;
310 let s = ctb_formats_eite::util::array::print_arr(&dc_array);
311 Ok(Value::String(s))
312 }
313 other => {
314 anyhow::bail!("Function '{other}' not found in EITE RPC allowlist")
315 }
316 }
317}