xml-disassembler 0.4.8

Disassemble XML into smaller, manageable files and reassemble on demand.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Build disassembled files from source XML file.

use crate::builders::{build_disassembled_file, extract_root_attributes};
use crate::parsers::{extract_xml_declaration_from_raw, parse_element_unified};
use crate::types::{
    BuildDisassembledFilesOptions, DecomposeRule, XmlElementArrayMap, XmlElementParams,
};
use crate::utils::normalize_path_unix;
use serde_json::{Map, Value};
use std::collections::HashMap;
use tokio::fs;

const BATCH_SIZE: usize = 20;

fn get_root_info(parsed_xml: &Value) -> Option<(String, Value)> {
    let obj = parsed_xml.as_object()?;
    let root_element_name = obj.keys().find(|k| *k != "?xml")?.clone();
    let root_element = obj.get(&root_element_name)?.clone();
    Some((root_element_name, root_element))
}

fn order_xml_element_keys(content: &Map<String, Value>, key_order: &[String]) -> Value {
    let mut ordered = Map::new();
    for key in key_order {
        if let Some(v) = content.get(key) {
            ordered.insert(key.clone(), v.clone());
        }
    }
    Value::Object(ordered)
}

#[allow(clippy::too_many_arguments)]
async fn disassemble_element_keys(
    root_element: &Value,
    key_order: &[String],
    disassembled_path: &str,
    root_element_name: &str,
    root_attributes: &Value,
    xml_declaration: Option<&Value>,
    unique_id_elements: Option<&str>,
    strategy: &str,
    format: &str,
) -> (Map<String, Value>, XmlElementArrayMap, usize, bool) {
    let mut leaf_content = Map::new();
    let mut nested_groups = XmlElementArrayMap::new();
    let mut leaf_count = 0usize;
    let mut has_nested_elements = false;

    let empty_map = Map::new();
    let root_obj = root_element.as_object().unwrap_or(&empty_map);

    // Iterate root_obj in key_order's ordering: we consume only keys that are present,
    // which matches the caller's invariant and keeps the loop body branch-free.
    let ordered: Vec<(&String, &Value)> = key_order
        .iter()
        .filter_map(|k| root_obj.get_key_value(k))
        .collect();
    for (key, val) in ordered {
        let elements: Vec<Value> = match val.as_array() {
            Some(arr) => arr.clone(),
            None => vec![val.clone()],
        };

        for chunk in elements.chunks(BATCH_SIZE) {
            for element in chunk {
                let result = parse_element_unified(XmlElementParams {
                    element: element.clone(),
                    disassembled_path,
                    unique_id_elements,
                    root_element_name,
                    root_attributes: root_attributes.clone(),
                    key,
                    leaf_content: Value::Object(Map::new()),
                    leaf_count,
                    has_nested_elements,
                    format,
                    xml_declaration: xml_declaration.cloned(),
                    strategy,
                })
                .await;

                if let Some(arr) = result.leaf_content.as_object().and_then(|o| o.get(key)) {
                    match leaf_content.get_mut(key).and_then(|v| v.as_array_mut()) {
                        Some(existing_arr) => {
                            if let Some(new_arr) = arr.as_array() {
                                existing_arr.extend(new_arr.iter().cloned());
                            }
                        }
                        None => {
                            leaf_content.insert(key.clone(), arr.clone());
                        }
                    }
                }

                if strategy == "grouped-by-tag" {
                    if let Some(groups) = result.nested_groups {
                        for (tag, arr) in groups {
                            nested_groups.entry(tag).or_default().extend(arr);
                        }
                    }
                }

                leaf_count = result.leaf_count;
                has_nested_elements = result.has_nested_elements;
            }
        }
    }

    (leaf_content, nested_groups, leaf_count, has_nested_elements)
}

/// Extract string from an element's field - handles direct strings and objects with #text (XML leaf elements).
fn get_field_value(element: &Value, field: &str) -> Option<String> {
    let v = element.as_object()?.get(field)?;
    if let Some(s) = v.as_str() {
        return Some(s.to_string());
    }
    v.as_object()
        .and_then(|child| child.get("#text"))
        .and_then(|t| t.as_str())
        .map(|s| s.to_string())
}

/// For group mode: use the segment before the first '.' as key when present (e.g. "Account.Name" -> "Account").
fn group_key_from_field_value(s: &str) -> &str {
    s.find('.').map(|i| &s[..i]).unwrap_or(s)
}

/// Sanitize a string for use as a filename (no path separators or invalid chars).
fn sanitize_filename(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

async fn write_nested_groups(
    nested_groups: &XmlElementArrayMap,
    strategy: &str,
    options: &WriteNestedOptions<'_>,
) {
    if strategy != "grouped-by-tag" {
        return;
    }
    let decompose_by_tag: HashMap<&str, &DecomposeRule> = options
        .decompose_rules
        .map(|rules| rules.iter().map(|r| (r.tag.as_str(), r)).collect())
        .unwrap_or_default();

    for (tag, arr) in nested_groups {
        let rule = decompose_by_tag.get(tag.as_str());
        let path_segment = rule
            .map(|r| {
                if r.path_segment.is_empty() {
                    &r.tag
                } else {
                    &r.path_segment
                }
            })
            .unwrap_or(tag);

        if let Some(r) = rule {
            if r.mode == "split" {
                for (idx, item) in arr.iter().enumerate() {
                    let name = get_field_value(item, &r.field)
                        .as_deref()
                        .map(sanitize_filename)
                        .filter(|s: &String| !s.is_empty())
                        .unwrap_or_else(|| idx.to_string());
                    let file_name = format!("{}.{}-meta.{}", name, tag, options.format);
                    let _ = build_disassembled_file(crate::types::BuildDisassembledFileOptions {
                        content: item.clone(),
                        disassembled_path: options.disassembled_path,
                        output_file_name: Some(&file_name),
                        subdirectory: Some(path_segment),
                        wrap_key: Some(tag),
                        is_grouped_array: false,
                        root_element_name: options.root_element_name,
                        root_attributes: options.root_attributes.clone(),
                        format: options.format,
                        xml_declaration: options.xml_declaration.clone(),
                        unique_id_elements: None,
                    })
                    .await;
                }
            } else if r.mode == "group" {
                let mut by_key: HashMap<String, Vec<Value>> = HashMap::new();
                for item in arr {
                    let key = get_field_value(item, &r.field)
                        .as_deref()
                        .map(group_key_from_field_value)
                        .map(sanitize_filename)
                        .filter(|s: &String| !s.is_empty())
                        .unwrap_or_else(|| "unknown".to_string());
                    by_key.entry(key).or_default().push(item.clone());
                }
                // Sort keys for deterministic cross-platform output order
                let mut sorted_keys: Vec<_> = by_key.keys().cloned().collect();
                sorted_keys.sort();
                for key in sorted_keys {
                    let group = by_key.remove(&key).unwrap();
                    let file_name = format!("{}.{}-meta.{}", key, tag, options.format);
                    let _ = build_disassembled_file(crate::types::BuildDisassembledFileOptions {
                        content: Value::Array(group),
                        disassembled_path: options.disassembled_path,
                        output_file_name: Some(&file_name),
                        subdirectory: Some(path_segment),
                        wrap_key: Some(tag),
                        is_grouped_array: true,
                        root_element_name: options.root_element_name,
                        root_attributes: options.root_attributes.clone(),
                        format: options.format,
                        xml_declaration: options.xml_declaration.clone(),
                        unique_id_elements: None,
                    })
                    .await;
                }
            } else {
                fallback_write_one_file(tag, arr, path_segment, options).await;
            }
        } else {
            fallback_write_one_file(tag, arr, path_segment, options).await;
        }
    }
}

async fn fallback_write_one_file(
    tag: &str,
    arr: &[Value],
    _path_segment: &str,
    options: &WriteNestedOptions<'_>,
) {
    let _ = build_disassembled_file(crate::types::BuildDisassembledFileOptions {
        content: Value::Array(arr.to_vec()),
        disassembled_path: options.disassembled_path,
        output_file_name: Some(&format!("{}.{}", tag, options.format)),
        subdirectory: None,
        wrap_key: Some(tag),
        is_grouped_array: true,
        root_element_name: options.root_element_name,
        root_attributes: options.root_attributes.clone(),
        format: options.format,
        xml_declaration: options.xml_declaration.clone(),
        unique_id_elements: None,
    })
    .await;
}

struct WriteNestedOptions<'a> {
    disassembled_path: &'a str,
    root_element_name: &'a str,
    root_attributes: Value,
    xml_declaration: Option<Value>,
    format: &'a str,
    decompose_rules: Option<&'a [DecomposeRule]>,
}

pub async fn build_disassembled_files_unified(
    options: BuildDisassembledFilesOptions<'_>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let BuildDisassembledFilesOptions {
        file_path,
        disassembled_path,
        base_name,
        post_purge,
        format,
        unique_id_elements,
        strategy,
        decompose_rules,
    } = options;

    let file_path = normalize_path_unix(file_path);

    let xml_content = match fs::read_to_string(&file_path).await {
        Ok(c) => c,
        Err(_) => return Ok(()),
    };

    let parsed_xml = match crate::parsers::parse_xml_from_str(&xml_content, &file_path) {
        Some(p) => p,
        None => return Ok(()),
    };

    let (root_element_name, root_element) = match get_root_info(&parsed_xml) {
        Some(info) => info,
        None => return Ok(()),
    };
    // The custom parser ignores <?xml ?>; always recover it from raw XML.
    let xml_declaration = extract_xml_declaration_from_raw(&xml_content);

    let root_attributes = extract_root_attributes(&root_element);
    let key_order: Vec<String> = root_element
        .as_object()
        .map(|o| o.keys().filter(|k| !k.starts_with('@')).cloned().collect())
        .unwrap_or_default();

    let (leaf_content, nested_groups, leaf_count, has_nested_elements) = disassemble_element_keys(
        &root_element,
        &key_order,
        disassembled_path,
        &root_element_name,
        &root_attributes,
        xml_declaration.as_ref(),
        unique_id_elements,
        strategy,
        format,
    )
    .await;

    if !has_nested_elements && leaf_count > 0 {
        log::error!(
            "The XML file {} only has leaf elements. This file will not be disassembled.",
            &file_path
        );
        return Ok(());
    }

    let write_opts = WriteNestedOptions {
        disassembled_path,
        root_element_name: &root_element_name,
        root_attributes: root_attributes.clone(),
        xml_declaration: xml_declaration.clone(),
        format,
        decompose_rules,
    };
    write_nested_groups(&nested_groups, strategy, &write_opts).await;

    // Persist root key order so reassembly can match original document order.
    // serde_json::to_string never fails for Vec<String>; writes are best-effort.
    let key_order_path = std::path::Path::new(disassembled_path).join(".key_order.json");
    let json = serde_json::to_string(&key_order).unwrap_or_else(|_| "[]".to_string());
    let _ = fs::write(key_order_path, json).await;

    if leaf_count > 0 {
        let final_leaf_content = if strategy == "grouped-by-tag" {
            order_xml_element_keys(&leaf_content, &key_order)
        } else {
            Value::Object(leaf_content.clone())
        };

        let _ = build_disassembled_file(crate::types::BuildDisassembledFileOptions {
            content: final_leaf_content,
            disassembled_path,
            output_file_name: Some(&format!("{}.{}", base_name, format)),
            subdirectory: None,
            wrap_key: None,
            is_grouped_array: false,
            root_element_name: &root_element_name,
            root_attributes: root_attributes.clone(),
            format,
            xml_declaration: xml_declaration.clone(),
            unique_id_elements: None,
        })
        .await;
    }

    if post_purge {
        // Best-effort purge; a failure here is benign (file may have been removed concurrently).
        let _ = fs::remove_file(&file_path).await;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn get_field_value_returns_direct_string() {
        let el = json!({ "field": "value" });
        assert_eq!(get_field_value(&el, "field"), Some("value".to_string()));
    }

    #[test]
    fn get_field_value_returns_nested_text() {
        let el = json!({ "field": { "#text": "value" } });
        assert_eq!(get_field_value(&el, "field"), Some("value".to_string()));
    }

    #[test]
    fn get_field_value_returns_none_when_missing_or_non_string() {
        let el = json!({ "field": { "nested": { "#text": "x" } } });
        assert!(get_field_value(&el, "field").is_none());
        assert!(get_field_value(&el, "missing").is_none());
        let el = json!("not-an-object");
        assert!(get_field_value(&el, "field").is_none());
    }

    #[test]
    fn group_key_from_field_value_takes_prefix_before_dot() {
        assert_eq!(group_key_from_field_value("Account.Name"), "Account");
        assert_eq!(group_key_from_field_value("NoDot"), "NoDot");
    }

    #[test]
    fn sanitize_filename_replaces_disallowed_chars_with_underscore() {
        assert_eq!(sanitize_filename("a/b c:d"), "a_b_c_d");
        assert_eq!(sanitize_filename("ok-name_1.xml"), "ok-name_1.xml");
    }

    #[test]
    fn order_xml_element_keys_preserves_order_and_drops_absent() {
        let mut m = Map::new();
        m.insert("b".to_string(), json!(2));
        m.insert("a".to_string(), json!(1));
        let ordered =
            order_xml_element_keys(&m, &["a".to_string(), "c".to_string(), "b".to_string()]);
        let obj = ordered.as_object().unwrap();
        let keys: Vec<&String> = obj.keys().collect();
        assert_eq!(keys, vec![&"a".to_string(), &"b".to_string()]);
    }

    #[test]
    fn get_root_info_returns_name_and_element() {
        let parsed = json!({ "?xml": {"@version": "1.0"}, "Root": { "child": 1 } });
        let (name, element) = get_root_info(&parsed).unwrap();
        assert_eq!(name, "Root");
        assert!(element.as_object().unwrap().contains_key("child"));
    }

    #[test]
    fn get_root_info_returns_none_for_non_object_or_decl_only() {
        assert!(get_root_info(&json!("s")).is_none());
        assert!(get_root_info(&json!({ "?xml": {} })).is_none());
    }

    #[tokio::test]
    async fn unified_build_returns_ok_when_source_unreadable() {
        // Missing source file: unified build should short-circuit with Ok(()).
        let dir = tempfile::tempdir().unwrap();
        let disassembled = dir.path().join("out");
        let missing = dir.path().join("does_not_exist.xml");
        build_disassembled_files_unified(BuildDisassembledFilesOptions {
            file_path: missing.to_str().unwrap(),
            disassembled_path: disassembled.to_str().unwrap(),
            base_name: "does_not_exist",
            post_purge: false,
            format: "xml",
            unique_id_elements: None,
            strategy: "unique-id",
            decompose_rules: None,
        })
        .await
        .unwrap();
        assert!(!disassembled.exists());
    }
}