1use alloc::{boxed::Box, vec::Vec};
4use num_bigint::BigInt;
5
6use crate::error::DecodeError;
7use crate::types::{self, AsnType, Constraints, Enumerated, SetOf, Tag};
8
9pub use nom::Needed;
10pub use rasn_derive::Decode;
11
12pub trait Decode: Sized + AsnType {
14 fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, D::Error> {
23 Self::decode_with_tag(decoder, Self::TAG)
24 }
25
26 fn decode_with_tag<D: Decoder>(decoder: &mut D, tag: Tag) -> Result<Self, D::Error> {
32 Self::decode_with_tag_and_constraints(decoder, tag, Self::CONSTRAINTS)
33 }
34
35 fn decode_with_constraints<D: Decoder>(
42 decoder: &mut D,
43 constraints: Constraints,
44 ) -> Result<Self, D::Error> {
45 Self::decode_with_tag_and_constraints(decoder, Self::TAG, constraints)
46 }
47
48 fn decode_with_tag_and_constraints<D: Decoder>(
55 decoder: &mut D,
56 tag: Tag,
57 constraints: Constraints,
58 ) -> Result<Self, D::Error>;
59}
60
61pub trait Decoder<const RCL: usize = 0, const ECL: usize = 0>: Sized {
66 type Ok;
68 type Error: Error + Into<crate::error::DecodeError> + From<crate::error::DecodeError>;
70 type AnyDecoder<const R: usize, const E: usize>: Decoder<RCL, ECL, Ok = Self::Ok, Error = Self::Error>
72 + Decoder;
73
74 #[must_use]
76 fn codec(&self) -> crate::Codec;
77
78 fn decode_any(&mut self) -> Result<types::Any, Self::Error>;
80 fn decode_bit_string(
82 &mut self,
83 tag: Tag,
84 constraints: Constraints,
85 ) -> Result<types::BitString, Self::Error>;
86 fn decode_bool(&mut self, tag: Tag) -> Result<bool, Self::Error>;
88 fn decode_enumerated<E: Enumerated>(&mut self, tag: Tag) -> Result<E, Self::Error>;
90 fn decode_integer<I: types::IntegerType>(
92 &mut self,
93 tag: Tag,
94 constraints: Constraints,
95 ) -> Result<I, Self::Error>;
96
97 fn decode_real<R: types::RealType>(
99 &mut self,
100 tag: Tag,
101 constraints: Constraints,
102 ) -> Result<R, Self::Error>;
103
104 fn decode_null(&mut self, tag: Tag) -> Result<(), Self::Error>;
106 fn decode_object_identifier(
108 &mut self,
109 tag: Tag,
110 ) -> Result<types::ObjectIdentifier, Self::Error>;
111 fn decode_sequence<const RC: usize, const EC: usize, D, DF, F>(
121 &mut self,
122 tag: Tag,
123 default_initializer_fn: Option<DF>,
124 decode_fn: F,
125 ) -> Result<D, Self::Error>
126 where
127 D: crate::types::Constructed<RC, EC>,
128 DF: FnOnce() -> D,
129 F: FnOnce(&mut Self::AnyDecoder<RC, EC>) -> Result<D, Self::Error>;
130 fn decode_sequence_of<D: Decode>(
132 &mut self,
133 tag: Tag,
134 constraints: Constraints,
135 ) -> Result<Vec<D>, Self::Error>;
136 fn decode_set_of<D: Decode + Eq + core::hash::Hash>(
138 &mut self,
139 tag: Tag,
140 constraints: Constraints,
141 ) -> Result<types::SetOf<D>, Self::Error>;
142 fn decode_octet_string<'buf, T>(
144 &'buf mut self,
145 tag: Tag,
146 constraints: Constraints,
147 ) -> Result<T, Self::Error>
148 where
149 T: From<&'buf [u8]> + From<Vec<u8>>;
150
151 fn decode_utf8_string(
153 &mut self,
154 tag: Tag,
155 constraints: Constraints,
156 ) -> Result<types::Utf8String, Self::Error>;
157
158 fn decode_visible_string(
160 &mut self,
161 tag: Tag,
162 constraints: Constraints,
163 ) -> Result<types::VisibleString, Self::Error>;
164
165 fn decode_general_string(
167 &mut self,
168 tag: Tag,
169 constraints: Constraints,
170 ) -> Result<types::GeneralString, Self::Error>;
171
172 fn decode_graphic_string(
174 &mut self,
175 tag: Tag,
176 constraints: Constraints,
177 ) -> Result<types::GraphicString, Self::Error>;
178
179 fn decode_ia5_string(
181 &mut self,
182 tag: Tag,
183 constraints: Constraints,
184 ) -> Result<types::Ia5String, Self::Error>;
185
186 fn decode_printable_string(
188 &mut self,
189 tag: Tag,
190 constraints: Constraints,
191 ) -> Result<types::PrintableString, Self::Error>;
192
193 fn decode_numeric_string(
195 &mut self,
196 tag: Tag,
197 constraints: Constraints,
198 ) -> Result<types::NumericString, Self::Error>;
199
200 fn decode_teletex_string(
202 &mut self,
203 tag: Tag,
204 constraints: Constraints,
205 ) -> Result<types::TeletexString, Self::Error>;
206
207 fn decode_bmp_string(
209 &mut self,
210 tag: Tag,
211 constraints: Constraints,
212 ) -> Result<types::BmpString, Self::Error>;
213
214 fn decode_explicit_prefix<D: Decode>(&mut self, tag: Tag) -> Result<D, Self::Error>;
216 fn decode_optional_with_explicit_prefix<D: Decode>(
218 &mut self,
219 tag: Tag,
220 ) -> Result<Option<D>, Self::Error>;
221 fn decode_utc_time(&mut self, tag: Tag) -> Result<types::UtcTime, Self::Error>;
223 fn decode_generalized_time(&mut self, tag: Tag) -> Result<types::GeneralizedTime, Self::Error>;
225 fn decode_date(&mut self, tag: Tag) -> Result<types::Date, Self::Error>;
227
228 fn decode_set<const RC: usize, const EC: usize, FIELDS, SET, D, F>(
242 &mut self,
243 tag: Tag,
244 decode_fn: D,
245 field_fn: F,
246 ) -> Result<SET, Self::Error>
247 where
248 SET: Decode + crate::types::Constructed<RC, EC>,
249 FIELDS: Decode,
250 D: Fn(&mut Self::AnyDecoder<RC, EC>, usize, Tag) -> Result<FIELDS, Self::Error>,
251 F: FnOnce(Vec<FIELDS>) -> Result<SET, Self::Error>;
252
253 fn decode_choice<D>(&mut self, constraints: Constraints) -> Result<D, Self::Error>
255 where
256 D: crate::types::DecodeChoice;
257
258 fn decode_optional<D: Decode>(&mut self) -> Result<Option<D>, Self::Error>;
260
261 fn decode_optional_with_tag<D: Decode>(&mut self, tag: Tag) -> Result<Option<D>, Self::Error>;
265
266 fn decode_optional_with_constraints<D: Decode>(
268 &mut self,
269 constraints: Constraints,
270 ) -> Result<Option<D>, Self::Error>;
271
272 fn decode_optional_with_tag_and_constraints<D: Decode>(
275 &mut self,
276 tag: Tag,
277 constraints: Constraints,
278 ) -> Result<Option<D>, Self::Error>;
279
280 fn decode_default<D: Decode, F: FnOnce() -> D>(
282 &mut self,
283 default_fn: F,
284 ) -> Result<D, Self::Error> {
285 self.decode_default_with_tag(D::TAG, default_fn)
286 }
287
288 fn decode_default_with_tag<D: Decode, F: FnOnce() -> D>(
290 &mut self,
291 tag: Tag,
292 default_fn: F,
293 ) -> Result<D, Self::Error> {
294 Ok(self
295 .decode_optional_with_tag::<D>(tag)?
296 .unwrap_or_else(default_fn))
297 }
298
299 fn decode_default_with_constraints<D: Decode, F: FnOnce() -> D>(
301 &mut self,
302 default_fn: F,
303 constraints: Constraints,
304 ) -> Result<D, Self::Error> {
305 Ok(self
306 .decode_optional_with_constraints::<D>(constraints)?
307 .unwrap_or_else(default_fn))
308 }
309
310 fn decode_default_with_tag_and_constraints<D: Decode, F: FnOnce() -> D>(
312 &mut self,
313 tag: Tag,
314 default_fn: F,
315 constraints: Constraints,
316 ) -> Result<D, Self::Error> {
317 Ok(self
318 .decode_optional_with_tag_and_constraints::<D>(tag, constraints)?
319 .unwrap_or_else(default_fn))
320 }
321
322 fn decode_extension_addition<D>(&mut self) -> Result<Option<D>, Self::Error>
324 where
325 D: Decode,
326 {
327 self.decode_extension_addition_with_constraints(Constraints::default())
328 }
329 fn decode_extension_addition_with_explicit_tag_and_constraints<D>(
331 &mut self,
332 tag: Tag,
333 constraints: Constraints,
334 ) -> Result<Option<D>, Self::Error>
335 where
336 D: Decode;
337
338 fn decode_extension_addition_with_tag<D>(&mut self, tag: Tag) -> Result<Option<D>, Self::Error>
340 where
341 D: Decode,
342 {
343 self.decode_extension_addition_with_tag_and_constraints(tag, Constraints::default())
344 }
345 fn decode_extension_addition_with_constraints<D>(
347 &mut self,
348 constraints: Constraints,
349 ) -> Result<Option<D>, Self::Error>
350 where
351 D: Decode,
352 {
353 self.decode_extension_addition_with_tag_and_constraints(D::TAG, constraints)
354 }
355 fn decode_extension_addition_with_tag_and_constraints<D>(
357 &mut self,
358 tag: Tag,
359 constraints: Constraints,
360 ) -> Result<Option<D>, Self::Error>
361 where
362 D: Decode;
363
364 fn decode_extension_addition_with_default<D: Decode, F: FnOnce() -> D>(
366 &mut self,
367 default_fn: F,
368 ) -> Result<D, Self::Error> {
369 self.decode_extension_addition_with_default_and_constraints(
370 default_fn,
371 Constraints::default(),
372 )
373 }
374 fn decode_extension_addition_with_default_and_tag<D: Decode, F: FnOnce() -> D>(
376 &mut self,
377 tag: Tag,
378 default_fn: F,
379 ) -> Result<D, Self::Error> {
380 self.decode_extension_addition_with_default_and_tag_and_constraints::<D, F>(
381 tag,
382 default_fn,
383 Constraints::default(),
384 )
385 }
386
387 fn decode_extension_addition_with_default_and_constraints<D: Decode, F: FnOnce() -> D>(
389 &mut self,
390 default_fn: F,
391 constraints: Constraints,
392 ) -> Result<D, Self::Error> {
393 Ok(self
394 .decode_extension_addition_with_constraints::<D>(constraints)?
395 .unwrap_or_else(default_fn))
396 }
397 fn decode_extension_addition_with_default_and_tag_and_constraints<
399 D: Decode,
400 F: FnOnce() -> D,
401 >(
402 &mut self,
403 tag: Tag,
404 default_fn: F,
405 constraints: Constraints,
406 ) -> Result<D, Self::Error> {
407 Ok(self
408 .decode_extension_addition_with_tag_and_constraints::<D>(tag, constraints)?
409 .unwrap_or_else(default_fn))
410 }
411
412 fn decode_extension_addition_group<
418 const RC: usize,
419 const EC: usize,
420 D: Decode + crate::types::Constructed<RC, EC>,
421 >(
422 &mut self,
423 ) -> Result<Option<D>, Self::Error>;
424}
425
426pub trait Error: core::fmt::Display {
429 #[must_use]
431 fn custom<D: core::fmt::Display>(msg: D, codec: crate::Codec) -> Self;
432 #[must_use]
434 fn incomplete(needed: Needed, codec: crate::Codec) -> Self;
435 #[must_use]
437 fn exceeds_max_length(length: num_bigint::BigUint, codec: crate::Codec) -> Self;
438 #[must_use]
440 fn missing_field(name: &'static str, codec: crate::Codec) -> Self;
441 #[must_use]
443 fn no_valid_choice(name: &'static str, codec: crate::Codec) -> Self;
444 #[must_use]
447 fn field_error(name: &'static str, error: DecodeError, codec: crate::Codec) -> Self;
448 #[must_use]
450 fn duplicate_field(name: &'static str, codec: crate::Codec) -> Self;
451 #[must_use]
453 fn unknown_field(index: usize, tag: Tag, codec: crate::Codec) -> Self;
454}
455
456impl Decode for () {
457 fn decode_with_tag_and_constraints<D: Decoder>(
458 decoder: &mut D,
459 tag: Tag,
460 _: Constraints,
461 ) -> Result<Self, D::Error> {
462 decoder.decode_null(tag)
463 }
464}
465
466impl<D: Decode> Decode for Option<D> {
467 fn decode<DE: Decoder>(decoder: &mut DE) -> Result<Self, DE::Error> {
468 decoder.decode_optional()
469 }
470
471 fn decode_with_tag<DE: Decoder>(decoder: &mut DE, tag: Tag) -> Result<Self, DE::Error> {
472 decoder.decode_optional_with_tag(tag)
473 }
474
475 fn decode_with_constraints<DE: Decoder>(
476 decoder: &mut DE,
477 constraints: Constraints,
478 ) -> Result<Self, DE::Error> {
479 decoder.decode_optional_with_constraints(constraints)
480 }
481
482 fn decode_with_tag_and_constraints<DE: Decoder>(
483 decoder: &mut DE,
484 tag: Tag,
485 constraints: Constraints,
486 ) -> Result<Self, DE::Error> {
487 decoder.decode_optional_with_tag_and_constraints(tag, constraints)
488 }
489}
490
491impl Decode for bool {
492 fn decode_with_tag_and_constraints<D: Decoder>(
493 decoder: &mut D,
494 tag: Tag,
495 _: Constraints,
496 ) -> Result<Self, D::Error> {
497 decoder.decode_bool(tag)
498 }
499}
500
501macro_rules! impl_integers {
502 ($($int:ty),+ $(,)?) => {
503 $(
504 impl Decode for $int {
505 fn decode_with_tag_and_constraints<D: Decoder>(decoder: &mut D, tag: Tag, constraints: Constraints) -> Result<Self, D::Error> {
506 decoder.decode_integer::<$int>(tag, constraints)
507 }
508 }
509 )+
510 }
511}
512
513impl_integers! {
514 i8,
515 i16,
516 i32,
517 i64,
518 i128,
519 isize,
520 u8,
521 u16,
522 u32,
523 u64,
524 usize,
527 BigInt
528}
529
530impl<const START: i128, const END: i128> Decode for types::ConstrainedInteger<START, END> {
531 fn decode_with_tag_and_constraints<D: Decoder>(
532 decoder: &mut D,
533 tag: Tag,
534 constraints: Constraints,
535 ) -> Result<Self, D::Error> {
536 decoder
537 .decode_integer::<types::Integer>(tag, constraints)
538 .map(Self)
539 }
540}
541
542impl Decode for types::Integer {
543 fn decode_with_tag_and_constraints<D: Decoder>(
544 decoder: &mut D,
545 tag: Tag,
546 constraints: Constraints,
547 ) -> Result<Self, D::Error> {
548 decoder.decode_integer::<types::Integer>(tag, constraints)
549 }
550}
551
552#[cfg(feature = "f32")]
553impl Decode for f32 {
554 fn decode_with_tag_and_constraints<D: Decoder>(
555 decoder: &mut D,
556 tag: Tag,
557 _: Constraints,
558 ) -> Result<Self, D::Error> {
559 decoder.decode_real::<f32>(tag, Constraints::default())
560 }
561}
562
563#[cfg(feature = "f64")]
564impl Decode for f64 {
565 fn decode_with_tag_and_constraints<D: Decoder>(
566 decoder: &mut D,
567 tag: Tag,
568 _: Constraints,
569 ) -> Result<Self, D::Error> {
570 decoder.decode_real::<f64>(tag, Constraints::default())
571 }
572}
573
574impl<T: Decode> Decode for Box<T> {
575 fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, D::Error> {
576 T::decode(decoder).map(Box::new)
577 }
578
579 fn decode_with_tag<D: Decoder>(decoder: &mut D, tag: Tag) -> Result<Self, D::Error> {
580 T::decode_with_tag(decoder, tag).map(Box::new)
581 }
582
583 fn decode_with_constraints<DE: Decoder>(
584 decoder: &mut DE,
585 constraints: Constraints,
586 ) -> Result<Self, DE::Error> {
587 T::decode_with_constraints(decoder, constraints).map(Box::new)
588 }
589
590 fn decode_with_tag_and_constraints<DE: Decoder>(
591 decoder: &mut DE,
592 tag: Tag,
593 constraints: Constraints,
594 ) -> Result<Self, DE::Error> {
595 T::decode_with_tag_and_constraints(decoder, tag, constraints).map(Box::new)
596 }
597}
598
599impl Decode for types::OctetString {
600 fn decode_with_tag_and_constraints<D: Decoder>(
601 decoder: &mut D,
602 tag: Tag,
603 constraints: Constraints,
604 ) -> Result<Self, D::Error> {
605 decoder
606 .decode_octet_string::<Vec<u8>>(tag, constraints)
607 .map(Into::into)
608 }
609}
610
611impl Decode for types::ObjectIdentifier {
612 fn decode_with_tag_and_constraints<D: Decoder>(
613 decoder: &mut D,
614 tag: Tag,
615 _: Constraints,
616 ) -> Result<Self, D::Error> {
617 decoder.decode_object_identifier(tag)
618 }
619}
620
621impl Decode for types::Utf8String {
622 fn decode_with_tag_and_constraints<D: Decoder>(
623 decoder: &mut D,
624 tag: Tag,
625 constraints: Constraints,
626 ) -> Result<Self, D::Error> {
627 decoder.decode_utf8_string(tag, constraints)
628 }
629}
630
631impl Decode for types::UtcTime {
632 fn decode_with_tag_and_constraints<D: Decoder>(
633 decoder: &mut D,
634 tag: Tag,
635 _: Constraints,
636 ) -> Result<Self, D::Error> {
637 decoder.decode_utc_time(tag)
638 }
639}
640
641impl Decode for types::GeneralizedTime {
642 fn decode_with_tag_and_constraints<D: Decoder>(
643 decoder: &mut D,
644 tag: Tag,
645 _: Constraints,
646 ) -> Result<Self, D::Error> {
647 decoder.decode_generalized_time(tag)
648 }
649}
650
651impl Decode for types::Any {
652 fn decode_with_tag_and_constraints<D: Decoder>(
653 decoder: &mut D,
654 _: Tag,
655 _: Constraints,
656 ) -> Result<Self, D::Error> {
657 decoder.decode_any()
658 }
659}
660
661impl<T: Decode> Decode for alloc::vec::Vec<T> {
662 fn decode_with_tag_and_constraints<D: Decoder>(
663 decoder: &mut D,
664 tag: Tag,
665 constraints: Constraints,
666 ) -> Result<Self, D::Error> {
667 decoder.decode_sequence_of(tag, constraints)
668 }
669}
670
671impl<T: Decode + Eq + core::hash::Hash> Decode for SetOf<T> {
672 fn decode_with_tag_and_constraints<D: Decoder>(
673 decoder: &mut D,
674 tag: Tag,
675 constraints: Constraints,
676 ) -> Result<Self, D::Error> {
677 decoder.decode_set_of(tag, constraints)
678 }
679}
680
681impl<T: Decode, const N: usize> Decode for [T; N] {
682 fn decode_with_tag_and_constraints<D: Decoder>(
683 decoder: &mut D,
684 tag: Tag,
685 constraints: Constraints,
686 ) -> Result<Self, D::Error> {
687 let sequence = decoder.decode_sequence_of(tag, constraints)?;
688 sequence.try_into().map_err(|seq: Vec<_>| {
689 D::Error::from(DecodeError::incorrect_item_number_in_sequence(
690 N,
691 seq.len(),
692 decoder.codec(),
693 ))
694 })
695 }
696}
697
698impl<T: AsnType, V: Decode> Decode for types::Implicit<T, V> {
699 fn decode_with_tag_and_constraints<D: Decoder>(
700 decoder: &mut D,
701 tag: Tag,
702 constraints: Constraints,
703 ) -> Result<Self, D::Error> {
704 Ok(Self::new(V::decode_with_tag_and_constraints(
705 decoder,
706 tag,
707 constraints,
708 )?))
709 }
710}
711
712impl<T: AsnType, V: Decode> Decode for types::Explicit<T, V> {
713 fn decode_with_tag_and_constraints<D: Decoder>(
714 decoder: &mut D,
715 tag: Tag,
716 _: Constraints,
717 ) -> Result<Self, D::Error> {
718 Ok(Self::new(decoder.decode_explicit_prefix(tag)?))
719 }
720}