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
//! Disassemble XML file handler.

use crate::builders::build_disassembled_files_unified;
use crate::multi_level::{
    capture_xmlns_from_root, path_segment_from_file_pattern, save_multi_level_config,
    strip_root_and_build_xml,
};
use crate::parsers::parse_xml;
use crate::types::{BuildDisassembledFilesOptions, DecomposeRule, MultiLevelRule};
use crate::utils::normalize_path_unix;
use ignore::gitignore::GitignoreBuilder;
use std::path::Path;
use tokio::fs;

pub struct DisassembleXmlFileHandler {
    ign: Option<ignore::gitignore::Gitignore>,
}

impl DisassembleXmlFileHandler {
    pub fn new() -> Self {
        Self { ign: None }
    }

    async fn load_ignore_rules(&mut self, ignore_path: &str) {
        let path = Path::new(ignore_path);
        let content = match fs::read_to_string(path).await {
            Ok(c) => c,
            Err(_) => return,
        };
        let root = path.parent().unwrap_or(Path::new("."));
        let mut builder = GitignoreBuilder::new(root);
        for line in content.lines() {
            let _ = builder.add_line(None, line);
        }
        // `GitignoreBuilder::build` only fails on unlikely I/O errors; treat as absent rules.
        self.ign = builder.build().ok();
    }

    fn posix_path(path: &str) -> String {
        path.replace('\\', "/")
    }

    fn is_xml_file(file_path: &str) -> bool {
        file_path.to_lowercase().ends_with(".xml")
    }

    fn is_ignored(&self, path: &str) -> bool {
        self.ign
            .as_ref()
            .map(|ign| ign.matched(path, false).is_ignore())
            .unwrap_or(false)
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn disassemble(
        &mut self,
        file_path: &str,
        unique_id_elements: Option<&str>,
        strategy: Option<&str>,
        pre_purge: bool,
        post_purge: bool,
        ignore_path: &str,
        format: &str,
        multi_level_rule: Option<&MultiLevelRule>,
        decompose_rules: Option<&[DecomposeRule]>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let strategy = strategy.unwrap_or("unique-id");
        let strategy = if ["unique-id", "grouped-by-tag"].contains(&strategy) {
            strategy
        } else {
            log::warn!(
                "Unsupported strategy \"{}\", defaulting to \"unique-id\".",
                strategy
            );
            "unique-id"
        };

        self.load_ignore_rules(ignore_path).await;

        let path = Path::new(file_path);
        let meta = fs::metadata(path).await?;
        let cwd = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf());
        let relative_path = path.strip_prefix(&cwd).unwrap_or(path).to_string_lossy();
        let relative_path = Self::posix_path(&relative_path);

        if meta.is_file() {
            self.handle_file(
                file_path,
                &relative_path,
                unique_id_elements,
                strategy,
                pre_purge,
                post_purge,
                format,
                multi_level_rule,
                decompose_rules,
            )
            .await?;
        } else {
            // Anything that isn't a regular file is treated as a directory; fs::metadata on
            // the caller already errored out if the path didn't exist.
            self.handle_directory(
                file_path,
                unique_id_elements,
                strategy,
                pre_purge,
                post_purge,
                format,
                multi_level_rule,
                decompose_rules,
            )
            .await?;
        }

        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    async fn handle_file(
        &self,
        file_path: &str,
        relative_path: &str,
        unique_id_elements: Option<&str>,
        strategy: &str,
        pre_purge: bool,
        post_purge: bool,
        format: &str,
        multi_level_rule: Option<&MultiLevelRule>,
        decompose_rules: Option<&[DecomposeRule]>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let resolved = Path::new(file_path)
            .canonicalize()
            .unwrap_or_else(|_| Path::new(file_path).to_path_buf());
        let resolved_str = normalize_path_unix(&resolved.to_string_lossy());

        if !Self::is_xml_file(&resolved_str) {
            log::error!(
                "The file path provided is not an XML file: {}",
                resolved_str
            );
            return Ok(());
        }

        if self.is_ignored(relative_path) {
            log::warn!("File ignored by ignore rules: {}", resolved_str);
            return Ok(());
        }

        let dir_path = resolved.parent().unwrap_or(Path::new("."));
        let dir_path_str = normalize_path_unix(&dir_path.to_string_lossy());
        self.process_file(
            &dir_path_str,
            strategy,
            &resolved_str,
            unique_id_elements,
            pre_purge,
            post_purge,
            format,
            multi_level_rule,
            decompose_rules,
        )
        .await
    }

    #[allow(clippy::too_many_arguments)]
    async fn handle_directory(
        &self,
        dir_path: &str,
        unique_id_elements: Option<&str>,
        strategy: &str,
        pre_purge: bool,
        post_purge: bool,
        format: &str,
        multi_level_rule: Option<&MultiLevelRule>,
        decompose_rules: Option<&[DecomposeRule]>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let dir_path = normalize_path_unix(dir_path);
        let mut entries = fs::read_dir(&dir_path).await?;
        let cwd = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf());

        while let Some(entry) = entries.next_entry().await? {
            let sub_path = entry.path();
            let sub_file_path = sub_path.to_string_lossy();
            let relative_sub = sub_path
                .strip_prefix(&cwd)
                .unwrap_or(&sub_path)
                .to_string_lossy();
            let relative_sub = Self::posix_path(&relative_sub);

            if !(sub_path.is_file() && Self::is_xml_file(&sub_file_path)) {
                continue;
            }
            if self.is_ignored(&relative_sub) {
                log::warn!("File ignored by ignore rules: {}", sub_file_path);
                continue;
            }
            let sub_file_path_norm = normalize_path_unix(&sub_file_path);
            self.process_file(
                &dir_path,
                strategy,
                &sub_file_path_norm,
                unique_id_elements,
                pre_purge,
                post_purge,
                format,
                multi_level_rule,
                decompose_rules,
            )
            .await?;
        }
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    async fn process_file(
        &self,
        dir_path: &str,
        strategy: &str,
        file_path: &str,
        unique_id_elements: Option<&str>,
        pre_purge: bool,
        post_purge: bool,
        format: &str,
        multi_level_rule: Option<&MultiLevelRule>,
        decompose_rules: Option<&[DecomposeRule]>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        log::debug!("Parsing file to disassemble: {}", file_path);

        let file_name = Path::new(file_path)
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("output");
        let base_name = file_name.split('.').next().unwrap_or(file_name);
        let output_path = Path::new(dir_path).join(base_name);

        if pre_purge && output_path.exists() {
            fs::remove_dir_all(&output_path).await.ok();
        }

        build_disassembled_files_unified(BuildDisassembledFilesOptions {
            file_path,
            disassembled_path: output_path.to_str().unwrap_or("."),
            base_name: file_name,
            post_purge,
            format,
            unique_id_elements,
            strategy,
            decompose_rules,
        })
        .await?;

        if let Some(rule) = multi_level_rule {
            self.recursively_disassemble_multi_level(&output_path, rule, format)
                .await?;
        }

        Ok(())
    }

    /// Recursively walk the disassembly output; for XML files matching the rule's file_pattern,
    /// strip the root and re-disassemble with the rule's unique_id_elements.
    async fn recursively_disassemble_multi_level(
        &self,
        dir_path: &Path,
        rule: &MultiLevelRule,
        format: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut config = crate::multi_level::load_multi_level_config(dir_path)
            .await
            .unwrap_or_default();

        let mut stack = vec![dir_path.to_path_buf()];
        while let Some(current) = stack.pop() {
            let mut entries = Vec::new();
            let mut read_dir = fs::read_dir(&current).await?;
            while let Some(entry) = read_dir.next_entry().await? {
                entries.push(entry);
            }

            for entry in entries {
                let path = entry.path();
                let path_str = path.to_string_lossy().to_string();

                if path.is_dir() {
                    stack.push(path);
                    continue;
                }
                // Anything not a directory is processed as a regular file below.
                {
                    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
                    let path_str_check = path.to_string_lossy();
                    if !name.ends_with(".xml")
                        || (!name.contains(&rule.file_pattern)
                            && !path_str_check.contains(&rule.file_pattern))
                    {
                        continue;
                    }

                    let parsed = match parse_xml(&path_str).await {
                        Some(p) => p,
                        None => continue,
                    };
                    let has_element_to_strip = parsed
                        .as_object()
                        .and_then(|o| {
                            let root_key = o.keys().find(|k| *k != "?xml")?;
                            let root_val = o.get(root_key)?.as_object()?;
                            Some(
                                root_key == &rule.root_to_strip
                                    || root_val.contains_key(&rule.root_to_strip),
                            )
                        })
                        .unwrap_or(false);
                    if !has_element_to_strip {
                        continue;
                    }

                    let wrap_xmlns = capture_xmlns_from_root(&parsed).unwrap_or_default();

                    let stripped_xml = match strip_root_and_build_xml(&parsed, &rule.root_to_strip)
                    {
                        Some(xml) => xml,
                        None => continue,
                    };

                    fs::write(&path, stripped_xml).await?;

                    let file_stem = path
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .unwrap_or("output");
                    let output_dir_name = file_stem.split('.').next().unwrap_or(file_stem);
                    let parent = path.parent().unwrap_or(dir_path);
                    let second_level_output = parent.join(output_dir_name);

                    build_disassembled_files_unified(BuildDisassembledFilesOptions {
                        file_path: &path_str,
                        disassembled_path: second_level_output.to_str().unwrap_or("."),
                        base_name: output_dir_name,
                        post_purge: true,
                        format,
                        unique_id_elements: Some(&rule.unique_id_elements),
                        strategy: "unique-id",
                        decompose_rules: None,
                    })
                    .await?;

                    match config.rules.first_mut() {
                        None => {
                            let wrap_root = parsed
                                .as_object()
                                .and_then(|o| o.keys().find(|k| *k != "?xml").cloned())
                                .unwrap_or_else(|| rule.wrap_root_element.clone());
                            let path_segment = if rule.path_segment.is_empty() {
                                path_segment_from_file_pattern(&rule.file_pattern)
                            } else {
                                rule.path_segment.clone()
                            };
                            let stored_xmlns = if rule.wrap_xmlns.is_empty() {
                                wrap_xmlns
                            } else {
                                rule.wrap_xmlns.clone()
                            };
                            config.rules.push(MultiLevelRule {
                                file_pattern: rule.file_pattern.clone(),
                                root_to_strip: rule.root_to_strip.clone(),
                                unique_id_elements: rule.unique_id_elements.clone(),
                                path_segment,
                                // Persist document root (e.g. LoyaltyProgramSetup) so reassembly uses it
                                // as root with xmlns; path_segment is the inner wrapper in each file.
                                wrap_root_element: wrap_root,
                                wrap_xmlns: stored_xmlns,
                            });
                        }
                        Some(r) if r.wrap_xmlns.is_empty() => {
                            r.wrap_xmlns = wrap_xmlns;
                        }
                        Some(_) => {}
                    }
                }
            }
        }

        if !config.rules.is_empty() {
            save_multi_level_config(dir_path, &config).await?;
        }

        Ok(())
    }
}

impl Default for DisassembleXmlFileHandler {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    #[allow(clippy::default_constructed_unit_structs)]
    fn disassemble_handler_default_equals_new() {
        let _ = DisassembleXmlFileHandler::default();
    }

    #[test]
    fn is_xml_file_matches_case_insensitively() {
        assert!(DisassembleXmlFileHandler::is_xml_file("foo.xml"));
        assert!(DisassembleXmlFileHandler::is_xml_file("BAR.XML"));
        assert!(!DisassembleXmlFileHandler::is_xml_file("foo.txt"));
    }

    #[test]
    fn posix_path_converts_backslashes() {
        assert_eq!(
            DisassembleXmlFileHandler::posix_path(r"C:\Users\name\file.xml"),
            "C:/Users/name/file.xml"
        );
    }

    #[tokio::test]
    async fn load_ignore_rules_noop_when_path_missing() {
        let mut handler = DisassembleXmlFileHandler::new();
        handler
            .load_ignore_rules("/definitely/does/not/exist/.ignore")
            .await;
        assert!(handler.ign.is_none());
    }

    #[tokio::test]
    async fn load_ignore_rules_builds_matcher() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join(".ignore");
        tokio::fs::write(&path, "*.xml\n").await.unwrap();
        let mut handler = DisassembleXmlFileHandler::new();
        handler.load_ignore_rules(path.to_str().unwrap()).await;
        assert!(handler.ign.is_some());
        assert!(handler.is_ignored("file.xml"));
        assert!(!handler.is_ignored("file.txt"));
    }

    #[test]
    fn is_ignored_default_false_without_rules() {
        let handler = DisassembleXmlFileHandler::new();
        assert!(!handler.is_ignored("some/path.xml"));
    }
}