Skip to main content

xml_disassembler/
types.rs

1//! Type definitions for XML element representation.
2//!
3//! Uses serde_json::Value for flexible representation matching the TypeScript structure:
4//! - Object with keys: element names, @attr for attributes, #text for text content, ?xml for declaration
5//! - Values: string, nested object, or array of objects/strings
6
7use serde_json::Value as JsonValue;
8
9/// XmlElement is a flexible representation of XML - equivalent to TypeScript's XmlElement type.
10/// Uses serde_json::Value for compatibility with quickxml_to_serde output.
11pub type XmlElement = JsonValue;
12
13/// Parameters for parsing an element during disassembly.
14#[derive(Debug, Clone)]
15pub struct XmlElementParams<'a> {
16    pub element: XmlElement,
17    pub disassembled_path: &'a str,
18    pub unique_id_elements: Option<&'a str>,
19    pub root_element_name: &'a str,
20    pub root_attributes: XmlElement,
21    pub key: &'a str,
22    pub leaf_content: XmlElement,
23    pub leaf_count: usize,
24    pub has_nested_elements: bool,
25    pub format: &'a str,
26    pub xml_declaration: Option<XmlElement>,
27    pub strategy: &'a str,
28}
29
30/// Options for building a single disassembled file.
31#[derive(Debug, Clone)]
32pub struct BuildDisassembledFileOptions<'a> {
33    pub content: XmlElement,
34    pub disassembled_path: &'a str,
35    pub output_file_name: Option<&'a str>,
36    pub subdirectory: Option<&'a str>,
37    pub wrap_key: Option<&'a str>,
38    pub is_grouped_array: bool,
39    pub root_element_name: &'a str,
40    pub root_attributes: XmlElement,
41    pub format: &'a str,
42    pub xml_declaration: Option<XmlElement>,
43    pub unique_id_elements: Option<&'a str>,
44}
45
46/// Result from unified element parsing.
47#[derive(Debug, Clone, Default)]
48pub struct UnifiedParseResult {
49    pub leaf_content: XmlElement,
50    pub leaf_count: usize,
51    pub has_nested_elements: bool,
52    pub nested_groups: Option<XmlElementArrayMap>,
53}
54
55/// Map of tag name to array of elements.
56pub type XmlElementArrayMap = std::collections::HashMap<String, Vec<XmlElement>>;
57
58/// Options for building disassembled files from a source file.
59#[derive(Debug, Clone)]
60pub struct BuildDisassembledFilesOptions<'a> {
61    pub file_path: &'a str,
62    pub disassembled_path: &'a str,
63    pub base_name: &'a str,
64    pub post_purge: bool,
65    pub format: &'a str,
66    pub unique_id_elements: Option<&'a str>,
67    pub strategy: &'a str,
68}
69
70/// Parameters for writing leaf content.
71#[derive(Debug, Clone)]
72pub struct LeafWriteParams<'a> {
73    pub leaf_count: usize,
74    pub leaf_content: XmlElement,
75    pub strategy: &'a str,
76    pub key_order: Vec<String>,
77    pub options: LeafWriteOptions<'a>,
78}
79
80#[derive(Debug, Clone)]
81pub struct LeafWriteOptions<'a> {
82    pub disassembled_path: &'a str,
83    pub output_file_name: &'a str,
84    pub root_element_name: &'a str,
85    pub root_attributes: XmlElement,
86    pub xml_declaration: Option<XmlElement>,
87    pub format: &'a str,
88}
89
90/// Rule for multi-level disassembly: which files to further disassemble and how.
91#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
92pub struct MultiLevelRule {
93    /// File name pattern (e.g. "programProcesses-meta"); any XML file whose name contains this is processed.
94    pub file_pattern: String,
95    /// Root element to strip (e.g. "LoyaltyProgramSetup"); its inner content becomes the new document.
96    pub root_to_strip: String,
97    /// Comma-separated unique-id elements for the second-level disassembly (e.g. "parameterName,ruleName").
98    pub unique_id_elements: String,
99    /// Path segment under the disassembly root for reassembly (e.g. "programProcesses").
100    #[serde(default)]
101    pub path_segment: String,
102    /// Root element name to wrap reassembled files with (defaults to root_to_strip).
103    #[serde(default)]
104    pub wrap_root_element: String,
105    /// xmlns value for the wrap root (optional; captured from original when stripping).
106    #[serde(default)]
107    pub wrap_xmlns: String,
108}
109
110/// Persisted config for multi-level reassembly (stored as .multi_level.json in the disassembly root).
111#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
112pub struct MultiLevelConfig {
113    pub rules: Vec<MultiLevelRule>,
114}