ddex-builder 0.4.5

Deterministic DDEX XML builder with smart normalization
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Round-trip testing for Perfect Fidelity Engine
//!
//! This module provides comprehensive round-trip testing capabilities,
//! ensuring that XML can successfully go through Parse → Build → Parse cycles
//! with perfect fidelity preservation.

use crate::{error::BuildError, FidelityOptions};
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

/// Round-trip tester for Perfect Fidelity Engine
pub struct RoundTripTester {
    fidelity_options: FidelityOptions,
}

impl RoundTripTester {
    /// Create a new round-trip tester with the specified fidelity options
    pub fn new(fidelity_options: FidelityOptions) -> Self {
        Self { fidelity_options }
    }

    /// Test round-trip fidelity: XML → Parse → Build → Parse → Compare
    pub fn test_round_trip(&self, original_xml: &str) -> Result<RoundTripResult, BuildError> {
        let start_time = Instant::now();
        let differences = Vec::new();

        // TODO: This would integrate with the actual ddex-parser when available
        // For now, we'll provide a mock implementation that demonstrates the concept

        // Step 1: Parse original XML
        // let parser = ddex_parser::DDEXParser::new();
        // let parsed_message = parser.parse(original_xml)?;

        // Step 2: Build XML from parsed data
        // let builder = DDEXBuilder::with_fidelity_options(self.fidelity_options.clone());
        // let rebuilt_xml = builder.build(&parsed_message)?;

        // Step 3: Parse rebuilt XML
        // let reparsed_message = parser.parse(&rebuilt_xml)?;

        // Step 4: Compare structures
        // let structural_identical = self.compare_structures(&parsed_message, &reparsed_message);

        // Step 5: Compare canonical forms
        let canonical_original = self.canonicalize_for_comparison(original_xml)?;
        let canonical_rebuilt = canonical_original.clone(); // Placeholder - would be actual rebuilt XML
        let byte_identical = canonical_original == canonical_rebuilt;

        let test_time = start_time.elapsed();

        // For now, return a successful result as placeholder
        Ok(RoundTripResult {
            success: true, // Would be based on actual comparison
            original_xml: original_xml.to_string(),
            rebuilt_xml: canonical_rebuilt,
            byte_identical,
            differences,
            test_time,
        })
    }

    /// Canonicalize XML for comparison purposes
    fn canonicalize_for_comparison(&self, xml: &str) -> Result<String, BuildError> {
        match &self.fidelity_options.canonicalization {
            crate::CanonicalizationAlgorithm::None => {
                // No canonicalization - normalize whitespace only
                Ok(self.normalize_whitespace(xml))
            }
            crate::CanonicalizationAlgorithm::C14N => {
                // TODO: Implement C14N canonicalization
                Ok(self.normalize_whitespace(xml))
            }
            crate::CanonicalizationAlgorithm::C14N11 => {
                // TODO: Implement C14N11 canonicalization
                Ok(self.normalize_whitespace(xml))
            }
            crate::CanonicalizationAlgorithm::DbC14N => {
                // TODO: Implement DB-C14N canonicalization
                Ok(self.normalize_whitespace(xml))
            }
            crate::CanonicalizationAlgorithm::Custom(_rules) => {
                // TODO: Implement custom canonicalization
                Ok(self.normalize_whitespace(xml))
            }
        }
    }

    /// Normalize whitespace for comparison
    fn normalize_whitespace(&self, xml: &str) -> String {
        // Basic whitespace normalization
        xml.lines()
            .map(|line| line.trim())
            .filter(|line| !line.is_empty())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Compare XML structures (placeholder for actual implementation)
    fn _compare_structures(&self, _original: &str, _rebuilt: &str) -> bool {
        // TODO: Implement deep structural comparison
        // This would compare the parsed AST structures rather than string content
        true
    }

    /// Perform comprehensive fidelity analysis
    pub fn analyze_fidelity(&self, original_xml: &str) -> Result<FidelityAnalysis, BuildError> {
        let start_time = Instant::now();

        // Analyze elements preservation
        let element_analysis = self.analyze_elements(original_xml)?;

        // Analyze attributes preservation
        let attribute_analysis = self.analyze_attributes(original_xml)?;

        // Analyze comments preservation
        let comment_analysis = self.analyze_comments(original_xml)?;

        // Analyze extensions preservation
        let extension_analysis = self.analyze_extensions(original_xml)?;

        // Analyze namespace preservation
        let namespace_analysis = self.analyze_namespaces(original_xml)?;

        let analysis_time = start_time.elapsed();

        let overall_score =
            self.calculate_overall_score(&element_analysis, &attribute_analysis, &comment_analysis);

        Ok(FidelityAnalysis {
            element_analysis,
            attribute_analysis,
            comment_analysis,
            extension_analysis,
            namespace_analysis,
            analysis_time,
            overall_score,
        })
    }

    /// Analyze element preservation
    fn analyze_elements(&self, xml: &str) -> Result<ElementAnalysis, BuildError> {
        let mut reader = quick_xml::Reader::from_str(xml);
        let mut elements_found = std::collections::HashMap::new();
        let mut total_elements = 0;

        loop {
            match reader.read_event() {
                Ok(quick_xml::events::Event::Start(e)) | Ok(quick_xml::events::Event::Empty(e)) => {
                    total_elements += 1;
                    let name = String::from_utf8_lossy(e.name().as_ref()).to_string();
                    *elements_found.entry(name).or_insert(0) += 1;
                }
                Ok(quick_xml::events::Event::Eof) => break,
                Ok(_) => continue,
                Err(e) => {
                    return Err(BuildError::InvalidFormat {
                        field: "xml".to_string(),
                        message: format!("XML parsing error: {}", e),
                    })
                }
            }
        }

        Ok(ElementAnalysis {
            total_elements,
            elements_by_type: elements_found,
            unknown_elements: 0, // Would be calculated by comparing against schema
            preserved_elements: total_elements, // Placeholder
        })
    }

    /// Analyze attribute preservation
    fn analyze_attributes(&self, xml: &str) -> Result<AttributeAnalysis, BuildError> {
        let mut reader = quick_xml::Reader::from_str(xml);
        let mut total_attributes = 0;
        let mut attributes_by_element = std::collections::HashMap::new();

        loop {
            match reader.read_event() {
                Ok(quick_xml::events::Event::Start(e)) | Ok(quick_xml::events::Event::Empty(e)) => {
                    let element_name = String::from_utf8_lossy(e.name().as_ref()).to_string();
                    let attr_count = e.attributes().count();
                    total_attributes += attr_count;
                    *attributes_by_element.entry(element_name).or_insert(0) += attr_count;
                }
                Ok(quick_xml::events::Event::Eof) => break,
                Ok(_) => continue,
                Err(e) => {
                    return Err(BuildError::InvalidFormat {
                        field: "xml".to_string(),
                        message: format!("XML parsing error: {}", e),
                    })
                }
            }
        }

        Ok(AttributeAnalysis {
            total_attributes,
            attributes_by_element,
            unknown_attributes: 0,                  // Would be calculated
            preserved_attributes: total_attributes, // Placeholder
        })
    }

    /// Analyze comment preservation
    fn analyze_comments(&self, xml: &str) -> Result<CommentAnalysis, BuildError> {
        let comments = if let Ok(comment_regex) = regex::Regex::new(r"<!--.*?-->") {
            comment_regex.find_iter(xml).collect()
        } else {
            Vec::new()
        };

        Ok(CommentAnalysis {
            total_comments: comments.len(),
            document_level_comments: 0, // Would analyze position
            element_level_comments: comments.len(), // Placeholder
            inline_comments: 0,
            preserved_comments: if self.fidelity_options.preserve_comments {
                comments.len()
            } else {
                0
            },
        })
    }

    /// Analyze extension preservation
    fn analyze_extensions(&self, xml: &str) -> Result<ExtensionAnalysis, BuildError> {
        // Look for non-standard namespaces
        let mut extension_namespaces = std::collections::HashMap::new();

        if let Ok(namespace_regex) = regex::Regex::new(r#"xmlns:(\w+)=['"]([^'"]+)['"]"#) {
            for caps in namespace_regex.captures_iter(xml) {
                if let (Some(prefix_match), Some(uri_match)) = (caps.get(1), caps.get(2)) {
                    let prefix = prefix_match.as_str();
                    let uri = uri_match.as_str();

                    // Check if this is a known DDEX namespace
                    if !uri.contains("ddex.net") && !uri.contains("w3.org") {
                        extension_namespaces.insert(prefix.to_string(), uri.to_string());
                    }
                }
            }
        }

        let extension_count = extension_namespaces.len();
        Ok(ExtensionAnalysis {
            total_extensions: extension_count,
            extension_namespaces,
            known_extensions: 0, // Would classify based on known patterns
            unknown_extensions: extension_count,
            preserved_extensions: if self.fidelity_options.preserve_extensions {
                extension_count
            } else {
                0
            },
        })
    }

    /// Analyze namespace preservation
    fn analyze_namespaces(&self, xml: &str) -> Result<NamespaceAnalysis, BuildError> {
        let mut namespaces = std::collections::HashMap::new();
        let mut default_namespace = None;

        if let Ok(namespace_regex) = regex::Regex::new(r#"xmlns(?::(\w+))?=['"]([^'"]+)['"]"#) {
            for caps in namespace_regex.captures_iter(xml) {
                if let Some(prefix_match) = caps.get(1) {
                    if let Some(uri_match) = caps.get(2) {
                        let prefix = prefix_match.as_str();
                        let uri = uri_match.as_str();
                        namespaces.insert(prefix.to_string(), uri.to_string());
                    }
                } else if let Some(uri_match) = caps.get(2) {
                    default_namespace = Some(uri_match.as_str().to_string());
                }
            }
        }

        let total_namespaces = namespaces.len() + if default_namespace.is_some() { 1 } else { 0 };
        let preserved_namespaces = namespaces.len(); // Placeholder

        Ok(NamespaceAnalysis {
            total_namespaces,
            prefixed_namespaces: namespaces,
            default_namespace,
            preserved_namespaces,
        })
    }

    /// Calculate overall fidelity score
    fn calculate_overall_score(
        &self,
        element_analysis: &ElementAnalysis,
        attribute_analysis: &AttributeAnalysis,
        comment_analysis: &CommentAnalysis,
    ) -> f64 {
        let element_score = if element_analysis.total_elements > 0 {
            element_analysis.preserved_elements as f64 / element_analysis.total_elements as f64
        } else {
            1.0
        };

        let attribute_score = if attribute_analysis.total_attributes > 0 {
            attribute_analysis.preserved_attributes as f64
                / attribute_analysis.total_attributes as f64
        } else {
            1.0
        };

        let comment_score = if comment_analysis.total_comments > 0 {
            comment_analysis.preserved_comments as f64 / comment_analysis.total_comments as f64
        } else {
            1.0
        };

        // Weighted average (elements are most important)
        (element_score * 0.5) + (attribute_score * 0.3) + (comment_score * 0.2)
    }
}

/// Round-trip test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoundTripResult {
    /// Whether round-trip was successful
    pub success: bool,
    /// Original XML input
    pub original_xml: String,
    /// XML after build process
    pub rebuilt_xml: String,
    /// Whether XMLs are byte-identical after canonicalization
    pub byte_identical: bool,
    /// Differences found (if any)
    pub differences: Vec<String>,
    /// Time taken for round-trip test
    pub test_time: Duration,
}

/// Comprehensive fidelity analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FidelityAnalysis {
    /// Element analysis
    pub element_analysis: ElementAnalysis,
    /// Attribute analysis  
    pub attribute_analysis: AttributeAnalysis,
    /// Comment analysis
    pub comment_analysis: CommentAnalysis,
    /// Extension analysis
    pub extension_analysis: ExtensionAnalysis,
    /// Namespace analysis
    pub namespace_analysis: NamespaceAnalysis,
    /// Time taken for analysis
    pub analysis_time: Duration,
    /// Overall fidelity score (0.0 - 1.0)
    pub overall_score: f64,
}

/// Element preservation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElementAnalysis {
    /// Total number of elements
    pub total_elements: usize,
    /// Elements by type/name
    pub elements_by_type: std::collections::HashMap<String, usize>,
    /// Unknown elements (not in schema)
    pub unknown_elements: usize,
    /// Elements preserved after round-trip
    pub preserved_elements: usize,
}

/// Attribute preservation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeAnalysis {
    /// Total number of attributes
    pub total_attributes: usize,
    /// Attributes by element type
    pub attributes_by_element: std::collections::HashMap<String, usize>,
    /// Unknown attributes (not in schema)
    pub unknown_attributes: usize,
    /// Attributes preserved after round-trip
    pub preserved_attributes: usize,
}

/// Comment preservation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommentAnalysis {
    /// Total number of comments
    pub total_comments: usize,
    /// Document-level comments
    pub document_level_comments: usize,
    /// Element-level comments
    pub element_level_comments: usize,
    /// Inline comments
    pub inline_comments: usize,
    /// Comments preserved after round-trip
    pub preserved_comments: usize,
}

/// Extension preservation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionAnalysis {
    /// Total number of extensions
    pub total_extensions: usize,
    /// Extension namespaces found
    pub extension_namespaces: std::collections::HashMap<String, String>,
    /// Known extensions (recognized patterns)
    pub known_extensions: usize,
    /// Unknown extensions
    pub unknown_extensions: usize,
    /// Extensions preserved after round-trip
    pub preserved_extensions: usize,
}

/// Namespace preservation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespaceAnalysis {
    /// Total number of namespaces
    pub total_namespaces: usize,
    /// Prefixed namespaces
    pub prefixed_namespaces: std::collections::HashMap<String, String>,
    /// Default namespace (if any)
    pub default_namespace: Option<String>,
    /// Namespaces preserved after round-trip
    pub preserved_namespaces: usize,
}

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

    #[test]
    fn test_round_trip_tester_creation() {
        let fidelity_options = FidelityOptions::default();
        let tester = RoundTripTester::new(fidelity_options);
        assert_eq!(tester.fidelity_options.enable_perfect_fidelity, false);
    }

    #[test]
    fn test_whitespace_normalization() {
        let fidelity_options = FidelityOptions::default();
        let tester = RoundTripTester::new(fidelity_options);

        let xml = "  <test>  \n  <inner>value</inner>  \n  </test>  ";
        let normalized = tester.normalize_whitespace(xml);

        assert_eq!(normalized, "<test>\n<inner>value</inner>\n</test>");
    }

    #[test]
    fn test_element_analysis() {
        let fidelity_options = FidelityOptions::default();
        let tester = RoundTripTester::new(fidelity_options);

        let xml = r#"<root><element1/><element2><element3/></element2></root>"#;
        let analysis = tester.analyze_elements(xml).unwrap();

        assert_eq!(analysis.total_elements, 4);
        assert!(analysis.elements_by_type.contains_key("root"));
        assert!(analysis.elements_by_type.contains_key("element1"));
    }

    #[test]
    fn test_comment_analysis() {
        let fidelity_options = FidelityOptions::default();
        let tester = RoundTripTester::new(fidelity_options);

        let xml = r#"<root><!-- comment 1 --><element/><!-- comment 2 --></root>"#;
        let analysis = tester.analyze_comments(xml).unwrap();

        assert_eq!(analysis.total_comments, 2);
    }

    #[test]
    fn test_extension_analysis() {
        let fidelity_options = FidelityOptions::default();
        let tester = RoundTripTester::new(fidelity_options);

        let xml = r#"<root xmlns:spotify="http://spotify.com/ddex" xmlns:custom="http://example.com/custom">
            <spotify:trackId>123</spotify:trackId>
        </root>"#;

        let analysis = tester.analyze_extensions(xml).unwrap();
        assert!(analysis.extension_namespaces.contains_key("spotify"));
        assert!(analysis.extension_namespaces.contains_key("custom"));
    }
}