rasn 0.28.10

A safe no_std ASN.1 codec framework.
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
//! Error types associated with decoding from ASN.1 codecs.

use core::num::ParseIntError;

use super::strings::PermittedAlphabetError;
use alloc::{boxed::Box, string::ToString};

use snafu::Snafu;
#[cfg(feature = "backtraces")]
use snafu::{Backtrace, GenerateImplicitData};

use crate::Codec;
use crate::de::Error;
use crate::types::{Tag, constraints::Bounded, variants::Variants};
use num_bigint::BigInt;

/// Variants for every codec-specific `DecodeError` kind.
#[derive(Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum CodecDecodeError {
    Ber(BerDecodeErrorKind),
    Cer(CerDecodeErrorKind),
    Der(DerDecodeErrorKind),
    Uper(UperDecodeErrorKind),
    Aper(AperDecodeErrorKind),
    Jer(JerDecodeErrorKind),
    Oer(OerDecodeErrorKind),
    Coer(CoerDecodeErrorKind),
    Xer(XerDecodeErrorKind),
}

impl core::fmt::Display for CodecDecodeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            CodecDecodeError::Ber(kind) => write!(f, "BER decoding error: {kind}"),
            CodecDecodeError::Cer(kind) => write!(f, "CER decoding error: {kind}"),
            CodecDecodeError::Der(kind) => write!(f, "DER decoding error: {kind}"),
            CodecDecodeError::Uper(kind) => write!(f, "UPER decoding error: {kind}"),
            CodecDecodeError::Aper(kind) => write!(f, "APER decoding error: {kind}"),
            CodecDecodeError::Jer(kind) => write!(f, "JER decoding error: {kind}"),
            CodecDecodeError::Oer(kind) => write!(f, "OER decoding error: {kind}"),
            CodecDecodeError::Coer(kind) => write!(f, "COER decoding error: {kind}"),
            CodecDecodeError::Xer(kind) => write!(f, "XER decoding error: {kind}"),
        }
    }
}

macro_rules! impl_from {
    ($variant:ident, $error_kind:ty) => {
        impl From<$error_kind> for DecodeError {
            fn from(error: $error_kind) -> Self {
                Self::from_codec_kind(CodecDecodeError::$variant(error))
            }
        }
    };
}

// implement From for each variant of CodecDecodeError into DecodeError
impl_from!(Ber, BerDecodeErrorKind);
impl_from!(Cer, CerDecodeErrorKind);
impl_from!(Der, DerDecodeErrorKind);
impl_from!(Uper, UperDecodeErrorKind);
impl_from!(Aper, AperDecodeErrorKind);
impl_from!(Jer, JerDecodeErrorKind);
impl_from!(Oer, OerDecodeErrorKind);
impl_from!(Coer, CoerDecodeErrorKind);
impl_from!(Xer, XerDecodeErrorKind);

impl From<CodecDecodeError> for DecodeError {
    fn from(error: CodecDecodeError) -> Self {
        Self::from_codec_kind(error)
    }
}

/// An error type for failed decoding for every decoder.
/// Abstracts over the different generic and codec-specific errors.
///
/// `kind` field is used to determine the kind of error that occurred.
/// `codec` field is used to determine the codec that failed.
/// `backtrace` field is used to determine the backtrace of the error.
///
/// There is `Kind::CodecSpecific` variant which wraps the codec-specific
/// errors as `CodecEncodeError` type.
///
/// # Example
/// ```rust
/// use nom::Needed;
/// use rasn::{Codec, error::DecodeErrorKind, prelude::*};
///
/// #[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq)]
/// #[rasn(delegate)]
/// struct MyString(pub VisibleString);
///
/// // Hello, World! in decimal bytes with trailing zeros
/// // Below sample requires that `backtraces` feature is enabled
/// let hello_data = vec![
///     13, 145, 151, 102, 205, 235, 16, 119, 223, 203, 102, 68, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
///     0,
/// ];
/// // Initially parse the first 2 bytes for Error demonstration purposes
/// let mut total = 2;
///
/// loop {
///     let decoded = Codec::Uper.decode_from_binary::<MyString>(&hello_data[0..hello_data.len().min(total)]);
///     match decoded {
///         Ok(succ) => {
///             println!("Successful decoding!");
///             println!("Decoded string: {}", succ.0);
///             break;
///         }
///         Err(e) => {
///             // e is DecodeError, kind is boxed
///             match *e.kind {
///                 DecodeErrorKind::Incomplete { needed } => {
///                     println!("Codec error source: {}", e.codec);
///                     println!("Error kind: {}", e.kind);
///                     // Here you need to know, that VisibleString has width of 7 bits and UPER parses input
///                     // as bits, if you want to build logic around it, and feed exactly the correct amount of data.
///                     // Usually you might need to just provide one byte at time instead when something is missing, since
///                     // inner logic might not be known to you, and data structures can get complex.
///                     total += match needed {
///                         Needed::Size(n) => {
///                             let missing_bytes = n.get() / 7;
///                             missing_bytes
///
///                         }
///                         _ => {
///                             #[cfg(feature = "backtraces")]
///                             println!("Backtrace:\n{:?}", e.backtrace);
///                             panic!("Unexpected error! {e:?}");
///                         }
///                     }
///                 }
///                 k => {
///                     #[cfg(feature = "backtraces")]
///                     println!("Backtrace:\n{:?}", e.backtrace);
///                     panic!("Unexpected error! {k:?}");
///                 }
///             }
///         }
///     }
/// }
///```
/// The previous will produce something like following:
/// ```text
/// Codec error: UPER
/// Error kind: Need more BITS to continue: (Size(83)).
/// Successful decoding!
/// Decoded string: Hello, world!
/// ```
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct DecodeError {
    /// The kind of decoding error received.
    pub kind: Box<DecodeErrorKind>,
    /// The codec that returned the error.
    pub codec: Codec,
    /// The backtrace associated with the error.
    #[cfg(feature = "backtraces")]
    pub backtrace: Backtrace,
}

impl core::fmt::Display for DecodeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{} (Codec: {})", self.kind, self.codec)?;
        #[cfg(feature = "backtraces")]
        write!(f, "\n\nBacktrace:\n{}", self.backtrace)?;
        Ok(())
    }
}

impl DecodeError {
    /// Creates a wrapper around  EOF error from a given codec.
    fn eof(codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::Eof, codec)
    }

    /// Creates a wrapper around a permitted alphabet error from a given codec.
    #[must_use]
    pub fn permitted_alphabet_error(reason: PermittedAlphabetError, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::PermittedAlphabetError { reason }, codec)
    }

    /// Creates a wrapper around a size error from a given codec.
    #[must_use]
    pub fn size_constraint_not_satisfied(
        size: Option<usize>,
        expected: alloc::string::String,
        codec: Codec,
    ) -> Self {
        Self::from_kind(
            DecodeErrorKind::SizeConstraintNotSatisfied { size, expected },
            codec,
        )
    }

    /// Creates a wrapper around a value error from a given codec.
    #[must_use]
    pub fn value_constraint_not_satisfied(
        value: BigInt,
        expected: Bounded<i128>,
        codec: Codec,
    ) -> Self {
        Self::from_kind(
            DecodeErrorKind::ValueConstraintNotSatisfied { value, expected },
            codec,
        )
    }
    /// Creates a wrapper around a inner subtype constraint error from a given codec.
    /// This is mainly used by standards.
    #[must_use]
    pub fn inner_subtype_constraint_not_satisfied(
        reason: super::InnerSubtypeConstraintError,
        codec: Codec,
    ) -> Self {
        Self::from_kind(
            DecodeErrorKind::InnerSubtypeConstraintNotSatisfied { reason },
            codec,
        )
    }

    /// Creates a wrapper around a discriminant value error from a given codec.
    #[must_use]
    pub fn discriminant_value_not_found(discriminant: isize, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::DiscriminantValueNotFound { discriminant },
            codec,
        )
    }

    /// Creates a wrapper around a range value error from a given codec.
    #[must_use]
    pub fn range_exceeds_platform_width(needed: u32, present: u32, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::RangeExceedsPlatformWidth { needed, present },
            codec,
        )
    }

    /// Creates a wrapper around a fixed string conversion error from a given codec.
    #[must_use]
    pub fn fixed_string_conversion_failed(
        tag: Tag,
        actual: usize,
        expected: usize,
        codec: Codec,
    ) -> Self {
        Self::from_kind(
            DecodeErrorKind::FixedStringConversionFailed {
                tag,
                actual,
                expected,
            },
            codec,
        )
    }

    /// Creates a wrapper around a sequence item error from a given codec.
    #[must_use]
    pub fn incorrect_item_number_in_sequence(expected: usize, actual: usize, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::IncorrectItemNumberInSequence { expected, actual },
            codec,
        )
    }

    /// Creates a wrapper around a integer overflow error from a given codec.
    #[must_use]
    pub fn integer_overflow(max_width: u32, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::IntegerOverflow { max_width }, codec)
    }

    /// Creates a wrapper around a integer conversion error from a given codec.
    #[must_use]
    pub fn integer_type_conversion_failed(msg: alloc::string::String, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::IntegerTypeConversionFailed { msg }, codec)
    }

    /// Creates a wrapper around a invalid bit string error from a given codec.
    #[must_use]
    pub fn invalid_bit_string(bits: u8, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::InvalidBitString { bits }, codec)
    }

    /// Creates a wrapper around a missing tag error from a given codec.
    #[must_use]
    pub fn missing_tag_class_or_value_in_sequence_or_set(
        class: crate::types::Class,
        value: u32,
        codec: Codec,
    ) -> Self {
        Self::from_kind(
            DecodeErrorKind::MissingTagClassOrValueInSequenceOrSet { class, value },
            codec,
        )
    }

    /// Creates a wrapper around a unexpected non-extensible type error from a given codec.
    #[must_use]
    pub fn type_not_extensible(codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::TypeNotExtensible, codec)
    }

    /// Creates a wrapper around a parser error from a given codec.
    #[must_use]
    pub fn parser_fail(msg: alloc::string::String, codec: Codec) -> Self {
        DecodeError::from_kind(DecodeErrorKind::Parser { msg }, codec)
    }

    /// Creates a wrapper around using `REAL` with unsupported codecs.
    #[must_use]
    pub fn real_not_supported(codec: Codec) -> Self {
        DecodeError::from_kind(DecodeErrorKind::RealNotSupported, codec)
    }

    /// Creates a wrapper around a missing required extension error from a given codec.
    #[must_use]
    pub fn required_extension_not_present(tag: Tag, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::RequiredExtensionNotPresent { tag }, codec)
    }

    /// Creates a wrapper around a missing enum index error from a given codec.
    #[must_use]
    pub fn enumeration_index_not_found(index: usize, extended_list: bool, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::EnumerationIndexNotFound {
                index,
                extended_list,
            },
            codec,
        )
    }

    /// Creates a wrapper around a choice index exceeding platform width error from a given codec.
    #[must_use]
    pub fn choice_index_exceeds_platform_width(
        needed: u32,
        present: DecodeError,
        codec: Codec,
    ) -> Self {
        Self::from_kind(
            DecodeErrorKind::ChoiceIndexExceedsPlatformWidth { needed, present },
            codec,
        )
    }

    /// Creates a wrapper around a length exceeding platform width error from a given codec.
    #[must_use]
    pub fn length_exceeds_platform_width(msg: alloc::string::String, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::LengthExceedsPlatformWidth { msg }, codec)
    }

    /// Creates a wrapper around a missing choice index error from a given codec.
    #[must_use]
    pub fn choice_index_not_found(index: usize, variants: Variants, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::ChoiceIndexNotFound { index, variants },
            codec,
        )
    }

    /// Creates a wrapper around a string conversion error from a given codec.
    #[must_use]
    pub fn string_conversion_failed(tag: Tag, msg: alloc::string::String, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::StringConversionFailed { tag, msg }, codec)
    }

    /// Creates a wrapper around unexpected extra data error from a given codec.
    #[must_use]
    pub fn unexpected_extra_data(length: usize, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::UnexpectedExtraData { length }, codec)
    }

    /// Creates a wrapper around unexpected empty input error from a given codec.
    #[must_use]
    pub fn unexpected_empty_input(codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::UnexpectedEmptyInput, codec)
    }

    /// Checks whether the length matches, and returns an error if not.
    pub fn assert_length(
        expected: usize,
        actual: usize,
        codec: Codec,
    ) -> core::result::Result<(), DecodeError> {
        if expected == actual {
            Ok(())
        } else {
            Err(DecodeError::from_kind(
                DecodeErrorKind::MismatchedLength { expected, actual },
                codec,
            ))
        }
    }

    pub(crate) fn map_nom_err<T: core::fmt::Debug>(
        error: nom::Err<nom::error::Error<T>>,
        codec: Codec,
    ) -> DecodeError {
        match error {
            nom::Err::Incomplete(needed) => DecodeError::incomplete(needed, codec),
            nom::Err::Failure(e) | nom::Err::Error(e) => {
                if e.code == nom::error::ErrorKind::Eof {
                    DecodeError::eof(codec)
                } else {
                    DecodeError::parser_fail(alloc::format!("Parsing Failure: {e:?}"), codec)
                }
            }
        }
    }

    /// Creates a new error from a given decode error kind and codec.
    #[must_use]
    pub fn from_kind(kind: DecodeErrorKind, codec: Codec) -> Self {
        Self {
            kind: Box::new(kind),
            codec,
            #[cfg(feature = "backtraces")]
            backtrace: Backtrace::generate(),
        }
    }

    #[must_use]
    fn from_codec_kind(inner: CodecDecodeError) -> Self {
        let codec = match inner {
            CodecDecodeError::Ber(_) => crate::Codec::Ber,
            #[allow(unreachable_patterns)]
            CodecDecodeError::Cer(_) => crate::Codec::Cer,
            CodecDecodeError::Der(_) => crate::Codec::Der,
            #[allow(unreachable_patterns)]
            CodecDecodeError::Uper(_) => crate::Codec::Uper,
            #[allow(unreachable_patterns)]
            CodecDecodeError::Aper(_) => crate::Codec::Aper,
            CodecDecodeError::Jer(_) => crate::Codec::Jer,
            CodecDecodeError::Oer(_) => crate::Codec::Oer,
            CodecDecodeError::Coer(_) => crate::Codec::Coer,
            CodecDecodeError::Xer(_) => crate::Codec::Xer,
        };
        Self {
            kind: Box::new(DecodeErrorKind::CodecSpecific { inner }),
            codec,
            #[cfg(feature = "backtraces")]
            backtrace: Backtrace::generate(),
        }
    }

    /// Check if the root cause of this error matches the given predicate.
    #[must_use]
    pub fn matches_root_cause<F>(&self, predicate: F) -> bool
    where
        F: Fn(&DecodeErrorKind) -> bool,
    {
        let mut root = self;
        while let DecodeErrorKind::FieldError { nested, .. } = &*root.kind {
            root = &**nested;
        }

        predicate(&root.kind)
    }
}

impl core::error::Error for DecodeError {}

/// `DecodeError` kinds which are common for all codecs.
#[derive(Snafu)]
#[snafu(visibility(pub))]
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeErrorKind {
    /// Permitted alphabet constraint wasn't satisfied.
    #[snafu(display("Alphabet constraint not satisfied {}", reason))]
    PermittedAlphabetError {
        /// The reason the constraint wasn't satisfied.
        reason: PermittedAlphabetError,
    },

    /// Size constraint wasn't satisfied.
    #[snafu(display("Size constraint not satisfied: expected: {expected}; actual: {size:?}"))]
    SizeConstraintNotSatisfied {
        /// Actual sie of the data
        size: Option<usize>,
        /// Expected size by the constraint
        expected: alloc::string::String,
    },

    /// Value constraint wasn't satisfied.
    #[snafu(display("Value constraint not satisfied: expected: {expected}; actual: {value}"))]
    ValueConstraintNotSatisfied {
        /// Actual value of the data
        value: BigInt,
        /// Expected value by the constraint
        expected: Bounded<i128>,
    },
    /// Inner subtype constraint not satisfied.
    #[snafu(display("Inner subtype constraint not satisfied: {reason}"))]
    InnerSubtypeConstraintNotSatisfied {
        /// The reason the constraint wasn't satisfied.
        reason: super::InnerSubtypeConstraintError,
    },

    /// Codec specific error.
    #[snafu(display("{inner}"))]
    CodecSpecific {
        /// The inner error type.
        inner: CodecDecodeError,
    },

    /// Enumeration index didn't match any variant.
    #[snafu(display(
        "Enumeration index {} did not match any variant. Extended list checked: {}",
        index,
        extended_list
    ))]
    EnumerationIndexNotFound {
        /// The found index of the enumerated variant.
        index: usize,
        /// Whether the index was checked from the extended variants.
        extended_list: bool,
    },

    /// Choice index didn't match any variant.
    #[snafu(display("Choice index {index} did not match any variant"))]
    ChoiceIndexNotFound {
        /// The found index of the choice variant.
        index: usize,
        /// The variants checked for presence.
        variants: Variants,
    },

    /// Choice index exceeds maximum possible address width.
    #[snafu(display(
        "Choice index exceeds platform index width. Needed {needed} bytes, present: {present}"
    ))]
    ChoiceIndexExceedsPlatformWidth {
        /// Amount of bytes needed.
        needed: u32,
        /// Inner error
        present: DecodeError,
    },

    /// Uncategorised error.
    #[snafu(display("Custom error: {}", msg))]
    Custom {
        /// The error's message.
        msg: alloc::string::String,
    },

    /// Discriminant index didn't match any variant.
    #[snafu(display("Discriminant value {} did not match any variant", discriminant))]
    DiscriminantValueNotFound {
        /// The found value of the discriminant
        discriminant: isize,
    },

    /// Duplicate fields found.
    #[snafu(display("Duplicate field for `{}`", name))]
    DuplicateField {
        /// The field's name.
        name: &'static str,
    },

    /// Exceeds maxmium allowed length.
    #[snafu(display("Expected maximum of {} items", length))]
    ExceedsMaxLength {
        /// The maximum length.
        length: num_bigint::BigUint,
    },

    ///  More than `usize::MAX` number of data requested.
    #[snafu(display("Length of the data exceeds platform address width"))]
    LengthExceedsPlatformWidth {
        /// The specific message of the length error.
        msg: alloc::string::String,
    },

    /// An error when decoding a field in a constructed type.
    #[snafu(display("Error when decoding field `{}`: {}", name, nested))]
    FieldError {
        /// The field's name.
        name: &'static str,
        /// The underlying error type.
        nested: Box<DecodeError>,
    },

    /// Input is provided as BIT slice for nom in UPER/APER.
    /// On BER/CER/DER/OER/COER it is a BYTE slice.
    /// Hence, `needed` field can describe either bits or bytes depending on the codec.
    #[snafu(display("Need more data to continue: {:?}", needed))]
    Incomplete {
        /// Amount of bits/bytes needed.
        needed: nom::Needed,
    },
    /// Encountered EOF when decoding.
    /// BER/CER/DER uses EOF as part of the decoding logic.
    #[snafu(display("Unexpected EOF when decoding"))]
    Eof,

    /// Invalid item number in sequence.
    #[snafu(display(
        "Invalid item number in Sequence: expected: {}; actual: {}",
        expected,
        actual
    ))]
    IncorrectItemNumberInSequence {
        /// The expected item number.
        expected: usize,
        /// The actual item number.
        actual: usize,
    },

    /// Integer conversion overflow.
    #[snafu(display("Actual integer larger than expected {} bits", max_width))]
    IntegerOverflow {
        /// The maximum integer width.
        max_width: u32,
    },

    /// Integer conversion failure.
    #[snafu(display("Failed to cast integer to another integer type: {msg} "))]
    IntegerTypeConversionFailed {
        /// The reason the conversion failed.
        msg: alloc::string::String,
    },

    /// Real conversion failure.
    #[snafu(display("Invalid real encoding"))]
    InvalidRealEncoding,

    /// Decoder doesn't support REAL
    #[snafu(display("Decoder doesn't support `REAL` type"))]
    RealNotSupported,

    /// `BitString` contains an invalid amount of unused bits.
    #[snafu(display("BitString contains an invalid amount of unused bits: {}", bits))]
    InvalidBitString {
        /// The amount of invalid bits.
        bits: u8,
    },

    /// BOOL value is not `0` or `0xFF`.
    #[snafu(display(
        "Bool value is not `0` or `0xFF` as canonical requires. Actual: {}",
        value
    ))]
    InvalidBool {
        /// The invalid bool value.
        value: u8,
    },

    /// Length of Length cannot be zero.
    #[snafu(display("Length of Length cannot be zero"))]
    ZeroLengthOfLength,
    /// The length does not match what was expected.
    #[snafu(display("Expected {} bytes, actual length was {} bytes", expected, actual))]
    MismatchedLength {
        /// The expected length.
        expected: usize,
        /// The actual length.
        actual: usize,
    },

    /// Missing field in a sequence.
    #[snafu(display("Missing field `{}`", name))]
    MissingField {
        /// The field's name.
        name: &'static str,
    },
    /// When there is a mismatch between the expected and actual tag class or `value`.
    #[snafu(display(
        "Expected class: {}, value: {} in sequence or set Missing tag class or value in sequence or set",
        class,
        value
    ))]
    MissingTagClassOrValueInSequenceOrSet {
        /// The tag's class.
        class: crate::types::Class,
        /// The tag's value.
        value: u32,
    },

    /// The range of the integer exceeds the platform width.
    #[snafu(display(
        "Integer range larger than possible to address on this platform. needed: {needed} present: {present}"
    ))]
    RangeExceedsPlatformWidth {
        /// Amount of bytes needed.
        needed: u32,
        /// Amount of bytes needed.
        present: u32,
    },
    /// A specific required extension not present.
    #[snafu(display("Extension with class `{}` and tag `{}` required, but not present", tag.class, tag.value))]
    RequiredExtensionNotPresent {
        /// The tag of the required extension.
        tag: crate::types::Tag,
    },
    /// General error when parsing data.
    #[snafu(display("Error in Parser: {}", msg))]
    Parser {
        /// The error's message.
        msg: alloc::string::String,
    },
    /// General error for failed ASN.1 string conversion from bytes.
    #[snafu(display(
        "Failed to convert byte array into valid ASN.1 string. String type as tag: {} Error: {}",
        tag,
        msg
    ))]
    StringConversionFailed {
        /// Universal tag of the string type.
        tag: Tag,
        /// The error's message.
        msg: alloc::string::String,
    },
    /// General error for failed ASN.1 fixed-sized string conversion from bytes.
    #[snafu(display(
        "Failed to convert byte array into valid fixed-sized ASN.1 string. String type as tag: {}, actual: {}, expected: {}",
        tag,
        actual,
        expected
    ))]
    FixedStringConversionFailed {
        /// Tag of the string type.
        tag: Tag,
        /// Expected length
        expected: usize,
        /// Actual length
        actual: usize,
    },
    /// An error when the choice cannot be created from the given variant.
    #[snafu(display("No valid choice for `{}`", name))]
    NoValidChoice {
        /// The field's name.
        name: &'static str,
    },

    /// An error when the type is not extensible when it should.
    #[snafu(display("Attempted to decode extension on non-extensible type"))]
    TypeNotExtensible,
    /// Unexpected extra data found.
    #[snafu(display("Unexpected extra data found: length `{}` bytes", length))]
    UnexpectedExtraData {
        /// The amount of garbage data.
        length: usize,
    },
    /// An error when a unknown field is found when decoding sequence.
    #[snafu(display("Unknown field with index {} and tag {}", index, tag))]
    UnknownField {
        /// Index of the field.
        index: usize,
        /// Tag of the field.
        tag: Tag,
    },
    /// An error when there should be more data but it is not present.
    #[snafu(display("No input was provided where expected in the given SEQUENCE or INTEGER type"))]
    UnexpectedEmptyInput,

    /// An error when the decoder exceeds maximum allowed parse depth.
    #[snafu(display("Exceeded maximum parse depth"))]
    ExceedsMaxParseDepth,
}

/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for BER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum BerDecodeErrorKind {
    /// An error when the length is not definite.
    #[snafu(display("Indefinite length encountered but not allowed"))]
    IndefiniteLengthNotAllowed,
    /// An error if the value is not primitive when required.
    #[snafu(display("Invalid constructed identifier for ASN.1 value: not primitive"))]
    InvalidConstructedIdentifier,
    /// Invalid date.
    #[snafu(display("Invalid date string: {}", msg))]
    InvalidDate {
        /// The reason as string
        msg: alloc::string::String,
    },
    /// An error when the object identifier is invalid.
    #[snafu(display("Invalid object identifier with missing or corrupt root nodes"))]
    InvalidObjectIdentifier,
    /// The tag does not match what was expected.
    #[snafu(display("Expected {:?} tag, actual tag: {:?}", expected, actual))]
    MismatchedTag {
        /// The expected tag.
        expected: Tag,
        /// The actual tag.
        actual: Tag,
    },
}

impl BerDecodeErrorKind {
    /// A helper function to create an error [`BerDecodeErrorKind::InvalidDate`].
    #[must_use]
    pub fn invalid_date(msg: alloc::string::String) -> CodecDecodeError {
        CodecDecodeError::Ber(Self::InvalidDate { msg })
    }
    /// A helper function to create an error [`BerDecodeErrorKind::MismatchedTag`].
    pub fn assert_tag(expected: Tag, actual: Tag) -> core::result::Result<(), DecodeError> {
        if expected == actual {
            Ok(())
        } else {
            Err(BerDecodeErrorKind::MismatchedTag { expected, actual }.into())
        }
    }
}
// TODO check if there are more codec-specific errors here
/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for CER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum CerDecodeErrorKind {}

/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for DER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum DerDecodeErrorKind {
    /// An error when constructed encoding encountered but not allowed.
    #[snafu(display("Constructed encoding encountered but not allowed"))]
    ConstructedEncodingNotAllowed,
}

/// An error that occurred when decoding JER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum JerDecodeErrorKind {
    /// An error when the end of input is reached, but more data is needed.
    #[snafu(display("Unexpected end of input while decoding JER"))]
    EndOfInput {},
    /// An error when the JSON type is not the expected type.
    #[snafu(display(
        "Found mismatching JSON value. Expected type: {}. Found value: {}",
        needed,
        found
    ))]
    TypeMismatch {
        /// Expected JSON type.
        needed: &'static str,
        /// Found JSON value.
        found: alloc::string::String,
    },
    /// An error when the JSON value is not a valid bit string.
    #[snafu(display("Found invalid byte in bit string: {parse_int_err}"))]
    InvalidJerBitstring {
        /// The error that occurred when parsing the `BitString` byte.
        parse_int_err: ParseIntError,
    },
    /// An error when the JSON value is not a valid octet string.
    #[snafu(display("Found invalid character in octet string"))]
    InvalidJerOctetString {},
    /// An error when the JSON value is not a valid OID string.
    #[snafu(display("Failed to construct OID from value {value}",))]
    InvalidOIDString {
        /// The JSON value that could not be converted to an OID.
        value: serde_json::Value,
    },
    /// An error when the JSON value is not a valid enumerated discriminant.
    #[snafu(display("Found invalid enumerated discriminant {discriminant}",))]
    InvalidEnumDiscriminant {
        /// The invalid enumerated discriminant.
        discriminant: alloc::string::String,
    },
}

impl JerDecodeErrorKind {
    /// Helper function to create an error [`JerDecodeErrorKind::EndOfInput`].
    #[must_use]
    pub fn eoi() -> CodecDecodeError {
        CodecDecodeError::Jer(JerDecodeErrorKind::EndOfInput {})
    }
}

// TODO check if there codec-specific errors here
/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for UPER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum UperDecodeErrorKind {}

// TODO check if there codec-specific errors here
/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for APER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum AperDecodeErrorKind {}

/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for XER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum XerDecodeErrorKind {
    #[snafu(display("Unexpected end of input while decoding XER"))]
    /// An error that indicates that the XML input ended unexpectedly
    EndOfXmlInput {},
    #[snafu(display(
        "Found mismatching XML value. Expected type: {}. Found value: {}.",
        needed,
        found
    ))]
    /// An error that indicates an unexpected XML tag type or value
    XmlTypeMismatch {
        /// the expected tag type or value
        needed: &'static str,
        /// the encountered tag type or value
        found: alloc::string::String,
    },
    #[snafu(display("Found invalid character in octet string"))]
    /// An error that indicates a character in an octet string that does not conform to the hex alphabet
    InvalidXerOctetstring {
        /// Inner error thrown by the `parse` method
        parse_int_err: ParseIntError,
    },
    #[snafu(display("Encountered invalid value: {details}"))]
    /// An error that indicates invalid input XML
    InvalidInput {
        /// Error details from the underlying XML parser
        details: &'static str,
    },
    #[snafu(display("Found invalid open type encoding: {inner_err}"))]
    /// An error that indicates invalid XML in an ASN.1 open type value
    InvalidOpenType {
        /// Error details from the underlying XML parser
        inner_err: xml_no_std::writer::Error,
    },
    #[snafu(display("XML parser error: {details}"))]
    /// Miscellaneous error in the underlying XML parser
    XmlParser {
        /// Error details from the underlying XML parser
        details: alloc::string::String,
    },
    #[snafu(display("Error matching tag names: expected {needed}, found {found}"))]
    /// An error indicating an unexpected XML tag name
    XmlTag {
        /// The expected tag name
        needed: alloc::string::String,
        /// The encountered tag name
        found: alloc::string::String,
    },
    #[snafu(display("Encoding violates ITU-T X.693 (02/2021): {details}"))]
    /// An error that quotes the ITU-T X.693 paragraph being violated
    SpecViolation {
        /// Paragraph and excerpt from the ITU-T X.693 spec
        details: alloc::string::String,
    },
}

/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for OER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum OerDecodeErrorKind {
    /// Tag class must be one of Universal (0b00), Application (0b01), Context (0b10) or Private (0b11).
    #[snafu(display("Invalid tag class when decoding choice: actual {:?}", class))]
    InvalidTagClassOnChoice {
        /// The actual class.
        class: u8,
    },
    /// The tag number was incorrect when decoding a Choice type.
    #[snafu(display("Invalid tag number when decoding Choice: {value}"))]
    InvalidTagNumberOnChoice {
        /// Invalid tag bytes at the time of decoding.
        value: u32,
    },
    /// An error scenario where the tag number is not present in the expected Choice type.
    #[snafu(display(
        "Tag not found from the variants of the platform when decoding Choice. Tag: {value}, extensible status: {is_extensible}"
    ))]
    InvalidTagVariantOnChoice {
        /// The tag number that was not found.
        value: Tag,
        /// The extensible status of the Choice type.
        is_extensible: bool,
    },

    /// An error scenario where the extension header in preamble is invalid.
    InvalidExtensionHeader {
        /// The amount of invalid bits.
        msg: alloc::string::String,
    },
    /// An error scenario where the `BitString` is invalid for some reason.
    #[snafu(display("Invalid BitString: {msg}"))]
    InvalidOerBitString {
        /// The amount of invalid bits.
        msg: alloc::string::String,
    },
    /// An error scenario where the preamble is invalid.
    #[snafu(display("Invalid preamble: {msg}"))]
    InvalidPreamble {
        /// Message related to reason
        msg: alloc::string::String,
    },
}

impl OerDecodeErrorKind {
    #[must_use]
    /// Helper function to create an error [`OerDecodeErrorKind::InvalidTagNumberOnChoice`].
    pub fn invalid_tag_number_on_choice(value: u32) -> DecodeError {
        CodecDecodeError::Oer(Self::InvalidTagNumberOnChoice { value }).into()
    }
    #[must_use]
    /// Helper function to create an error [`OerDecodeErrorKind::InvalidTagVariantOnChoice`].
    pub fn invalid_tag_variant_on_choice(value: Tag, is_extensible: bool) -> DecodeError {
        CodecDecodeError::Oer(Self::InvalidTagVariantOnChoice {
            value,
            is_extensible,
        })
        .into()
    }

    /// Helper function to create an error [`OerDecodeErrorKind::InvalidExtensionHeader`].
    #[must_use]
    pub fn invalid_extension_header(msg: alloc::string::String) -> DecodeError {
        CodecDecodeError::Oer(Self::InvalidExtensionHeader { msg }).into()
    }
    /// Helper function to create an error [`OerDecodeErrorKind::InvalidOerBitString`].
    #[must_use]
    pub fn invalid_bit_string(msg: alloc::string::String) -> DecodeError {
        CodecDecodeError::Oer(Self::InvalidOerBitString { msg }).into()
    }
    /// Helper function to create an error [`OerDecodeErrorKind::InvalidPreamble`].
    #[must_use]
    pub fn invalid_preamble(msg: alloc::string::String) -> DecodeError {
        CodecDecodeError::Oer(Self::InvalidPreamble { msg }).into()
    }
}

/// `DecodeError` kinds of `Kind::CodecSpecific` which are specific for COER.
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum CoerDecodeErrorKind {
    /// An error of a result where the stricter Canonical Octet Encoding is not reached.
    #[snafu(display(
        "Invalid Canonical Octet Encoding, not encoded as the smallest possible number of octets: {msg}"
    ))]
    NotValidCanonicalEncoding {
        /// Reason for the error.
        msg: alloc::string::String,
    },
}

impl crate::de::Error for DecodeError {
    fn custom<D: core::fmt::Display>(msg: D, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::Custom {
                msg: msg.to_string(),
            },
            codec,
        )
    }
    fn incomplete(needed: nom::Needed, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::Incomplete { needed }, codec)
    }

    fn exceeds_max_length(length: num_bigint::BigUint, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::ExceedsMaxLength { length }, codec)
    }

    fn missing_field(name: &'static str, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::MissingField { name }, codec)
    }

    fn no_valid_choice(name: &'static str, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::NoValidChoice { name }, codec)
    }

    fn field_error(name: &'static str, nested: DecodeError, codec: Codec) -> Self {
        Self::from_kind(
            DecodeErrorKind::FieldError {
                name,
                nested: Box::new(nested),
            },
            codec,
        )
    }

    fn duplicate_field(name: &'static str, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::DuplicateField { name }, codec)
    }
    fn unknown_field(index: usize, tag: Tag, codec: Codec) -> Self {
        Self::from_kind(DecodeErrorKind::UnknownField { index, tag }, codec)
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    #[test]
    fn test_ber_decode_date() {
        use crate::error::{DecodeError, DecodeErrorKind};
        // "230122130000-050Z" as bytes
        let data = [
            23, 17, 50, 51, 48, 49, 50, 50, 49, 51, 48, 48, 48, 48, 45, 48, 53, 48, 90,
        ];
        let result = crate::ber::decode::<UtcTime>(&data);
        match result {
            Err(DecodeError { kind, .. }) => {
                if let DecodeErrorKind::CodecSpecific {
                    inner:
                        crate::error::CodecDecodeError::Ber(
                            crate::error::BerDecodeErrorKind::InvalidDate { msg },
                        ),
                    ..
                } = *kind
                {
                    assert_eq!(msg, "230122130000-050Z");
                } else {
                    // Handle other kinds of errors
                    panic!("Unexpected error kind: {kind}");
                }
            }
            Ok(_) => panic!("Expected error"),
        }
    }
    #[test]
    fn test_uper_missing_choice_index() {
        use crate as rasn;
        use crate::Codec;
        use crate::error::{DecodeError, DecodeErrorKind};
        #[derive(AsnType, Decode, Debug, PartialEq)]
        #[rasn(choice, automatic_tags)]
        enum MyChoice {
            Normal(Integer),
            High(Integer),
            Medium(Integer),
        }
        // Value 333 encoded for missing choice index 3
        let data = [192, 128, 83, 64];
        let result = Codec::Uper.decode_from_binary::<MyChoice>(&data);
        match result {
            Ok(_) => {
                panic!("Unexpected OK!");
            }
            Err(DecodeError { kind, .. }) => {
                if let DecodeErrorKind::ChoiceIndexNotFound { index, .. } = *kind {
                    assert_eq!(index, 3);
                } else {
                    // Handle other kinds of errors
                    panic!("Unexpected error kind: {kind}");
                }
            }
        }
    }
}