rs-pfcp 0.4.0

High-performance Rust implementation of PFCP (Packet Forwarding Control Protocol) for 5G networks with 100% 3GPP TS 29.244 Release 18 compliance
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Comparison result types.

use super::MessageDiff;
use crate::ie::IeType;
use crate::message::MsgType;

/// Result of a message comparison operation.
///
/// Contains detailed information about the comparison, including:
/// - Overall match status
/// - Header comparison result
/// - IE-level matches and mismatches
/// - Optional detailed diff
///
/// # Examples
///
/// ```rust,no_run
/// use rs_pfcp::comparison::MessageComparator;
/// # use rs_pfcp::message::Message;
/// # fn example(msg1: &dyn Message, msg2: &dyn Message) -> Result<(), rs_pfcp::error::PfcpError> {
///
/// let result = MessageComparator::new(msg1, msg2)
///     .ignore_sequence()
///     .compare()?;
///
/// if result.is_match() {
///     println!("Messages match!");
/// } else {
///     println!("Differences found: {}", result.summary());
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct ComparisonResult {
    /// Message types being compared
    pub left_type: MsgType,
    pub right_type: MsgType,

    /// Overall match status
    pub is_match: bool,

    /// Header comparison result
    pub header_match: HeaderMatch,

    /// IE-level comparison results (successful matches)
    pub ie_matches: Vec<IeMatch>,

    /// IEs that didn't match
    pub ie_mismatches: Vec<IeMismatch>,

    /// IEs only in left message
    pub left_only_ies: Vec<IeType>,

    /// IEs only in right message
    pub right_only_ies: Vec<IeType>,

    /// Detailed diff (if requested)
    pub diff: Option<MessageDiff>,

    /// Summary statistics
    pub stats: ComparisonStats,
}

impl ComparisonResult {
    /// Returns true if messages match according to comparison options.
    pub fn is_match(&self) -> bool {
        self.is_match
    }

    /// Returns true if comparison failed.
    pub fn is_mismatch(&self) -> bool {
        !self.is_match
    }

    /// Get the detailed diff, generating it if not already present.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use rs_pfcp::comparison::{MessageComparator, ComparisonResult};
    /// # use rs_pfcp::message::Message;
    /// # fn example(result: ComparisonResult) {
    /// let diff = result.into_diff();
    /// println!("{}", diff);
    /// # }
    /// ```
    pub fn into_diff(mut self) -> MessageDiff {
        self.diff
            .take()
            .unwrap_or_else(|| MessageDiff::from_result(&self))
    }

    /// Get a summary string describing the comparison.
    ///
    /// Returns a concise, single-line summary of the comparison result.
    pub fn summary(&self) -> String {
        if self.is_match {
            format!(
                "Messages match: {} IEs compared, {} matched",
                self.stats.total_ies_compared,
                self.ie_matches.len()
            )
        } else {
            let mut parts = Vec::new();

            if !self.ie_mismatches.is_empty() {
                parts.push(format!("{} mismatch(es)", self.ie_mismatches.len()));
            }
            if !self.left_only_ies.is_empty() {
                parts.push(format!("{} missing from right", self.left_only_ies.len()));
            }
            if !self.right_only_ies.is_empty() {
                parts.push(format!("{} missing from left", self.right_only_ies.len()));
            }

            format!("Messages differ: {}", parts.join(", "))
        }
    }

    /// Get detailed report as formatted string.
    ///
    /// Returns a multi-line report with complete comparison details.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use rs_pfcp::comparison::{MessageComparator, ComparisonResult};
    /// # use rs_pfcp::message::Message;
    /// # fn example(result: ComparisonResult) {
    /// println!("{}", result.report());
    /// # }
    /// ```
    pub fn report(&self) -> String {
        let mut report = String::new();
        report.push_str(&format!(
            "Comparison Result: {}\n",
            if self.is_match { "MATCH" } else { "MISMATCH" }
        ));
        report.push_str(&format!("Left:  {:?}\n", self.left_type));
        report.push_str(&format!("Right: {:?}\n\n", self.right_type));

        // Header status
        report.push_str(&format!("Header: {}\n", self.header_match.summary()));

        // Statistics
        report.push_str("\nStatistics:\n");
        report.push_str(&format!(
            "  IEs compared: {}\n",
            self.stats.total_ies_compared
        ));
        report.push_str(&format!("  Exact matches: {}\n", self.stats.exact_matches));
        report.push_str(&format!(
            "  Semantic matches: {}\n",
            self.stats.semantic_matches
        ));
        report.push_str(&format!("  Mismatches: {}\n", self.stats.mismatches));
        report.push_str(&format!("  Ignored IEs: {}\n", self.stats.ignored_ies));

        // Left-only IEs
        if !self.left_only_ies.is_empty() {
            report.push_str("\nIEs only in left:\n");
            for ie_type in &self.left_only_ies {
                report.push_str(&format!("  - {:?}\n", ie_type));
            }
        }

        // Right-only IEs
        if !self.right_only_ies.is_empty() {
            report.push_str("\nIEs only in right:\n");
            for ie_type in &self.right_only_ies {
                report.push_str(&format!("  - {:?}\n", ie_type));
            }
        }

        // Detailed mismatches
        if !self.ie_mismatches.is_empty() {
            report.push_str("\nMismatches:\n");
            for mismatch in &self.ie_mismatches {
                report.push_str(&format!(
                    "  - {:?}: {}\n",
                    mismatch.ie_type, mismatch.reason
                ));
                if let Some(ref context) = mismatch.context {
                    report.push_str(&format!("    Context: {}\n", context));
                }
            }
        }

        report
    }
}

/// Header comparison result.
///
/// Indicates which header fields matched and which didn't.
#[derive(Debug, Clone, Copy)]
pub struct HeaderMatch {
    /// Message type match status (always checked)
    pub message_type_match: bool,

    /// Sequence number match status (None if ignored)
    pub sequence_match: Option<bool>,

    /// SEID match status (None if ignored or not applicable)
    pub seid_match: Option<bool>,

    /// Priority match status (None if ignored)
    pub priority_match: Option<bool>,
}

impl HeaderMatch {
    /// Returns true if all checked header fields match.
    pub fn is_complete_match(&self) -> bool {
        self.message_type_match
            && self.sequence_match.unwrap_or(true)
            && self.seid_match.unwrap_or(true)
            && self.priority_match.unwrap_or(true)
    }

    /// Get a summary string for the header match.
    pub fn summary(&self) -> String {
        if self.is_complete_match() {
            "Match".to_string()
        } else {
            let mut parts = vec![];
            if !self.message_type_match {
                parts.push("type");
            }
            if self.sequence_match == Some(false) {
                parts.push("sequence");
            }
            if self.seid_match == Some(false) {
                parts.push("seid");
            }
            if self.priority_match == Some(false) {
                parts.push("priority");
            }
            format!("Mismatch in: {}", parts.join(", "))
        }
    }
}

/// Successful IE match.
///
/// Records that an IE was successfully compared and matched.
#[derive(Debug, Clone)]
pub struct IeMatch {
    /// The IE type that matched
    pub ie_type: IeType,

    /// How the IE matched
    pub match_type: IeMatchType,
}

/// Type of IE match that occurred.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IeMatchType {
    /// Exact byte-for-byte match
    Exact,

    /// Semantically equivalent but not byte-identical
    Semantic,

    /// Multiple instances all matched
    MultipleMatched,

    /// Grouped IE matched via deep recursive comparison of child IEs
    DeepGrouped,
}

/// IE that didn't match between messages.
///
/// Contains information about why the IE failed to match.
#[derive(Debug, Clone)]
pub struct IeMismatch {
    /// The IE type that didn't match
    pub ie_type: IeType,

    /// Why the IE didn't match
    pub reason: MismatchReason,

    /// Payload from left message (if requested in options)
    pub left_payload: Option<Vec<u8>>,

    /// Payload from right message (if requested in options)
    pub right_payload: Option<Vec<u8>>,

    /// Context for grouped IEs (shows parent path)
    ///
    /// For example: "CreatePdr > Pdi > SourceInterface"
    pub context: Option<String>,
}

/// Reason why an IE didn't match.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MismatchReason {
    /// Payload values differ
    ValueMismatch,

    /// Different number of instances
    CountMismatch {
        /// Count in left message
        left_count: usize,
        /// Count in right message
        right_count: usize,
    },

    /// IE present in left but not right
    MissingInRight,

    /// IE present in right but not left
    MissingInLeft,

    /// Grouped IE children don't match
    GroupedIeMismatch {
        /// Number of child IE mismatches
        child_mismatches: usize,
        /// Number of IEs only in left message
        missing_in_right: usize,
        /// Number of IEs only in right message
        missing_in_left: usize,
    },

    /// Custom semantic comparison failed
    SemanticMismatch {
        /// Details about the semantic mismatch
        details: String,
    },
}

impl std::fmt::Display for MismatchReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MismatchReason::ValueMismatch => write!(f, "values differ"),
            MismatchReason::CountMismatch {
                left_count,
                right_count,
            } => write!(f, "count differs ({} vs {})", left_count, right_count),
            MismatchReason::MissingInRight => write!(f, "missing in right message"),
            MismatchReason::MissingInLeft => write!(f, "missing in left message"),
            MismatchReason::GroupedIeMismatch {
                child_mismatches,
                missing_in_right,
                missing_in_left,
            } => {
                let mut parts = vec![format!("{} child mismatch(es)", child_mismatches)];
                if *missing_in_right > 0 {
                    parts.push(format!("{} missing in right", missing_in_right));
                }
                if *missing_in_left > 0 {
                    parts.push(format!("{} missing in left", missing_in_left));
                }
                write!(f, "grouped IE: {}", parts.join(", "))
            }
            MismatchReason::SemanticMismatch { details } => {
                write!(f, "semantic mismatch: {}", details)
            }
        }
    }
}

/// Statistics about the comparison.
///
/// Tracks how many IEs were compared and what the outcomes were.
#[derive(Debug, Clone, Copy, Default)]
pub struct ComparisonStats {
    /// Total number of IEs compared
    pub total_ies_compared: usize,

    /// Number of exact matches
    pub exact_matches: usize,

    /// Number of semantic matches
    pub semantic_matches: usize,

    /// Number of mismatches
    pub mismatches: usize,

    /// Number of IEs ignored (filtered out)
    pub ignored_ies: usize,

    /// Number of grouped IEs compared (recursively)
    pub grouped_ies_compared: usize,
}

impl ComparisonStats {
    /// Create new empty statistics.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the total number of successful matches.
    pub fn total_matches(&self) -> usize {
        self.exact_matches + self.semantic_matches
    }

    /// Get the match rate as a percentage (0.0 to 1.0).
    pub fn match_rate(&self) -> f64 {
        if self.total_ies_compared == 0 {
            1.0 // No IEs to compare = perfect match
        } else {
            self.total_matches() as f64 / self.total_ies_compared as f64
        }
    }
}

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

    #[test]
    fn test_header_match_complete() {
        let header = HeaderMatch {
            message_type_match: true,
            sequence_match: Some(true),
            seid_match: Some(true),
            priority_match: Some(true),
        };
        assert!(header.is_complete_match());
        assert_eq!(header.summary(), "Match");
    }

    #[test]
    fn test_header_match_with_ignores() {
        let header = HeaderMatch {
            message_type_match: true,
            sequence_match: None, // Ignored
            seid_match: None,     // Ignored
            priority_match: None, // Ignored
        };
        assert!(header.is_complete_match());
        assert_eq!(header.summary(), "Match");
    }

    #[test]
    fn test_header_match_mismatch() {
        let header = HeaderMatch {
            message_type_match: true,
            sequence_match: Some(false),
            seid_match: Some(true),
            priority_match: Some(true),
        };
        assert!(!header.is_complete_match());
        assert_eq!(header.summary(), "Mismatch in: sequence");
    }

    #[test]
    fn test_mismatch_reason_display() {
        assert_eq!(MismatchReason::ValueMismatch.to_string(), "values differ");
        assert_eq!(
            MismatchReason::CountMismatch {
                left_count: 2,
                right_count: 3
            }
            .to_string(),
            "count differs (2 vs 3)"
        );
        assert_eq!(
            MismatchReason::MissingInRight.to_string(),
            "missing in right message"
        );
    }

    #[test]
    fn test_comparison_stats_default() {
        let stats = ComparisonStats::default();
        assert_eq!(stats.total_ies_compared, 0);
        assert_eq!(stats.exact_matches, 0);
        assert_eq!(stats.mismatches, 0);
        assert_eq!(stats.match_rate(), 1.0);
    }

    #[test]
    fn test_comparison_stats_match_rate() {
        let stats = ComparisonStats {
            total_ies_compared: 10,
            exact_matches: 7,
            semantic_matches: 2,
            mismatches: 1,
            ..Default::default()
        };

        assert_eq!(stats.total_matches(), 9);
        assert_eq!(stats.match_rate(), 0.9);
    }

    #[test]
    fn test_comparison_result_is_match() {
        let result = ComparisonResult {
            left_type: MsgType::HeartbeatRequest,
            right_type: MsgType::HeartbeatRequest,
            is_match: true,
            header_match: HeaderMatch {
                message_type_match: true,
                sequence_match: Some(true),
                seid_match: None,
                priority_match: None,
            },
            ie_matches: vec![],
            ie_mismatches: vec![],
            left_only_ies: vec![],
            right_only_ies: vec![],
            diff: None,
            stats: ComparisonStats::default(),
        };

        assert!(result.is_match());
        assert!(!result.is_mismatch());
    }

    #[test]
    fn test_comparison_result_summary_match() {
        let stats = ComparisonStats {
            total_ies_compared: 5,
            ..Default::default()
        };

        let result = ComparisonResult {
            left_type: MsgType::HeartbeatRequest,
            right_type: MsgType::HeartbeatRequest,
            is_match: true,
            header_match: HeaderMatch {
                message_type_match: true,
                sequence_match: Some(true),
                seid_match: None,
                priority_match: None,
            },
            ie_matches: vec![IeMatch {
                ie_type: IeType::Cause,
                match_type: IeMatchType::Exact,
            }],
            ie_mismatches: vec![],
            left_only_ies: vec![],
            right_only_ies: vec![],
            diff: None,
            stats,
        };

        assert_eq!(
            result.summary(),
            "Messages match: 5 IEs compared, 1 matched"
        );
    }

    #[test]
    fn test_comparison_result_summary_mismatch() {
        let result = ComparisonResult {
            left_type: MsgType::HeartbeatRequest,
            right_type: MsgType::HeartbeatRequest,
            is_match: false,
            header_match: HeaderMatch {
                message_type_match: true,
                sequence_match: Some(true),
                seid_match: None,
                priority_match: None,
            },
            ie_matches: vec![],
            ie_mismatches: vec![IeMismatch {
                ie_type: IeType::Cause,
                reason: MismatchReason::ValueMismatch,
                left_payload: None,
                right_payload: None,
                context: None,
            }],
            left_only_ies: vec![IeType::NodeId],
            right_only_ies: vec![IeType::RecoveryTimeStamp],
            diff: None,
            stats: ComparisonStats::default(),
        };

        assert_eq!(
            result.summary(),
            "Messages differ: 1 mismatch(es), 1 missing from right, 1 missing from left"
        );
    }
}