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