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
//! Type definitions for DDEX semantic diffing

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Result of a semantic diff operation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DiffResult {
    /// No changes detected
    Unchanged,
    /// Content was added
    Added,
    /// Content was modified  
    Modified,
    /// Content was removed
    Removed,
    /// Element was moved
    Moved {
        /// Original path
        from: DiffPath,
        /// New path
        to: DiffPath,
    },
}

/// Complete set of changes between two DDEX documents
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeSet {
    /// Individual semantic changes
    pub changes: Vec<SemanticChange>,

    /// Summary statistics
    pub summary: ChangeSummary,

    /// Additional metadata about the diff
    pub metadata: IndexMap<String, String>,

    /// Timestamp when diff was performed
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

impl ChangeSet {
    /// Create a new empty changeset
    pub fn new() -> Self {
        Self {
            changes: Vec::new(),
            summary: ChangeSummary::default(),
            metadata: IndexMap::new(),
            timestamp: chrono::Utc::now(),
        }
    }

    /// Add a semantic change to the changeset
    pub fn add_change(&mut self, change: SemanticChange) {
        // Update summary statistics
        match change.change_type {
            ChangeType::ElementAdded | ChangeType::AttributeAdded => {
                self.summary.additions += 1;
            }
            ChangeType::ElementRemoved | ChangeType::AttributeRemoved => {
                self.summary.deletions += 1;
            }
            ChangeType::ElementModified
            | ChangeType::AttributeModified
            | ChangeType::TextModified
            | ChangeType::ElementRenamed => {
                self.summary.modifications += 1;
            }
            ChangeType::ElementMoved => {
                self.summary.moves += 1;
            }
        }

        if change.is_critical {
            self.summary.critical_changes += 1;
        }

        self.changes.push(change);
        self.summary.total_changes = self.changes.len();
    }

    /// Check if there are any changes
    pub fn has_changes(&self) -> bool {
        !self.changes.is_empty()
    }

    /// Get changes by criticality
    pub fn critical_changes(&self) -> Vec<&SemanticChange> {
        self.changes.iter().filter(|c| c.is_critical).collect()
    }

    /// Get changes by type
    pub fn changes_by_type(&self, change_type: ChangeType) -> Vec<&SemanticChange> {
        self.changes
            .iter()
            .filter(|c| c.change_type == change_type)
            .collect()
    }

    /// Get overall impact level
    pub fn impact_level(&self) -> ImpactLevel {
        if self.summary.critical_changes > 0 {
            ImpactLevel::High
        } else if self.summary.total_changes > 10 {
            ImpactLevel::Medium
        } else if self.summary.total_changes > 0 {
            ImpactLevel::Low
        } else {
            ImpactLevel::None
        }
    }
}

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

/// A single semantic change in a DDEX document
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticChange {
    /// Path to the changed element/attribute
    pub path: DiffPath,

    /// Type of change
    pub change_type: ChangeType,

    /// Previous value (if any)
    pub old_value: Option<String>,

    /// New value (if any)
    pub new_value: Option<String>,

    /// Whether this change is business-critical
    pub is_critical: bool,

    /// Human-readable description of the change
    pub description: String,
}

/// Path to a specific location in a DDEX document
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DiffPath {
    /// Path segments (element names, attribute names)
    pub segments: Vec<PathSegment>,
}

impl DiffPath {
    /// Create root path
    pub fn root() -> Self {
        Self {
            segments: Vec::new(),
        }
    }

    /// Create path with a single element
    pub fn element(name: &str) -> Self {
        Self {
            segments: vec![PathSegment::Element(name.to_string())],
        }
    }

    /// Add an element to the path
    pub fn with_element(&self, name: &str) -> Self {
        let mut segments = self.segments.clone();
        segments.push(PathSegment::Element(name.to_string()));
        Self { segments }
    }

    /// Add an attribute to the path
    pub fn with_attribute(&self, name: &str) -> Self {
        let mut segments = self.segments.clone();
        segments.push(PathSegment::Attribute(name.to_string()));
        Self { segments }
    }

    /// Add text content to the path
    pub fn with_text(&self) -> Self {
        let mut segments = self.segments.clone();
        segments.push(PathSegment::Text);
        Self { segments }
    }

    /// Add an index to the path for array elements
    pub fn with_index(&self, index: usize) -> Self {
        let mut segments = self.segments.clone();
        segments.push(PathSegment::Index(index));
        Self { segments }
    }

    /// Get the path as a slash-separated string
    pub fn to_string(&self) -> String {
        if self.segments.is_empty() {
            return "/".to_string();
        }

        let mut path = String::new();
        for segment in &self.segments {
            path.push('/');
            match segment {
                PathSegment::Element(name) => path.push_str(name),
                PathSegment::Attribute(name) => {
                    path.push('@');
                    path.push_str(name);
                }
                PathSegment::Text => path.push_str("#text"),
                PathSegment::Index(idx) => path.push_str(&format!("[{}]", idx)),
            }
        }
        path
    }
}

impl fmt::Display for DiffPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

/// Individual segment of a diff path
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PathSegment {
    /// XML element name
    Element(String),
    /// XML attribute name
    Attribute(String),
    /// Text content
    Text,
    /// Array index for repeated elements
    Index(usize),
}

/// Type of semantic change
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ChangeType {
    /// Element was added
    ElementAdded,
    /// Element was removed
    ElementRemoved,
    /// Element was modified
    ElementModified,
    /// Element was renamed
    ElementRenamed,
    /// Element was moved
    ElementMoved,
    /// Attribute was added
    AttributeAdded,
    /// Attribute was removed
    AttributeRemoved,
    /// Attribute was modified
    AttributeModified,
    /// Text content was modified
    TextModified,
}

impl fmt::Display for ChangeType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            ChangeType::ElementAdded => "Element Added",
            ChangeType::ElementRemoved => "Element Removed",
            ChangeType::ElementModified => "Element Modified",
            ChangeType::ElementRenamed => "Element Renamed",
            ChangeType::ElementMoved => "Element Moved",
            ChangeType::AttributeAdded => "Attribute Added",
            ChangeType::AttributeRemoved => "Attribute Removed",
            ChangeType::AttributeModified => "Attribute Modified",
            ChangeType::TextModified => "Text Modified",
        };
        write!(f, "{}", s)
    }
}

/// Summary of changes in a changeset
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ChangeSummary {
    /// Total number of changes
    pub total_changes: usize,
    /// Number of additions
    pub additions: usize,
    /// Number of deletions
    pub deletions: usize,
    /// Number of modifications
    pub modifications: usize,
    /// Number of moves
    pub moves: usize,
    /// Number of critical changes
    pub critical_changes: usize,
}

impl ChangeSummary {
    /// Check if there are any changes
    pub fn has_changes(&self) -> bool {
        self.total_changes > 0
    }

    /// Get a brief summary string
    pub fn summary_string(&self) -> String {
        if !self.has_changes() {
            return "No changes".to_string();
        }

        let mut parts = Vec::new();

        if self.additions > 0 {
            parts.push(format!("{} added", self.additions));
        }
        if self.deletions > 0 {
            parts.push(format!("{} deleted", self.deletions));
        }
        if self.modifications > 0 {
            parts.push(format!("{} modified", self.modifications));
        }
        if self.moves > 0 {
            parts.push(format!("{} moved", self.moves));
        }

        let summary = parts.join(", ");

        if self.critical_changes > 0 {
            format!("{} ({} critical)", summary, self.critical_changes)
        } else {
            summary
        }
    }
}

/// Impact level of changes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ImpactLevel {
    /// No changes
    None,
    /// Low impact changes (formatting, minor additions)
    Low,
    /// Medium impact changes (significant additions/modifications)
    Medium,
    /// High impact changes (critical business fields affected)
    High,
}

impl fmt::Display for ImpactLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            ImpactLevel::None => "None",
            ImpactLevel::Low => "Low",
            ImpactLevel::Medium => "Medium",
            ImpactLevel::High => "High",
        };
        write!(f, "{}", s)
    }
}

/// Context information for understanding a change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeContext {
    /// Related DDEX entity (Release, Resource, Deal, etc.)
    pub entity_type: Option<String>,

    /// Entity identifier if available
    pub entity_id: Option<String>,

    /// Business context (pricing, territory, rights, etc.)
    pub business_context: Option<String>,

    /// Technical context (schema version, etc.)
    pub technical_context: IndexMap<String, String>,
}

/// Configuration for what constitutes a significant change
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeSignificance {
    /// Fields that are considered critical
    pub critical_fields: Vec<String>,

    /// Fields that should be ignored
    pub ignored_fields: Vec<String>,

    /// Numeric tolerance for comparisons
    pub numeric_tolerance: f64,

    /// Whether to ignore order changes
    pub ignore_order: bool,
}

impl Default for ChangeSignificance {
    fn default() -> Self {
        Self {
            critical_fields: vec![
                "CommercialModelType".to_string(),
                "TerritoryCode".to_string(),
                "Price".to_string(),
                "ValidityPeriod".to_string(),
                "ReleaseDate".to_string(),
                "UPC".to_string(),
                "ISRC".to_string(),
            ],
            ignored_fields: vec![
                "MessageId".to_string(),
                "MessageCreatedDateTime".to_string(),
            ],
            numeric_tolerance: 0.01,
            ignore_order: true,
        }
    }
}

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

    #[test]
    fn test_diff_path() {
        let path = DiffPath::root()
            .with_element("Release")
            .with_attribute("ReleaseId");

        assert_eq!(path.to_string(), "/Release/@ReleaseId");
    }

    #[test]
    fn test_changeset() {
        let mut changeset = ChangeSet::new();

        changeset.add_change(SemanticChange {
            path: DiffPath::element("Test"),
            change_type: ChangeType::ElementAdded,
            old_value: None,
            new_value: Some("new".to_string()),
            is_critical: true,
            description: "Test change".to_string(),
        });

        assert!(changeset.has_changes());
        assert_eq!(changeset.summary.total_changes, 1);
        assert_eq!(changeset.summary.critical_changes, 1);
        assert_eq!(changeset.impact_level(), ImpactLevel::High);
    }

    #[test]
    fn test_change_summary() {
        let mut summary = ChangeSummary::default();
        assert!(!summary.has_changes());
        assert_eq!(summary.summary_string(), "No changes");

        summary.additions = 2;
        summary.modifications = 1;
        summary.critical_changes = 1;
        summary.total_changes = 3;

        assert!(summary.has_changes());
        let summary_str = summary.summary_string();
        assert!(summary_str.contains("2 added"));
        assert!(summary_str.contains("1 modified"));
        assert!(summary_str.contains("1 critical"));
    }
}