Skip to main content

xml_disassembler/parsers/
parse_element.rs

1//! Parse element during disassembly - unified strategy handling.
2
3use crate::builders::build_disassembled_file;
4use crate::types::{UnifiedParseResult, XmlElementArrayMap, XmlElementParams};
5use serde_json::{Map, Value};
6
7fn is_nested_object(element: &Value) -> bool {
8    if let Some(obj) = element.as_object() {
9        obj.keys()
10            .any(|k| !k.starts_with('#') && !k.starts_with('@') && k != "?xml")
11    } else {
12        false
13    }
14}
15
16pub async fn parse_element_unified(params: XmlElementParams<'_>) -> UnifiedParseResult {
17    let XmlElementParams {
18        element,
19        disassembled_path,
20        unique_id_elements,
21        root_element_name,
22        root_attributes,
23        key,
24        leaf_count,
25        has_nested_elements,
26        format,
27        xml_declaration,
28        strategy,
29        leaf_content: _,
30    } = params;
31
32    let is_array = element.is_array();
33    let is_nested_obj = is_nested_object(&element);
34    let is_nested = is_array || is_nested_obj;
35
36    if is_nested {
37        if strategy == "grouped-by-tag" {
38            let mut nested = XmlElementArrayMap::new();
39            nested.insert(key.to_string(), vec![element.clone()]);
40            return UnifiedParseResult {
41                leaf_content: Value::Object(Map::new()),
42                leaf_count,
43                has_nested_elements: true,
44                nested_groups: Some(nested),
45            };
46        } else {
47            let _ = build_disassembled_file(crate::types::BuildDisassembledFileOptions {
48                content: element.clone(),
49                disassembled_path,
50                output_file_name: None,
51                subdirectory: Some(key),
52                wrap_key: Some(key),
53                is_grouped_array: false,
54                root_element_name,
55                root_attributes: root_attributes.clone(),
56                format,
57                xml_declaration: xml_declaration.clone(),
58                unique_id_elements,
59            })
60            .await;
61            return UnifiedParseResult {
62                leaf_content: Value::Object(Map::new()),
63                leaf_count,
64                has_nested_elements: true,
65                nested_groups: None,
66            };
67        }
68    }
69
70    let mut leaf_content = Map::new();
71    leaf_content.insert(key.to_string(), Value::Array(vec![element]));
72    UnifiedParseResult {
73        leaf_content: Value::Object(leaf_content),
74        leaf_count: leaf_count + 1,
75        has_nested_elements,
76        nested_groups: None,
77    }
78}