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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
//! PFCP Error Handling
//!
//! This module provides the `PfcpError` enum for structured error handling across the rs-pfcp
//! library, along with error message templates for consistent error reporting.
//!
//! ## PfcpError (v0.2.5+)
//!
//! The `PfcpError` enum provides structured error handling with 8 variants:
//! - `MissingMandatoryIe` - Required IE not present
//! - `InvalidLength` - Payload too short or incorrect size
//! - `InvalidValue` - Invalid field value
//! - `ValidationError` - Builder validation failure
//! - `ZeroLengthNotAllowed` - Zero-length IE rejected per 3GPP TS 29.244
//! - `IeParseError` - IE-specific parsing error
//! - `EncodingError` - UTF-8 or other encoding error
//! - `MessageParseError` - Message-level parsing error
//! - `IoError` - Underlying I/O error wrapper
//!
//! All unmarshal methods in the library return `Result<T, PfcpError>`.
//!
//! ## Usage
//!
//! ```rust
//! use rs_pfcp::error::PfcpError;
//! use rs_pfcp::ie::IeType;
//!
//! # fn example() -> Result<(), PfcpError> {
//! // Pattern match on specific error variants
//! let result: Result<(), PfcpError> = Err(PfcpError::missing_ie(IeType::PdrId));
//!
//! match result {
//!     Ok(_) => println!("Success"),
//!     Err(PfcpError::MissingMandatoryIe { ie_type, .. }) => {
//!         println!("Missing required IE: {:?}", ie_type);
//!     }
//!     Err(e) => println!("Other error: {}", e),
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## 3GPP Cause Code Mapping
//!
//! Use `err.to_cause_code()` to map errors to 3GPP TS 29.244 Cause values for responses.
//!
//! ## Design Documentation
//!
//! See `docs/analysis/completed/custom-error-type.md` for the full design and implementation history.

/// Error message templates for consistent error reporting
pub mod messages {
    // ========================================================================
    // Missing IE Errors
    // ========================================================================

    /// Format: "Missing mandatory {ie_name} IE"
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::missing_mandatory_ie_short("PDR ID");
    /// assert_eq!(error_msg, "Missing mandatory PDR ID IE");
    /// ```
    pub fn missing_mandatory_ie_short(ie_name: &str) -> String {
        format!("Missing mandatory {} IE", ie_name)
    }

    /// Format: "Missing {ie_name} IE"
    ///
    /// Used for both mandatory and conditional IEs where context makes it clear.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::missing_ie("Node ID");
    /// assert_eq!(error_msg, "Missing Node ID IE");
    /// ```
    pub fn missing_ie(ie_name: &str) -> String {
        format!("Missing {} IE", ie_name)
    }

    /// Format: "{ie_name} IE not found"
    ///
    /// Alternative phrasing for IE lookup failures.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::ie_not_found("F-SEID");
    /// assert_eq!(error_msg, "F-SEID IE not found");
    /// ```
    pub fn ie_not_found(ie_name: &str) -> String {
        format!("{} IE not found", ie_name)
    }

    /// Format: "{ie_name} is required"
    ///
    /// Used in builder validation and field checks.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::ie_required("Cause");
    /// assert_eq!(error_msg, "Cause is required");
    /// ```
    pub fn ie_required(ie_name: &str) -> String {
        format!("{} is required", ie_name)
    }

    /// Format: "{ie_name} IE is mandatory"
    ///
    /// Explicit mandatory IE error for 3GPP compliance messages.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::ie_is_mandatory("Cause");
    /// assert_eq!(error_msg, "Cause IE is mandatory");
    /// ```
    pub fn ie_is_mandatory(ie_name: &str) -> String {
        format!("{} IE is mandatory", ie_name)
    }

    // ========================================================================
    // Length Errors
    // ========================================================================

    /// Format: "{ie_name} requires at least {min_bytes} byte(s)"
    ///
    /// Used when IE payload is too short.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::requires_at_least_bytes("PDR ID", 2);
    /// assert_eq!(error_msg, "PDR ID requires at least 2 bytes");
    /// ```
    pub fn requires_at_least_bytes(ie_name: &str, min_bytes: usize) -> String {
        let byte_word = if min_bytes == 1 { "byte" } else { "bytes" };
        format!("{} requires at least {} {}", ie_name, min_bytes, byte_word)
    }

    /// Format: "{ie_name} payload too short"
    ///
    /// Concise version for payload length errors.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::payload_too_short("Reporting Triggers");
    /// assert_eq!(error_msg, "Reporting Triggers payload too short");
    /// ```
    pub fn payload_too_short(ie_name: &str) -> String {
        format!("{} payload too short", ie_name)
    }

    /// Format: "{ie_name} payload too short: expected at least {min_bytes} byte(s)"
    ///
    /// Detailed version with expected length.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::payload_too_short_expected("Report Type", 1);
    /// assert_eq!(error_msg, "Report Type payload too short: expected at least 1 byte");
    /// ```
    pub fn payload_too_short_expected(ie_name: &str, min_bytes: usize) -> String {
        let byte_word = if min_bytes == 1 { "byte" } else { "bytes" };
        format!(
            "{} payload too short: expected at least {} {}",
            ie_name, min_bytes, byte_word
        )
    }

    /// Format: "{context} too short"
    ///
    /// Generic "too short" error for headers, payloads, or buffers.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::too_short("Header");
    /// assert_eq!(error_msg, "Header too short");
    /// ```
    pub fn too_short(context: &str) -> String {
        format!("{} too short", context)
    }

    /// Format: "Invalid {ie_name} length: expected at least {expected} bytes, got {actual}"
    ///
    /// Precise length mismatch with both expected and actual values.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::invalid_length("F-TEID", 9, 5);
    /// assert_eq!(error_msg, "Invalid F-TEID length: expected at least 9 bytes, got 5");
    /// ```
    pub fn invalid_length(ie_name: &str, expected: usize, actual: usize) -> String {
        format!(
            "Invalid {} length: expected at least {} bytes, got {}",
            ie_name, expected, actual
        )
    }

    // ========================================================================
    // Invalid Value Errors
    // ========================================================================

    /// Format: "Invalid {field_name} value"
    ///
    /// Generic invalid value error.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::invalid_value("DSCP");
    /// assert_eq!(error_msg, "Invalid DSCP value");
    /// ```
    pub fn invalid_value(field_name: &str) -> String {
        format!("Invalid {} value", field_name)
    }

    /// Format: "Invalid {field_name} value: {reason}"
    ///
    /// Invalid value with explanation.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::invalid_value_reason("gate status", "must be 0-3");
    /// assert_eq!(error_msg, "Invalid gate status value: must be 0-3");
    /// ```
    pub fn invalid_value_reason(field_name: &str, reason: &str) -> String {
        format!("Invalid {} value: {}", field_name, reason)
    }

    // ========================================================================
    // Builder Errors
    // ========================================================================

    /// Format: "{field_name} is required"
    ///
    /// Builder validation: missing required field.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::builder_field_required("pdr_id");
    /// assert_eq!(error_msg, "pdr_id is required");
    /// ```
    pub fn builder_field_required(field_name: &str) -> String {
        format!("{} is required", field_name)
    }

    /// Format: "Builder {builder_type} is missing required field '{field_name}'"
    ///
    /// Detailed builder error with context.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::builder_missing_field("CreatePdrBuilder", "pdr_id");
    /// assert_eq!(
    ///     error_msg,
    ///     "Builder CreatePdrBuilder is missing required field 'pdr_id'"
    /// );
    /// ```
    pub fn builder_missing_field(builder_type: &str, field_name: &str) -> String {
        format!(
            "Builder {} is missing required field '{}'",
            builder_type, field_name
        )
    }

    // ========================================================================
    // Security / Validation Errors
    // ========================================================================

    /// Format: "Zero-length IE not allowed for {ie_name} (IE type: {ie_type}) per 3GPP TS 29.244 R18"
    ///
    /// Security validation: zero-length IE protection.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::zero_length_ie_not_allowed("F-TEID", 21);
    /// assert_eq!(
    ///     error_msg,
    ///     "Zero-length IE not allowed for F-TEID (IE type: 21) per 3GPP TS 29.244 R18"
    /// );
    /// ```
    pub fn zero_length_ie_not_allowed(ie_name: &str, ie_type: u16) -> String {
        format!(
            "Zero-length IE not allowed for {} (IE type: {}) per 3GPP TS 29.244 R18",
            ie_name, ie_type
        )
    }

    // ========================================================================
    // UTF-8 Encoding Errors
    // ========================================================================

    /// Format: "Invalid UTF-8 in {ie_name}"
    ///
    /// UTF-8 decoding failure in IE payload.
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::messages;
    ///
    /// let error_msg = messages::invalid_utf8("Application ID");
    /// assert_eq!(error_msg, "Invalid UTF-8 in Application ID");
    /// ```
    pub fn invalid_utf8(ie_name: &str) -> String {
        format!("Invalid UTF-8 in {}", ie_name)
    }
}

// ============================================================================
// PfcpError - Custom Error Type (v0.3.0+)
// ============================================================================

use std::fmt;
use std::io;

/// PFCP protocol error type
///
/// This enum represents all error conditions that can occur when parsing,
/// validating, or constructing PFCP messages and Information Elements.
///
/// # Error Categories
///
/// - **Parsing Errors**: IE/message parsing failures, malformed data
/// - **Validation Errors**: Missing mandatory fields, invalid values
/// - **Encoding Errors**: UTF-8 conversion failures
/// - **I/O Errors**: Underlying transport errors
///
/// # Examples
///
/// ```rust
/// use rs_pfcp::error::PfcpError;
/// use rs_pfcp::ie::IeType;
///
/// // Pattern matching on error type
/// # fn example(result: Result<(), PfcpError>) {
/// match result {
///     Err(PfcpError::MissingMandatoryIe { ie_type, .. }) => {
///         println!("Missing required IE: {:?}", ie_type);
///     }
///     Err(PfcpError::InvalidLength { ie_name, expected, actual, .. }) => {
///         println!("{} length mismatch: expected {}, got {}", ie_name, expected, actual);
///     }
///     Err(e) => println!("Other error: {}", e),
///     Ok(_) => println!("Success"),
/// }
/// # }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum PfcpError {
    /// Missing mandatory Information Element
    ///
    /// This error occurs when a required IE is not present in a message or grouped IE.
    ///
    /// # Fields
    /// - `ie_type`: The missing IE type
    /// - `message_type`: Optional message type context (if missing from message)
    /// - `parent_ie`: Optional parent IE context (if missing from grouped IE)
    MissingMandatoryIe {
        ie_type: crate::ie::IeType,
        message_type: Option<crate::message::MsgType>,
        parent_ie: Option<crate::ie::IeType>,
    },

    /// Information Element parsing error
    ///
    /// This error occurs when an IE's payload cannot be parsed due to invalid format,
    /// insufficient data, or protocol violations.
    ///
    /// # Fields
    /// - `ie_type`: The IE type that failed to parse
    /// - `reason`: Human-readable explanation of the failure
    /// - `offset`: Optional byte offset where the error occurred
    IeParseError {
        ie_type: crate::ie::IeType,
        reason: String,
        offset: Option<usize>,
    },

    /// Invalid IE payload length
    ///
    /// This error occurs when an IE's payload is too short or doesn't match
    /// the expected length per 3GPP TS 29.244.
    ///
    /// # Fields
    /// - `ie_name`: Human-readable IE name
    /// - `ie_type`: The IE type
    /// - `expected`: Minimum expected length in bytes
    /// - `actual`: Actual length received
    InvalidLength {
        ie_name: String,
        ie_type: crate::ie::IeType,
        expected: usize,
        actual: usize,
    },

    /// Invalid field value
    ///
    /// This error occurs when a field contains an invalid or out-of-range value.
    ///
    /// # Fields
    /// - `field`: Field name
    /// - `value`: The invalid value (as string)
    /// - `reason`: Explanation of why it's invalid
    InvalidValue {
        field: String,
        value: String,
        reason: String,
    },

    /// Builder validation error
    ///
    /// This error occurs when a builder's `.build()` method is called but
    /// required fields are missing or validation fails.
    ///
    /// # Fields
    /// - `builder`: Builder type name
    /// - `field`: Missing or invalid field name
    /// - `reason`: Validation failure reason
    ValidationError {
        builder: String,
        field: String,
        reason: String,
    },

    /// UTF-8 encoding error
    ///
    /// This error occurs when IE payload contains invalid UTF-8 data.
    ///
    /// # Fields
    /// - `ie_name`: Human-readable IE name
    /// - `ie_type`: The IE type
    /// - `source`: The underlying UTF-8 error
    EncodingError {
        ie_name: String,
        ie_type: crate::ie::IeType,
        source: std::str::Utf8Error,
    },

    /// Zero-length IE security violation
    ///
    /// This error occurs when an IE that must not be zero-length has a zero-length payload,
    /// which could indicate a DoS attempt per 3GPP TS 29.244 security considerations.
    ///
    /// # Fields
    /// - `ie_name`: Human-readable IE name
    /// - `ie_type`: The IE type (as u16)
    ZeroLengthNotAllowed { ie_name: String, ie_type: u16 },

    /// Message parsing error
    ///
    /// This error occurs when a PFCP message cannot be parsed from the wire format.
    ///
    /// # Fields
    /// - `message_type`: Optional message type if header was parsed
    /// - `reason`: Human-readable explanation
    MessageParseError {
        message_type: Option<crate::message::MsgType>,
        reason: String,
    },

    /// Underlying I/O error
    ///
    /// This error wraps transport-level I/O errors from the standard library.
    /// Note: We store the error kind and message rather than the actual io::Error
    /// to allow PfcpError to implement Clone and PartialEq.
    IoError {
        kind: io::ErrorKind,
        message: String,
    },
}

impl fmt::Display for PfcpError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PfcpError::MissingMandatoryIe {
                ie_type,
                message_type,
                parent_ie,
            } => {
                let ie_name = format!("{:?}", ie_type);
                if let Some(msg_type) = message_type {
                    write!(
                        f,
                        "{} in {:?}",
                        messages::missing_mandatory_ie_short(&ie_name),
                        msg_type
                    )
                } else if let Some(parent) = parent_ie {
                    write!(
                        f,
                        "{} in grouped IE {:?}",
                        messages::missing_mandatory_ie_short(&ie_name),
                        parent
                    )
                } else {
                    write!(f, "{}", messages::missing_mandatory_ie_short(&ie_name))
                }
            }

            PfcpError::IeParseError {
                ie_type,
                reason,
                offset,
            } => {
                if let Some(off) = offset {
                    write!(
                        f,
                        "Failed to parse {:?} at offset {}: {}",
                        ie_type, off, reason
                    )
                } else {
                    write!(f, "Failed to parse {:?}: {}", ie_type, reason)
                }
            }

            PfcpError::InvalidLength {
                ie_name,
                expected,
                actual,
                ..
            } => {
                write!(
                    f,
                    "{}",
                    messages::invalid_length(ie_name, *expected, *actual)
                )
            }

            PfcpError::InvalidValue {
                field,
                value,
                reason,
            } => {
                write!(f, "Invalid {} value '{}': {}", field, value, reason)
            }

            PfcpError::ValidationError {
                builder,
                field,
                reason,
            } => {
                write!(
                    f,
                    "Validation failed for {}: field '{}' - {}",
                    builder, field, reason
                )
            }

            PfcpError::EncodingError { ie_name, .. } => {
                write!(f, "{}", messages::invalid_utf8(ie_name))
            }

            PfcpError::ZeroLengthNotAllowed { ie_name, ie_type } => {
                write!(
                    f,
                    "{}",
                    messages::zero_length_ie_not_allowed(ie_name, *ie_type)
                )
            }

            PfcpError::MessageParseError {
                message_type,
                reason,
            } => {
                if let Some(msg_type) = message_type {
                    write!(f, "Failed to parse {:?}: {}", msg_type, reason)
                } else {
                    write!(f, "Failed to parse PFCP message: {}", reason)
                }
            }

            PfcpError::IoError { kind, message } => {
                write!(f, "I/O error ({:?}): {}", kind, message)
            }
        }
    }
}

impl std::error::Error for PfcpError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            PfcpError::EncodingError { source, .. } => Some(source),
            // IoError stores kind+message, not the actual error, so no source
            _ => None,
        }
    }
}

// ============================================================================
// Conversions from std::io::Error and other error types
// ============================================================================

impl From<io::Error> for PfcpError {
    fn from(err: io::Error) -> Self {
        PfcpError::IoError {
            kind: err.kind(),
            message: err.to_string(),
        }
    }
}

impl From<std::str::Utf8Error> for PfcpError {
    fn from(source: std::str::Utf8Error) -> Self {
        // Note: This is a generic conversion. Prefer using PfcpError::encoding_error()
        // which allows specifying the IE name and type for better error messages.
        PfcpError::EncodingError {
            ie_name: "Unknown IE".to_string(),
            ie_type: crate::ie::IeType::CreatePdr, // Placeholder
            source,
        }
    }
}

// ============================================================================
// Helper constructors for common error patterns
// ============================================================================

impl PfcpError {
    /// Create a missing mandatory IE error
    pub fn missing_ie(ie_type: crate::ie::IeType) -> Self {
        PfcpError::MissingMandatoryIe {
            ie_type,
            message_type: None,
            parent_ie: None,
        }
    }

    /// Create a missing mandatory IE error with message context
    pub fn missing_ie_in_message(
        ie_type: crate::ie::IeType,
        message_type: crate::message::MsgType,
    ) -> Self {
        PfcpError::MissingMandatoryIe {
            ie_type,
            message_type: Some(message_type),
            parent_ie: None,
        }
    }

    /// Create a missing mandatory IE error with parent IE context
    pub fn missing_ie_in_grouped(ie_type: crate::ie::IeType, parent_ie: crate::ie::IeType) -> Self {
        PfcpError::MissingMandatoryIe {
            ie_type,
            message_type: None,
            parent_ie: Some(parent_ie),
        }
    }

    /// Create an IE parse error
    pub fn parse_error(ie_type: crate::ie::IeType, reason: impl Into<String>) -> Self {
        PfcpError::IeParseError {
            ie_type,
            reason: reason.into(),
            offset: None,
        }
    }

    /// Create an IE parse error with offset
    pub fn parse_error_at(
        ie_type: crate::ie::IeType,
        reason: impl Into<String>,
        offset: usize,
    ) -> Self {
        PfcpError::IeParseError {
            ie_type,
            reason: reason.into(),
            offset: Some(offset),
        }
    }

    /// Create an invalid length error
    pub fn invalid_length(
        ie_name: impl Into<String>,
        ie_type: crate::ie::IeType,
        expected: usize,
        actual: usize,
    ) -> Self {
        PfcpError::InvalidLength {
            ie_name: ie_name.into(),
            ie_type,
            expected,
            actual,
        }
    }

    /// Create an invalid value error
    pub fn invalid_value(
        field: impl Into<String>,
        value: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        PfcpError::InvalidValue {
            field: field.into(),
            value: value.into(),
            reason: reason.into(),
        }
    }

    /// Create a builder validation error
    pub fn validation_error(
        builder: impl Into<String>,
        field: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        PfcpError::ValidationError {
            builder: builder.into(),
            field: field.into(),
            reason: reason.into(),
        }
    }

    /// Create a zero-length IE security error
    pub fn zero_length_not_allowed(ie_name: impl Into<String>, ie_type: u16) -> Self {
        PfcpError::ZeroLengthNotAllowed {
            ie_name: ie_name.into(),
            ie_type,
        }
    }

    /// Create a message parse error
    pub fn message_parse_error(reason: impl Into<String>) -> Self {
        PfcpError::MessageParseError {
            message_type: None,
            reason: reason.into(),
        }
    }

    /// Create a UTF-8 encoding error with context
    pub fn encoding_error(
        ie_name: impl Into<String>,
        ie_type: crate::ie::IeType,
        source: std::str::Utf8Error,
    ) -> Self {
        PfcpError::EncodingError {
            ie_name: ie_name.into(),
            ie_type,
            source,
        }
    }

    // ========================================================================
    // 3GPP Cause Code Mapping
    // ========================================================================

    /// Convert PfcpError to appropriate 3GPP PFCP Cause code for protocol responses.
    ///
    /// This mapping allows PFCP implementations to return proper Cause IEs in response
    /// messages based on the error that occurred during processing.
    ///
    /// # Mapping Rules
    ///
    /// Per 3GPP TS 29.244 Section 8.2.1 and Table 8.2.1-1:
    /// - Missing mandatory IEs → Cause 66 (Mandatory IE Missing)
    /// - Invalid IE length → Cause 68 (Invalid Length)
    /// - IE parsing errors → Cause 69 (Mandatory IE Incorrect)
    /// - Validation errors → Cause 73 (Rule Creation/Modification Failure)
    /// - System errors → Cause 77 (System Failure)
    /// - Message parsing errors → Cause 64 (Request Rejected)
    ///
    /// # Examples
    ///
    /// ```
    /// use rs_pfcp::error::PfcpError;
    /// use rs_pfcp::ie::{IeType, cause::CauseValue};
    ///
    /// let error = PfcpError::missing_ie(IeType::PdrId);
    /// assert_eq!(error.to_cause_code(), CauseValue::MandatoryIeMissing);
    ///
    /// let error = PfcpError::invalid_length("F-TEID", IeType::Fteid, 9, 5);
    /// assert_eq!(error.to_cause_code(), CauseValue::InvalidLength);
    /// ```
    pub fn to_cause_code(&self) -> crate::ie::cause::CauseValue {
        use crate::ie::cause::CauseValue;

        match self {
            // Missing mandatory IE → Cause 66 (Mandatory IE Missing)
            PfcpError::MissingMandatoryIe { .. } => CauseValue::MandatoryIeMissing,

            // IE parsing errors → Cause 69 (Mandatory IE Incorrect)
            // This indicates the IE was present but could not be parsed correctly
            PfcpError::IeParseError { .. } => CauseValue::MandatoryIeIncorrect,

            // Invalid length → Cause 68 (Invalid Length)
            PfcpError::InvalidLength { .. } => CauseValue::InvalidLength,

            // Invalid value → Cause 69 (Mandatory IE Incorrect)
            // The IE value is outside acceptable range or format
            PfcpError::InvalidValue { .. } => CauseValue::MandatoryIeIncorrect,

            // Builder validation errors → Cause 73 (Rule Creation/Modification Failure)
            // These occur when constructing rules (PDR, FAR, QER, URR)
            PfcpError::ValidationError { .. } => CauseValue::RuleCreationModificationFailure,

            // UTF-8 encoding errors → Cause 69 (Mandatory IE Incorrect)
            // String IEs contain invalid UTF-8 data
            PfcpError::EncodingError { .. } => CauseValue::MandatoryIeIncorrect,

            // Zero-length IE security violation → Cause 68 (Invalid Length)
            // Zero-length IEs where not permitted per 3GPP security considerations
            PfcpError::ZeroLengthNotAllowed { .. } => CauseValue::InvalidLength,

            // Message parsing errors → Cause 64 (Request Rejected)
            // Unable to parse message structure itself
            PfcpError::MessageParseError { .. } => CauseValue::RequestRejected,

            // I/O errors → Cause 77 (System Failure)
            // Underlying transport or system issues
            PfcpError::IoError { .. } => CauseValue::SystemFailure,
        }
    }
}

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

    #[test]
    fn test_missing_ie_messages() {
        assert_eq!(
            messages::missing_mandatory_ie_short("PDR ID"),
            "Missing mandatory PDR ID IE"
        );
        assert_eq!(messages::missing_ie("Node ID"), "Missing Node ID IE");
        assert_eq!(messages::ie_not_found("F-SEID"), "F-SEID IE not found");
        assert_eq!(messages::ie_required("Cause"), "Cause is required");
        assert_eq!(
            messages::ie_is_mandatory("Node ID"),
            "Node ID IE is mandatory"
        );
    }

    #[test]
    fn test_length_error_messages() {
        assert_eq!(
            messages::requires_at_least_bytes("PDR ID", 2),
            "PDR ID requires at least 2 bytes"
        );
        assert_eq!(
            messages::requires_at_least_bytes("Cause", 1),
            "Cause requires at least 1 byte"
        );
        assert_eq!(
            messages::payload_too_short("Reporting Triggers"),
            "Reporting Triggers payload too short"
        );
        assert_eq!(
            messages::payload_too_short_expected("Report Type", 1),
            "Report Type payload too short: expected at least 1 byte"
        );
        assert_eq!(messages::too_short("Header"), "Header too short");
        assert_eq!(
            messages::invalid_length("F-TEID", 9, 5),
            "Invalid F-TEID length: expected at least 9 bytes, got 5"
        );
    }

    #[test]
    fn test_invalid_value_messages() {
        assert_eq!(messages::invalid_value("DSCP"), "Invalid DSCP value");
        assert_eq!(
            messages::invalid_value_reason("gate status", "must be 0-3"),
            "Invalid gate status value: must be 0-3"
        );
    }

    #[test]
    fn test_builder_error_messages() {
        assert_eq!(
            messages::builder_field_required("pdr_id"),
            "pdr_id is required"
        );
        assert_eq!(
            messages::builder_missing_field("CreatePdrBuilder", "pdr_id"),
            "Builder CreatePdrBuilder is missing required field 'pdr_id'"
        );
    }

    #[test]
    fn test_security_error_messages() {
        assert_eq!(
            messages::zero_length_ie_not_allowed("F-TEID", 21),
            "Zero-length IE not allowed for F-TEID (IE type: 21) per 3GPP TS 29.244 R18"
        );
    }

    #[test]
    fn test_utf8_error_messages() {
        assert_eq!(
            messages::invalid_utf8("Application ID"),
            "Invalid UTF-8 in Application ID"
        );
    }

    #[test]
    fn test_byte_pluralization() {
        // Test singular "byte"
        assert_eq!(
            messages::requires_at_least_bytes("Test", 1),
            "Test requires at least 1 byte"
        );
        assert_eq!(
            messages::payload_too_short_expected("Test", 1),
            "Test payload too short: expected at least 1 byte"
        );

        // Test plural "bytes"
        assert_eq!(
            messages::requires_at_least_bytes("Test", 2),
            "Test requires at least 2 bytes"
        );
        assert_eq!(
            messages::payload_too_short_expected("Test", 10),
            "Test payload too short: expected at least 10 bytes"
        );
    }

    // ========================================================================
    // PfcpError Tests (v0.3.0+)
    // ========================================================================

    use super::PfcpError;
    use std::error::Error;
    use std::io;

    #[test]
    fn test_pfcp_error_missing_mandatory_ie() {
        let err = PfcpError::missing_ie(crate::ie::IeType::PdrId);
        assert!(matches!(
            err,
            PfcpError::MissingMandatoryIe {
                ie_type: crate::ie::IeType::PdrId,
                message_type: None,
                parent_ie: None,
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Missing mandatory"));
        assert!(display.contains("PdrId"));
    }

    #[test]
    fn test_pfcp_error_missing_ie_in_message() {
        let err = PfcpError::missing_ie_in_message(
            crate::ie::IeType::NodeId,
            crate::message::MsgType::HeartbeatRequest,
        );
        assert!(matches!(
            err,
            PfcpError::MissingMandatoryIe {
                ie_type: crate::ie::IeType::NodeId,
                message_type: Some(crate::message::MsgType::HeartbeatRequest),
                parent_ie: None,
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Missing mandatory"));
        assert!(display.contains("NodeId"));
        assert!(display.contains("HeartbeatRequest"));
    }

    #[test]
    fn test_pfcp_error_missing_ie_in_grouped() {
        let err = PfcpError::missing_ie_in_grouped(
            crate::ie::IeType::PdrId,
            crate::ie::IeType::CreatePdr,
        );
        assert!(matches!(
            err,
            PfcpError::MissingMandatoryIe {
                ie_type: crate::ie::IeType::PdrId,
                message_type: None,
                parent_ie: Some(crate::ie::IeType::CreatePdr),
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Missing mandatory"));
        assert!(display.contains("PdrId"));
        assert!(display.contains("CreatePdr"));
    }

    #[test]
    fn test_pfcp_error_parse_error() {
        let err = PfcpError::parse_error(crate::ie::IeType::Fteid, "Invalid flags");
        assert!(matches!(
            err,
            PfcpError::IeParseError {
                ie_type: crate::ie::IeType::Fteid,
                offset: None,
                ..
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Failed to parse"));
        assert!(display.contains("Fteid"));
        assert!(display.contains("Invalid flags"));
    }

    #[test]
    fn test_pfcp_error_parse_error_at() {
        let err = PfcpError::parse_error_at(crate::ie::IeType::CreatePdr, "Bad PDI", 42);
        assert!(matches!(
            err,
            PfcpError::IeParseError {
                ie_type: crate::ie::IeType::CreatePdr,
                offset: Some(42),
                ..
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Failed to parse"));
        assert!(display.contains("CreatePdr"));
        assert!(display.contains("Bad PDI"));
        assert!(display.contains("offset 42"));
    }

    #[test]
    fn test_pfcp_error_invalid_length() {
        let err = PfcpError::invalid_length("F-TEID", crate::ie::IeType::Fteid, 9, 5);
        assert!(matches!(
            err,
            PfcpError::InvalidLength {
                ie_type: crate::ie::IeType::Fteid,
                expected: 9,
                actual: 5,
                ..
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Invalid F-TEID length"));
        assert!(display.contains("expected at least 9"));
        assert!(display.contains("got 5"));
    }

    #[test]
    fn test_pfcp_error_invalid_value() {
        let err = PfcpError::invalid_value("gate_status", "5", "must be 0-3");
        assert!(matches!(err, PfcpError::InvalidValue { .. }));
        let display = format!("{}", err);
        assert!(display.contains("Invalid gate_status value"));
        assert!(display.contains("must be 0-3"));
    }

    #[test]
    fn test_pfcp_error_validation_error() {
        let err = PfcpError::validation_error("CreatePdrBuilder", "pdr_id", "PDR ID is required");
        assert!(matches!(err, PfcpError::ValidationError { .. }));
        let display = format!("{}", err);
        assert!(display.contains("Validation failed"));
        assert!(display.contains("CreatePdrBuilder"));
        assert!(display.contains("pdr_id"));
        assert!(display.contains("PDR ID is required"));
    }

    #[test]
    fn test_pfcp_error_encoding_error() {
        // Create a UTF-8 error by trying to parse invalid UTF-8
        let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
        let utf8_err = std::str::from_utf8(&invalid_utf8).unwrap_err();

        let err = PfcpError::encoding_error(
            "Network Instance",
            crate::ie::IeType::NetworkInstance,
            utf8_err,
        );
        assert!(matches!(err, PfcpError::EncodingError { .. }));
        let display = format!("{}", err);
        assert!(display.contains("Invalid UTF-8"));
        assert!(display.contains("Network Instance"));
    }

    #[test]
    fn test_pfcp_error_zero_length_not_allowed() {
        let err = PfcpError::zero_length_not_allowed("F-TEID", 21);
        assert!(matches!(
            err,
            PfcpError::ZeroLengthNotAllowed { ie_type: 21, .. }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Zero-length IE not allowed"));
        assert!(display.contains("F-TEID"));
        assert!(display.contains("21"));
    }

    #[test]
    fn test_pfcp_error_message_parse_error() {
        let err = PfcpError::message_parse_error("Unexpected message type");
        assert!(matches!(
            err,
            PfcpError::MessageParseError {
                message_type: None,
                ..
            }
        ));
        let display = format!("{}", err);
        assert!(display.contains("Failed to parse PFCP message"));
        assert!(display.contains("Unexpected message type"));
    }

    #[test]
    fn test_pfcp_error_from_io_error() {
        let io_err = io::Error::new(io::ErrorKind::UnexpectedEof, "short read");
        let pfcp_err: PfcpError = io_err.into();
        assert!(matches!(
            pfcp_err,
            PfcpError::IoError {
                kind: io::ErrorKind::UnexpectedEof,
                ..
            }
        ));
        let display = format!("{}", pfcp_err);
        assert!(display.contains("I/O error"));
        assert!(display.contains("short read"));
    }

    #[test]
    fn test_pfcp_error_from_utf8_error() {
        let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
        let utf8_err = std::str::from_utf8(&invalid_utf8).unwrap_err();
        let pfcp_err: PfcpError = utf8_err.into();
        assert!(matches!(pfcp_err, PfcpError::EncodingError { .. }));
    }

    #[test]
    fn test_pfcp_error_source() {
        // Test error with source (EncodingError)
        let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
        let utf8_err = std::str::from_utf8(&invalid_utf8).unwrap_err();
        let pfcp_err =
            PfcpError::encoding_error("Test IE", crate::ie::IeType::NetworkInstance, utf8_err);
        assert!(pfcp_err.source().is_some());

        // Test error without source (IoError - stores kind+message, not original error)
        let io_err = io::Error::new(io::ErrorKind::UnexpectedEof, "test");
        let pfcp_err: PfcpError = io_err.into();
        assert!(pfcp_err.source().is_none());

        // Test error without source (MissingMandatoryIe)
        let pfcp_err = PfcpError::missing_ie(crate::ie::IeType::PdrId);
        assert!(pfcp_err.source().is_none());

        // Test error without source (InvalidValue)
        let pfcp_err = PfcpError::invalid_value("field", "value", "reason");
        assert!(pfcp_err.source().is_none());
    }

    #[test]
    fn test_pfcp_error_clone() {
        let err1 = PfcpError::missing_ie(crate::ie::IeType::PdrId);
        let err2 = err1.clone();
        assert_eq!(err1, err2);
    }

    #[test]
    fn test_pfcp_error_debug() {
        let err = PfcpError::invalid_value("test_field", "bad_value", "test reason");
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("InvalidValue"));
        assert!(debug_str.contains("test_field"));
        assert!(debug_str.contains("bad_value"));
    }

    #[test]
    fn test_pfcp_error_display_all_variants() {
        // Test that all error variants produce non-empty display strings
        let errors = vec![
            PfcpError::missing_ie(crate::ie::IeType::PdrId),
            PfcpError::missing_ie_in_message(
                crate::ie::IeType::NodeId,
                crate::message::MsgType::HeartbeatRequest,
            ),
            PfcpError::missing_ie_in_grouped(
                crate::ie::IeType::PdrId,
                crate::ie::IeType::CreatePdr,
            ),
            PfcpError::parse_error(crate::ie::IeType::Fteid, "test error"),
            PfcpError::parse_error_at(crate::ie::IeType::CreatePdr, "test error", 10),
            PfcpError::invalid_length("Test IE", crate::ie::IeType::PdrId, 10, 5),
            PfcpError::invalid_value("field", "value", "reason"),
            PfcpError::validation_error("Builder", "field", "reason"),
            PfcpError::zero_length_not_allowed("IE", 42),
            PfcpError::message_parse_error("test error"),
            PfcpError::IoError {
                kind: io::ErrorKind::InvalidData,
                message: "test error".to_string(),
            },
        ];

        for err in errors {
            let display = format!("{}", err);
            assert!(!display.is_empty(), "Error display should not be empty");
            assert!(display.len() > 10, "Error display should be descriptive");
        }
    }

    // ========================================================================
    // 3GPP Cause Code Mapping Tests
    // ========================================================================

    use crate::ie::cause::CauseValue;

    #[test]
    fn test_to_cause_code_missing_mandatory_ie() {
        let err = PfcpError::missing_ie(crate::ie::IeType::PdrId);
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeMissing);

        let err = PfcpError::missing_ie_in_message(
            crate::ie::IeType::NodeId,
            crate::message::MsgType::HeartbeatRequest,
        );
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeMissing);

        let err = PfcpError::missing_ie_in_grouped(
            crate::ie::IeType::PdrId,
            crate::ie::IeType::CreatePdr,
        );
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeMissing);
    }

    #[test]
    fn test_to_cause_code_ie_parse_error() {
        let err = PfcpError::parse_error(crate::ie::IeType::Fteid, "Invalid flags");
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeIncorrect);

        let err = PfcpError::parse_error_at(crate::ie::IeType::CreatePdr, "Bad PDI", 42);
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeIncorrect);
    }

    #[test]
    fn test_to_cause_code_invalid_length() {
        let err = PfcpError::invalid_length("F-TEID", crate::ie::IeType::Fteid, 9, 5);
        assert_eq!(err.to_cause_code(), CauseValue::InvalidLength);

        let err = PfcpError::zero_length_not_allowed("F-TEID", 21);
        assert_eq!(err.to_cause_code(), CauseValue::InvalidLength);
    }

    #[test]
    fn test_to_cause_code_invalid_value() {
        let err = PfcpError::invalid_value("gate_status", "5", "must be 0-3");
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeIncorrect);
    }

    #[test]
    fn test_to_cause_code_validation_error() {
        let err = PfcpError::validation_error("CreatePdrBuilder", "pdr_id", "PDR ID is required");
        assert_eq!(
            err.to_cause_code(),
            CauseValue::RuleCreationModificationFailure
        );
    }

    #[test]
    fn test_to_cause_code_encoding_error() {
        let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
        let utf8_err = std::str::from_utf8(&invalid_utf8).unwrap_err();
        let err = PfcpError::encoding_error(
            "Network Instance",
            crate::ie::IeType::NetworkInstance,
            utf8_err,
        );
        assert_eq!(err.to_cause_code(), CauseValue::MandatoryIeIncorrect);
    }

    #[test]
    fn test_to_cause_code_message_parse_error() {
        let err = PfcpError::message_parse_error("Unexpected message type");
        assert_eq!(err.to_cause_code(), CauseValue::RequestRejected);
    }

    #[test]
    fn test_to_cause_code_io_error() {
        let io_err = io::Error::new(io::ErrorKind::UnexpectedEof, "short read");
        let err: PfcpError = io_err.into();
        assert_eq!(err.to_cause_code(), CauseValue::SystemFailure);
    }

    #[test]
    fn test_to_cause_code_all_variants() {
        // Test that all error variants have a cause code mapping
        let errors_and_causes = vec![
            (
                PfcpError::missing_ie(crate::ie::IeType::PdrId),
                CauseValue::MandatoryIeMissing,
            ),
            (
                PfcpError::parse_error(crate::ie::IeType::Fteid, "test"),
                CauseValue::MandatoryIeIncorrect,
            ),
            (
                PfcpError::invalid_length("Test", crate::ie::IeType::PdrId, 10, 5),
                CauseValue::InvalidLength,
            ),
            (
                PfcpError::invalid_value("field", "value", "reason"),
                CauseValue::MandatoryIeIncorrect,
            ),
            (
                PfcpError::validation_error("Builder", "field", "reason"),
                CauseValue::RuleCreationModificationFailure,
            ),
            (
                PfcpError::zero_length_not_allowed("IE", 42),
                CauseValue::InvalidLength,
            ),
            (
                PfcpError::message_parse_error("test"),
                CauseValue::RequestRejected,
            ),
            (
                PfcpError::IoError {
                    kind: io::ErrorKind::InvalidData,
                    message: "test".to_string(),
                },
                CauseValue::SystemFailure,
            ),
        ];

        for (error, expected_cause) in errors_and_causes {
            assert_eq!(
                error.to_cause_code(),
                expected_cause,
                "Error {:?} should map to cause {:?}",
                error,
                expected_cause
            );
        }
    }
}