Skip to main content

xml_disassembler/
multi_level.rs

1//! Multi-level disassembly: strip a root element and re-disassemble with different unique-id elements.
2
3use serde_json::{Map, Value};
4
5use crate::builders::build_xml_string;
6use crate::types::{MultiLevelConfig, XmlElement};
7
8/// Strip the given element and build a new XML string.
9/// - If it is the root element: its inner content becomes the new document (with ?xml preserved).
10/// - If it is a child of the root (e.g. programProcesses under LoyaltyProgramSetup): unwrap it so
11///   its inner content becomes the direct children of the root; the root element is kept.
12pub fn strip_root_and_build_xml(parsed: &XmlElement, element_to_strip: &str) -> Option<String> {
13    let obj = parsed.as_object()?;
14    let root_key = obj.keys().find(|k| *k != "?xml")?.clone();
15    let root_val = obj.get(&root_key)?.as_object()?;
16    let decl = obj.get("?xml").cloned().unwrap_or_else(|| {
17        let mut d = Map::new();
18        d.insert("@version".to_string(), Value::String("1.0".to_string()));
19        d.insert("@encoding".to_string(), Value::String("UTF-8".to_string()));
20        Value::Object(d)
21    });
22
23    if root_key == element_to_strip {
24        // Strip the root: new doc = ?xml + inner content of root (element keys only, not @attributes)
25        let mut new_obj = Map::new();
26        new_obj.insert("?xml".to_string(), decl);
27        for (k, v) in root_val {
28            if !k.starts_with('@') {
29                new_obj.insert(k.clone(), v.clone());
30            }
31        }
32        return Some(build_xml_string(&Value::Object(new_obj)));
33    }
34
35    // Strip a child of the root: unwrap it so its inner content becomes direct children of the root
36    let inner = root_val.get(element_to_strip)?.as_object()?;
37    let mut new_root_val = Map::new();
38    for (k, v) in root_val {
39        if k != element_to_strip {
40            new_root_val.insert(k.clone(), v.clone());
41        }
42    }
43    for (k, v) in inner {
44        new_root_val.insert(k.clone(), v.clone());
45    }
46    let mut new_obj = Map::new();
47    new_obj.insert("?xml".to_string(), decl);
48    new_obj.insert(root_key, Value::Object(new_root_val));
49    Some(build_xml_string(&Value::Object(new_obj)))
50}
51
52/// Capture xmlns from the root element (e.g. LoyaltyProgramSetup) for later wrap.
53pub fn capture_xmlns_from_root(parsed: &XmlElement) -> Option<String> {
54    let obj = parsed.as_object()?;
55    let root_key = obj.keys().find(|k| *k != "?xml")?.clone();
56    let root_val = obj.get(&root_key)?.as_object()?;
57    let xmlns = root_val.get("@xmlns")?.as_str()?;
58    Some(xmlns.to_string())
59}
60
61/// Derive path_segment from file_pattern (e.g. "programProcesses-meta" -> "programProcesses").
62pub fn path_segment_from_file_pattern(file_pattern: &str) -> String {
63    // `split('-').next()` always returns `Some(_)` for any string - even an empty one -
64    // so falling back to the original `file_pattern` is unreachable.
65    file_pattern
66        .split('-')
67        .next()
68        .unwrap_or(file_pattern)
69        .to_string()
70}
71
72/// Load multi-level config from a directory (reads .multi_level.json).
73pub async fn load_multi_level_config(dir_path: &std::path::Path) -> Option<MultiLevelConfig> {
74    let path = dir_path.join(".multi_level.json");
75    let content = tokio::fs::read_to_string(&path).await.ok()?;
76    serde_json::from_str(&content).ok()
77}
78
79/// Persist multi-level config to a directory.
80pub async fn save_multi_level_config(
81    dir_path: &std::path::Path,
82    config: &MultiLevelConfig,
83) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
84    let path = dir_path.join(".multi_level.json");
85    let content = serde_json::to_string_pretty(config)?;
86    tokio::fs::write(path, content).await?;
87    Ok(())
88}
89
90/// Ensure all XML files in a segment directory have structure:
91/// document_root (with xmlns) > inner_wrapper (no xmlns) > content.
92/// Used after inner-level reassembly for multi-level (e.g. LoyaltyProgramSetup > programProcesses).
93pub async fn ensure_segment_files_structure(
94    dir_path: &std::path::Path,
95    document_root: &str,
96    inner_wrapper: &str,
97    xmlns: &str,
98) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
99    use crate::parsers::parse_xml_from_str;
100    use serde_json::Map;
101
102    let mut entries = Vec::new();
103    let mut read_dir = tokio::fs::read_dir(dir_path).await?;
104    while let Some(entry) = read_dir.next_entry().await? {
105        entries.push(entry);
106    }
107    // Sort for deterministic cross-platform ordering
108    entries.sort_by_key(|e| e.file_name());
109
110    for entry in entries {
111        let path = entry.path();
112        if !path.is_file() {
113            continue;
114        }
115        let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
116        if !name.ends_with(".xml") {
117            continue;
118        }
119        let path_str = path.to_string_lossy();
120        // Read errors on a file the walker just reported as present are essentially impossible
121        // (concurrent deletion); treat the content as empty so downstream lookups skip naturally.
122        let content = tokio::fs::read_to_string(&path).await.unwrap_or_default();
123        let Some(parsed) = parse_xml_from_str(&content, &path_str) else {
124            continue;
125        };
126        // parse_xml_from_str always yields a JSON object when it returns Some; fall back to an
127        // empty map for any unexpected shape so subsequent lookups simply produce None.
128        let obj = parsed.as_object().cloned().unwrap_or_default();
129        let Some(current_root_key) = obj.keys().find(|k| *k != "?xml").cloned() else {
130            continue;
131        };
132        let root_val = obj
133            .get(&current_root_key)
134            .and_then(|v| v.as_object())
135            .cloned()
136            .unwrap_or_default();
137
138        let decl = obj.get("?xml").cloned().unwrap_or_else(|| {
139            let mut d = Map::new();
140            d.insert(
141                "@version".to_string(),
142                serde_json::Value::String("1.0".to_string()),
143            );
144            d.insert(
145                "@encoding".to_string(),
146                serde_json::Value::String("UTF-8".to_string()),
147            );
148            serde_json::Value::Object(d)
149        });
150
151        let non_attr_keys: Vec<&String> = root_val.keys().filter(|k| *k != "@xmlns").collect();
152        let single_inner = non_attr_keys.len() == 1 && non_attr_keys[0].as_str() == inner_wrapper;
153        let inner_content: serde_json::Value = if current_root_key == document_root && single_inner
154        {
155            let inner_obj = root_val
156                .get(inner_wrapper)
157                .and_then(|v| v.as_object())
158                .cloned()
159                .unwrap_or_else(Map::new);
160            let mut inner_clean = Map::new();
161            for (k, v) in &inner_obj {
162                if k != "@xmlns" {
163                    inner_clean.insert(k.clone(), v.clone());
164                }
165            }
166            serde_json::Value::Object(inner_clean)
167        } else {
168            serde_json::Value::Object(root_val.clone())
169        };
170
171        let already_correct = current_root_key == document_root
172            && root_val.get("@xmlns").is_some()
173            && single_inner
174            && root_val
175                .get(inner_wrapper)
176                .and_then(|v| v.as_object())
177                .map(|o| !o.contains_key("@xmlns"))
178                .unwrap_or(true);
179        if already_correct {
180            continue;
181        }
182
183        // Build document_root (with @xmlns only on root) > inner_wrapper (no xmlns) > content
184        let mut root_val_new = Map::new();
185        if !xmlns.is_empty() {
186            root_val_new.insert(
187                "@xmlns".to_string(),
188                serde_json::Value::String(xmlns.to_string()),
189            );
190        }
191        root_val_new.insert(inner_wrapper.to_string(), inner_content);
192
193        let mut top = Map::new();
194        top.insert("?xml".to_string(), decl);
195        top.insert(
196            document_root.to_string(),
197            serde_json::Value::Object(root_val_new),
198        );
199        let wrapped = serde_json::Value::Object(top);
200        let xml_string = build_xml_string(&wrapped);
201        tokio::fs::write(&path, xml_string).await?;
202    }
203    Ok(())
204}
205
206#[cfg(test)]
207mod tests {
208    use super::*;
209    use serde_json::json;
210
211    #[test]
212    fn path_segment_from_file_pattern_strips_suffix() {
213        assert_eq!(
214            path_segment_from_file_pattern("programProcesses-meta"),
215            "programProcesses"
216        );
217    }
218
219    #[test]
220    fn path_segment_from_file_pattern_no_dash() {
221        assert_eq!(path_segment_from_file_pattern("foo"), "foo");
222    }
223
224    #[test]
225    fn strip_root_and_build_xml_strips_child_not_root() {
226        let parsed = json!({
227            "?xml": { "@version": "1.0" },
228            "Root": {
229                "programProcesses": { "a": "1", "b": "2" },
230                "label": "x"
231            }
232        });
233        let out = strip_root_and_build_xml(&parsed, "programProcesses").unwrap();
234        assert!(out.contains("<Root>"));
235        assert!(out.contains("<a>1</a>"));
236        assert!(out.contains("<b>2</b>"));
237        assert!(out.contains("<label>x</label>"));
238    }
239
240    #[test]
241    fn strip_root_and_build_xml_strips_root_excludes_attributes() {
242        let parsed = json!({
243            "?xml": { "@version": "1.0" },
244            "LoyaltyProgramSetup": {
245                "@xmlns": "http://example.com",
246                "programProcesses": { "x": "1" }
247            }
248        });
249        let out = strip_root_and_build_xml(&parsed, "LoyaltyProgramSetup").unwrap();
250        assert!(!out.contains("@xmlns"));
251        assert!(out.contains("programProcesses"));
252    }
253
254    #[test]
255    fn capture_xmlns_from_root_returns_some() {
256        let parsed = json!({
257            "Root": { "@xmlns": "http://ns.example.com" }
258        });
259        assert_eq!(
260            capture_xmlns_from_root(&parsed),
261            Some("http://ns.example.com".to_string())
262        );
263    }
264
265    #[test]
266    fn capture_xmlns_from_root_returns_none_when_absent() {
267        let parsed = json!({ "Root": { "child": "x" } });
268        assert!(capture_xmlns_from_root(&parsed).is_none());
269    }
270
271    #[tokio::test]
272    async fn save_and_load_multi_level_config() {
273        let dir = tempfile::tempdir().unwrap();
274        let config = MultiLevelConfig {
275            rules: vec![crate::types::MultiLevelRule {
276                file_pattern: "test-meta".to_string(),
277                root_to_strip: "Root".to_string(),
278                unique_id_elements: "id".to_string(),
279                path_segment: "test".to_string(),
280                wrap_root_element: "Root".to_string(),
281                wrap_xmlns: "http://example.com".to_string(),
282            }],
283        };
284        save_multi_level_config(dir.path(), &config).await.unwrap();
285        let loaded = load_multi_level_config(dir.path()).await.unwrap();
286        assert_eq!(loaded.rules.len(), 1);
287        assert_eq!(loaded.rules[0].path_segment, "test");
288    }
289
290    #[tokio::test]
291    async fn load_multi_level_config_missing_file_returns_none() {
292        let dir = tempfile::tempdir().unwrap();
293        assert!(load_multi_level_config(dir.path()).await.is_none());
294    }
295
296    #[tokio::test]
297    async fn ensure_segment_files_structure_adds_xmlns_and_rewrites() {
298        let dir = tempfile::tempdir().unwrap();
299        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
300<Root>
301  <programProcesses><x>1</x></programProcesses>
302</Root>"#;
303        let path = dir.path().join("segment.xml");
304        tokio::fs::write(&path, xml).await.unwrap();
305        ensure_segment_files_structure(
306            dir.path(),
307            "Root",
308            "programProcesses",
309            "http://example.com",
310        )
311        .await
312        .unwrap();
313        let out = tokio::fs::read_to_string(&path).await.unwrap();
314        assert!(out.contains("http://example.com"));
315        assert!(out.contains("<programProcesses>"));
316        assert!(out.contains("<x>1</x>"));
317    }
318
319    #[tokio::test]
320    async fn ensure_segment_files_structure_skips_already_correct_files() {
321        // Root wraps inner_wrapper and has xmlns; inner has no xmlns -> no rewrite.
322        let dir = tempfile::tempdir().unwrap();
323        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
324<Root xmlns="http://example.com"><programProcesses><x>1</x></programProcesses></Root>"#;
325        let path = dir.path().join("ok.xml");
326        tokio::fs::write(&path, xml).await.unwrap();
327        let before = tokio::fs::metadata(&path).await.unwrap().modified().ok();
328        ensure_segment_files_structure(
329            dir.path(),
330            "Root",
331            "programProcesses",
332            "http://example.com",
333        )
334        .await
335        .unwrap();
336        let after = tokio::fs::metadata(&path).await.unwrap().modified().ok();
337        assert_eq!(before, after, "already-correct files must be left as-is");
338    }
339
340    #[tokio::test]
341    async fn ensure_segment_files_structure_skips_non_xml_and_subdirs() {
342        let dir = tempfile::tempdir().unwrap();
343        tokio::fs::create_dir(dir.path().join("nested"))
344            .await
345            .unwrap();
346        tokio::fs::write(dir.path().join("notes.txt"), "hello")
347            .await
348            .unwrap();
349        tokio::fs::write(dir.path().join("broken.xml"), "<<not xml>")
350            .await
351            .unwrap();
352        // No XML payload that matches; should succeed without writing anything.
353        ensure_segment_files_structure(
354            dir.path(),
355            "Root",
356            "programProcesses",
357            "http://example.com",
358        )
359        .await
360        .unwrap();
361        // broken.xml remains unchanged
362        let raw = tokio::fs::read_to_string(dir.path().join("broken.xml"))
363            .await
364            .unwrap();
365        assert_eq!(raw, "<<not xml>");
366    }
367
368    #[tokio::test]
369    async fn ensure_segment_files_structure_skips_xml_missing_root() {
370        // Only a declaration, no root element (empty document)
371        let dir = tempfile::tempdir().unwrap();
372        tokio::fs::write(dir.path().join("empty.xml"), "")
373            .await
374            .unwrap();
375        ensure_segment_files_structure(dir.path(), "Root", "programProcesses", "")
376            .await
377            .unwrap();
378    }
379}