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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Message comparison builder.

use super::{ComparisonOptions, ComparisonResult, IeMultiplicityMode, MessageDiff, OptionalIeMode};
use crate::error::PfcpError;
use crate::ie::IeType;
use crate::message::Message;

mod compare;

/// Builder for configuring and executing message comparisons.
///
/// Provides a fluent API for setting comparison options and executing
/// the comparison. Uses the builder pattern to enable flexible, chainable
/// configuration.
///
/// # 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()
///     .ignore_timestamps()
///     .compare()?;
///
/// assert!(result.is_match());
/// # Ok(())
/// # }
/// ```
///
/// # Type Safety
///
/// The builder enforces type safety at compile time:
/// - Messages must exist (references required)
/// - Options are strongly typed enums
/// - Configuration methods return `Self` for chaining
///
/// # Performance
///
/// The builder is zero-cost - all configuration is compile-time.
/// Only the final `compare()` or `diff()` call performs work.
pub struct MessageComparator<'a> {
    left: &'a dyn Message,
    right: &'a dyn Message,
    options: ComparisonOptions,
}

impl<'a> MessageComparator<'a> {
    // ========================================================================
    // Construction
    // ========================================================================

    /// Create a new comparator for two messages.
    ///
    /// # Panics
    ///
    /// Panics if the messages are of different types. Use `new_unchecked()`
    /// if you need to compare different message types (e.g., request/response pairs).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use rs_pfcp::comparison::MessageComparator;
    /// # use rs_pfcp::message::Message;
    /// # fn example(msg1: &dyn Message, msg2: &dyn Message) {
    ///
    /// let comparator = MessageComparator::new(msg1, msg2);
    /// # }
    /// ```
    pub fn new(left: &'a dyn Message, right: &'a dyn Message) -> Self {
        // Type check
        if left.msg_type() != right.msg_type() {
            panic!(
                "Cannot compare different message types: {:?} vs {:?}. Use new_unchecked() if this is intentional.",
                left.msg_type(),
                right.msg_type()
            );
        }

        Self {
            left,
            right,
            options: ComparisonOptions::default(),
        }
    }

    /// Create a comparator that allows comparing different message types.
    ///
    /// Useful for request/response pair analysis or cross-message validation.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use rs_pfcp::comparison::MessageComparator;
    /// # use rs_pfcp::message::Message;
    /// # fn example(request: &dyn Message, response: &dyn Message) {
    ///
    /// // Compare request/response pair
    /// let comparator = MessageComparator::new_unchecked(request, response);
    /// # }
    /// ```
    pub fn new_unchecked(left: &'a dyn Message, right: &'a dyn Message) -> Self {
        Self {
            left,
            right,
            options: ComparisonOptions::default(),
        }
    }

    // ========================================================================
    // Header Field Filtering
    // ========================================================================

    /// Ignore sequence numbers during comparison.
    ///
    /// Most useful for testing scenarios where sequence numbers are
    /// generated dynamically.
    ///
    /// # 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()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn ignore_sequence(mut self) -> Self {
        self.options.ignore_sequence = true;
        self
    }

    /// Include sequence numbers in comparison (default).
    pub fn include_sequence(mut self) -> Self {
        self.options.ignore_sequence = false;
        self
    }

    /// Ignore SEID (Session Endpoint Identifier) during comparison.
    ///
    /// Use this when comparing messages across different sessions
    /// or when SEID allocation is dynamic.
    pub fn ignore_seid(mut self) -> Self {
        self.options.ignore_seid = true;
        self
    }

    /// Ignore message priority field (usually safe to ignore).
    pub fn ignore_priority(mut self) -> Self {
        self.options.ignore_priority = true;
        self
    }

    /// Ignore all header fields except message type.
    ///
    /// Most lenient header comparison - only verifies message types match.
    pub fn ignore_all_header_fields(mut self) -> Self {
        self.options.ignore_sequence = true;
        self.options.ignore_seid = true;
        self.options.ignore_priority = true;
        self
    }

    // ========================================================================
    // Timestamp Handling
    // ========================================================================

    /// Ignore all timestamp IEs during comparison.
    ///
    /// This includes:
    /// - RecoveryTimeStamp (IE 96)
    /// - StartTime (IE 75)
    /// - EndTime (IE 76)
    /// - TimeOfFirstPacket (IE 69)
    /// - TimeOfLastPacket (IE 70)
    /// - ActivationTime (IE 163)
    /// - DeactivationTime (IE 164)
    /// - MonitoringTime (IE 33)
    pub fn ignore_timestamps(mut self) -> Self {
        self.options.ignore_timestamps = true;
        self
    }

    /// Ignore only RecoveryTimeStamp IE (most common case).
    pub fn ignore_recovery_timestamp(mut self) -> Self {
        self.options
            .ignored_ie_types
            .insert(IeType::RecoveryTimeStamp);
        self
    }

    /// Allow timestamp comparison within a tolerance window (in seconds).
    ///
    /// Useful for comparing messages captured at slightly different times.
    ///
    /// # 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> {
    /// // Allow 5 second tolerance
    /// let result = MessageComparator::new(msg1, msg2)
    ///     .timestamp_tolerance_secs(5)
    ///     .compare()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn timestamp_tolerance_secs(mut self, seconds: u32) -> Self {
        self.options.timestamp_tolerance_secs = Some(seconds);
        self
    }

    // ========================================================================
    // IE-Level Configuration
    // ========================================================================

    /// Ignore specific IE types during comparison.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use rs_pfcp::comparison::MessageComparator;
    /// use rs_pfcp::ie::IeType;
    /// # 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_ie_types(&[IeType::RecoveryTimeStamp, IeType::UrSeqn])
    ///     .compare()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn ignore_ie_types(mut self, ie_types: &[IeType]) -> Self {
        self.options.ignored_ie_types.extend(ie_types.iter());
        self
    }

    /// Only compare specific IE types, ignore all others.
    ///
    /// Useful for focused validation of critical IEs.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use rs_pfcp::comparison::MessageComparator;
    /// use rs_pfcp::ie::IeType;
    /// # use rs_pfcp::message::Message;
    /// # fn example(msg1: &dyn Message, msg2: &dyn Message) -> Result<(), rs_pfcp::error::PfcpError> {
    ///
    /// // Only verify PDR and FAR configuration
    /// let result = MessageComparator::new(msg1, msg2)
    ///     .focus_on_ie_types(&[IeType::CreatePdr, IeType::CreateFar])
    ///     .compare()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn focus_on_ie_types(mut self, ie_types: &[IeType]) -> Self {
        self.options.focus_ie_types = Some(ie_types.iter().copied().collect());
        self
    }

    /// Clear focus - compare all IEs (except those in ignore list).
    pub fn clear_focus(mut self) -> Self {
        self.options.focus_ie_types = None;
        self
    }

    // ========================================================================
    // IE Ordering and Multiplicity
    // ========================================================================

    /// Treat IE order as significant (strict mode).
    ///
    /// By default, IEs can appear in any order per 3GPP spec.
    /// Enable this for strict byte-for-byte comparison.
    pub fn strict_ie_order(mut self) -> Self {
        self.options.strict_ie_order = true;
        self
    }

    /// Allow IEs in any order (default, per 3GPP TS 29.244).
    pub fn unordered_ies(mut self) -> Self {
        self.options.strict_ie_order = false;
        self
    }

    /// How to handle multiple instances of the same IE type.
    ///
    /// - `ExactMatch`: Must have same count and values (order-independent)
    /// - `SetEquality`: Same IEs present, order matters
    /// - `Lenient`: Only check that at least one instance matches
    pub fn ie_multiplicity_mode(mut self, mode: IeMultiplicityMode) -> Self {
        self.options.ie_multiplicity_mode = mode;
        self
    }

    // ========================================================================
    // Optional IE Handling
    // ========================================================================

    /// How to handle IEs present in one message but not the other.
    ///
    /// - `Strict`: Any difference is a mismatch
    /// - `IgnoreMissing`: Only compare IEs present in both
    /// - `RequireLeft`: Right can have extra IEs, but left cannot
    /// - `RequireRight`: Left can have extra IEs, but right cannot
    pub fn optional_ie_mode(mut self, mode: OptionalIeMode) -> Self {
        self.options.optional_ie_mode = mode;
        self
    }

    /// Ignore IEs that are missing from either message.
    ///
    /// Equivalent to `optional_ie_mode(OptionalIeMode::IgnoreMissing)`.
    pub fn ignore_missing_ies(mut self) -> Self {
        self.options.optional_ie_mode = OptionalIeMode::IgnoreMissing;
        self
    }

    // ========================================================================
    // Grouped IE Handling
    // ========================================================================

    /// Enable deep comparison of grouped IEs.
    ///
    /// Recursively compares child IEs within grouped IEs like CreatePDR.
    /// This is the default behavior.
    pub fn deep_compare_grouped_ies(mut self) -> Self {
        self.options.deep_compare_grouped = true;
        self
    }

    /// Only compare grouped IEs at the top level (by payload bytes).
    ///
    /// Faster but less informative for debugging.
    pub fn shallow_compare_grouped_ies(mut self) -> Self {
        self.options.deep_compare_grouped = false;
        self
    }

    // ========================================================================
    // Value Comparison Strategies
    // ========================================================================

    /// Use semantic comparison for specific IE types.
    ///
    /// Example: For F-TEID, compare functionality (TEID + IP) rather than
    /// exact flag bits that might differ between implementations.
    pub fn semantic_comparison_for(mut self, ie_type: IeType) -> Self {
        self.options.semantic_ie_types.insert(ie_type);
        self
    }

    /// Enable semantic comparison for all supported IE types.
    ///
    /// This makes comparison more lenient, focusing on functional
    /// equivalence rather than byte-for-byte matching.
    pub fn semantic_mode(mut self) -> Self {
        self.options.use_semantic_comparison = true;
        self
    }

    // ========================================================================
    // Diff Generation Options
    // ========================================================================

    /// Generate detailed diff output.
    ///
    /// Includes all differences found, not just pass/fail.
    pub fn with_detailed_diff(mut self) -> Self {
        self.options.generate_diff = true;
        self
    }

    /// Limit the number of differences reported (for large messages).
    pub fn max_differences(mut self, max: usize) -> Self {
        self.options.max_reported_differences = Some(max);
        self
    }

    /// Include full IE payload in diff output (can be verbose).
    pub fn include_payload_in_diff(mut self) -> Self {
        self.options.include_payload_in_diff = true;
        self
    }

    // ========================================================================
    // Preset Configurations (Convenience Methods)
    // ========================================================================

    /// Preset: Test mode comparison.
    ///
    /// Ignores all transient fields suitable for unit testing:
    /// - Sequence numbers
    /// - Timestamps
    /// - Message priority
    /// - Unordered IEs
    ///
    /// # 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)
    ///     .test_mode()
    ///     .compare()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn test_mode(self) -> Self {
        self.ignore_sequence()
            .ignore_timestamps()
            .ignore_priority()
            .unordered_ies()
    }

    /// Preset: Strict mode comparison.
    ///
    /// Most restrictive - everything must match exactly.
    pub fn strict_mode(self) -> Self {
        self.include_sequence()
            .strict_ie_order()
            .optional_ie_mode(OptionalIeMode::Strict)
    }

    /// Preset: Semantic mode comparison.
    ///
    /// Compare functional equivalence, not byte-for-byte.
    pub fn semantic_preset(self) -> Self {
        self.ignore_all_header_fields()
            .ignore_timestamps()
            .unordered_ies()
            .semantic_mode()
            .ignore_missing_ies()
    }

    /// Preset: Audit mode comparison.
    ///
    /// For compliance checking - compare all meaningful fields
    /// but allow for timing differences.
    pub fn audit_mode(self) -> Self {
        self.ignore_recovery_timestamp()
            .timestamp_tolerance_secs(5)
            .unordered_ies()
    }

    // ========================================================================
    // Execution Methods
    // ========================================================================

    /// Execute the comparison and return a result.
    ///
    /// # Errors
    ///
    /// Returns error if messages cannot be compared (e.g., parsing issues).
    ///
    /// # 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!("{}", result.summary());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn compare(self) -> Result<ComparisonResult, PfcpError> {
        compare::execute_comparison(self.left, self.right, &self.options)
    }

    /// Execute comparison and return a detailed diff.
    ///
    /// Always generates full diff regardless of options.
    ///
    /// # 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 diff = MessageComparator::new(msg1, msg2)
    ///     .diff()?;
    ///
    /// println!("{}", diff);
    /// # Ok(())
    /// # }
    /// ```
    pub fn diff(mut self) -> Result<MessageDiff, PfcpError> {
        self.options.generate_diff = true;
        let result = compare::execute_comparison(self.left, self.right, &self.options)?;
        Ok(result.into_diff())
    }

    /// Quick check: returns true if messages match according to options.
    ///
    /// More efficient than `compare()` if you only need boolean result.
    ///
    /// # 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> {
    /// if MessageComparator::new(msg1, msg2).test_mode().matches()? {
    ///     println!("Messages are equivalent");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn matches(self) -> Result<bool, PfcpError> {
        let result = compare::execute_comparison(self.left, self.right, &self.options)?;
        Ok(result.is_match())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::message::heartbeat_request::HeartbeatRequestBuilder;
    use std::time::SystemTime;

    #[test]
    fn test_new_same_type() {
        let msg1 = HeartbeatRequestBuilder::new(100)
            .recovery_time_stamp(SystemTime::now())
            .build();
        let msg2 = HeartbeatRequestBuilder::new(200)
            .recovery_time_stamp(SystemTime::now())
            .build();

        let _comparator = MessageComparator::new(&msg1, &msg2);
        // Should not panic
    }

    // Note: Skipping panic test as we would need different message types
    // and we don't want to introduce cross-dependencies in this test module

    #[test]
    fn test_builder_chaining() {
        let msg1 = HeartbeatRequestBuilder::new(100)
            .recovery_time_stamp(SystemTime::now())
            .build();
        let msg2 = HeartbeatRequestBuilder::new(200)
            .recovery_time_stamp(SystemTime::now())
            .build();

        let comparator = MessageComparator::new(&msg1, &msg2)
            .ignore_sequence()
            .ignore_timestamps()
            .unordered_ies()
            .with_detailed_diff();

        assert!(comparator.options.ignore_sequence);
        assert!(comparator.options.ignore_timestamps);
        assert!(!comparator.options.strict_ie_order);
        assert!(comparator.options.generate_diff);
    }

    #[test]
    fn test_preset_test_mode() {
        let msg1 = HeartbeatRequestBuilder::new(100)
            .recovery_time_stamp(SystemTime::now())
            .build();
        let msg2 = HeartbeatRequestBuilder::new(200)
            .recovery_time_stamp(SystemTime::now())
            .build();

        let comparator = MessageComparator::new(&msg1, &msg2).test_mode();

        assert!(comparator.options.ignore_sequence);
        assert!(comparator.options.ignore_timestamps);
        assert!(comparator.options.ignore_priority);
        assert!(!comparator.options.strict_ie_order);
    }

    #[test]
    fn test_preset_strict_mode() {
        let msg1 = HeartbeatRequestBuilder::new(100)
            .recovery_time_stamp(SystemTime::now())
            .build();
        let msg2 = HeartbeatRequestBuilder::new(200)
            .recovery_time_stamp(SystemTime::now())
            .build();

        let comparator = MessageComparator::new(&msg1, &msg2).strict_mode();

        assert!(!comparator.options.ignore_sequence);
        assert!(comparator.options.strict_ie_order);
        assert_eq!(comparator.options.optional_ie_mode, OptionalIeMode::Strict);
    }
}