rasn/
enc.rs

1//! Generic ASN.1 encoding framework.
2
3use crate::types::{self, AsnType, Constraints, Enumerated, IntegerType, SetOf, Tag};
4use crate::types::{Identifier, RealType};
5use num_bigint::BigInt;
6pub use rasn_derive::Encode;
7
8/// A **data type** that can be encoded to a ASN.1 data format.
9pub trait Encode: AsnType {
10    /// Encodes `self`'s data into the given [`crate::Encoder`].
11    ///
12    /// **Note for implementors** You typically do not need to implement this.
13    /// The default implementation will call [`Encode::encode_with_tag_and_constraints`] with
14    /// your types associated [`AsnType::TAG`] and [`AsnType::CONSTRAINTS`]. You
15    /// should only ever need to implement this if you have a type that *cannot*
16    /// be implicitly tagged, such as a `CHOICE` type, in which case you want to
17    /// implement encoding in [`Self::encode`].
18    fn encode<'b, E: Encoder<'b>>(&self, encoder: &mut E) -> Result<(), E::Error> {
19        self.encode_with_tag_and_constraints(
20            encoder,
21            Self::TAG,
22            Self::CONSTRAINTS,
23            Self::IDENTIFIER,
24        )
25    }
26
27    /// Encode this value with `tag` into the given [`crate::Encoder`].
28    ///
29    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
30    /// this will **explicitly tag** the value, for all other types, it will
31    /// **implicitly** tag the value.
32    fn encode_with_tag<'b, E: Encoder<'b>>(
33        &self,
34        encoder: &mut E,
35        tag: Tag,
36    ) -> Result<(), E::Error> {
37        self.encode_with_tag_and_constraints(encoder, tag, Self::CONSTRAINTS, Self::IDENTIFIER)
38    }
39
40    /// Encode this value with `identifier` into the given [`crate::Encoder`].
41    ///
42    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
43    /// this will **explicitly tag** the value, for all other types, it will
44    /// **implicitly** tag the value.
45    fn encode_with_identifier<'b, E: Encoder<'b>>(
46        &self,
47        encoder: &mut E,
48        identifier: Identifier,
49    ) -> Result<(), E::Error> {
50        self.encode_with_tag_and_constraints(encoder, Self::TAG, Self::CONSTRAINTS, identifier)
51    }
52
53    /// Encode this value with `tag` and `identifier` into the given [`crate::Encoder`].
54    ///
55    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
56    /// this will **explicitly tag** the value, for all other types, it will
57    /// **implicitly** tag the value.
58    fn encode_with_tag_and_identifier<'b, E: Encoder<'b>>(
59        &self,
60        encoder: &mut E,
61        tag: Tag,
62        identifier: Identifier,
63    ) -> Result<(), E::Error> {
64        self.encode_with_tag_and_constraints(encoder, tag, Self::CONSTRAINTS, identifier)
65    }
66
67    /// Encode this value into the given [`crate::Encoder`] with the
68    /// constraints the values this is allowed to encode into.
69    ///
70    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
71    /// this will **explicitly tag** the value, for all other types, it will
72    /// **implicitly** tag the value.
73    fn encode_with_constraints<'b, E: Encoder<'b>>(
74        &self,
75        encoder: &mut E,
76        constraints: Constraints,
77    ) -> Result<(), E::Error> {
78        self.encode_with_tag_and_constraints(encoder, Self::TAG, constraints, Self::IDENTIFIER)
79    }
80
81    /// Encode this value into the given [`crate::Encoder`] with `identifier` and the
82    /// constraints the values this is allowed to encode into.
83    ///
84    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
85    /// this will **explicitly tag** the value, for all other types, it will
86    /// **implicitly** tag the value.
87    fn encode_with_constraints_and_identifier<'b, E: Encoder<'b>>(
88        &self,
89        encoder: &mut E,
90        constraints: Constraints,
91        identifier: Identifier,
92    ) -> Result<(), E::Error> {
93        self.encode_with_tag_and_constraints(encoder, Self::TAG, constraints, identifier)
94    }
95
96    /// Encode this value with `tag` into the given [`crate::Encoder`] with the
97    /// constraints the values this is allowed to encode into.
98    ///
99    /// **Note** For `CHOICE` and other types that cannot be implicitly tagged
100    /// this will **explicitly tag** the value, for all other types, it will
101    /// **implicitly** tag the value.
102    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
103        &self,
104        encoder: &mut E,
105        tag: Tag,
106        constraints: Constraints,
107        identifier: Identifier,
108    ) -> Result<(), E::Error>;
109}
110
111/// A **data format** encode any ASN.1 data type.
112///
113/// Const `RCL` is the count of root components in the root component list of a sequence or set.
114/// Const `ECL` is the count of extension additions in the extension addition component type list in a sequence or set.
115pub trait Encoder<'encoder, const RCL: usize = 0, const ECL: usize = 0> {
116    /// The associated success type returned on success.
117    type Ok;
118    /// The associated error type returned on failure.
119    type Error: Error + Into<crate::error::EncodeError> + From<crate::error::EncodeError>;
120    /// Helper type for encoding recursive `Encoder` instances with different `RCL` or  `ECL` values.
121    type AnyEncoder<'other_encoder, const R: usize, const E: usize>: Encoder<
122        'other_encoder,
123        RCL,
124        ECL,
125        Ok = Self::Ok,
126        Error = Self::Error,
127    >;
128
129    /// Returns codec variant of `Codec` that current encoder is encoding.
130    fn codec(&self) -> crate::Codec;
131
132    /// Encode an unknown ASN.1 value.
133    fn encode_any(
134        &mut self,
135        tag: Tag,
136        value: &types::Any,
137        identifier: Identifier,
138    ) -> Result<Self::Ok, Self::Error>;
139
140    /// Encode a `BOOL` value.
141    fn encode_bool(
142        &mut self,
143        tag: Tag,
144        value: bool,
145        identifier: Identifier,
146    ) -> Result<Self::Ok, Self::Error>;
147
148    /// Encode a `BIT STRING` value.
149    fn encode_bit_string(
150        &mut self,
151        tag: Tag,
152        constraints: Constraints,
153        value: &types::BitStr,
154        identifier: Identifier,
155    ) -> Result<Self::Ok, Self::Error>;
156
157    /// Encode a `ENUMERATED` value.
158    fn encode_enumerated<E: Enumerated>(
159        &mut self,
160        tag: Tag,
161        value: &E,
162        identifier: Identifier,
163    ) -> Result<Self::Ok, Self::Error>;
164
165    /// Encode a `OBJECT IDENTIFIER` value.
166    fn encode_object_identifier(
167        &mut self,
168        tag: Tag,
169        value: &[u32],
170        identifier: Identifier,
171    ) -> Result<Self::Ok, Self::Error>;
172
173    /// Encode a `INTEGER` value.
174    fn encode_integer<I: IntegerType>(
175        &mut self,
176        tag: Tag,
177        constraints: Constraints,
178        value: &I,
179        identifier: Identifier,
180    ) -> Result<Self::Ok, Self::Error>;
181
182    /// Encode a `REAL` value.
183    fn encode_real<R: RealType>(
184        &mut self,
185        tag: Tag,
186        constraints: Constraints,
187        value: &R,
188        identifier: Identifier,
189    ) -> Result<Self::Ok, Self::Error>;
190
191    /// Encode a `NULL` value.
192    fn encode_null(&mut self, tag: Tag, identifier: Identifier) -> Result<Self::Ok, Self::Error>;
193
194    /// Encode a `OCTET STRING` value.
195    fn encode_octet_string(
196        &mut self,
197        tag: Tag,
198        constraints: Constraints,
199        value: &[u8],
200        identifier: Identifier,
201    ) -> Result<Self::Ok, Self::Error>;
202
203    /// Encode a `GeneralString` value.
204    fn encode_general_string(
205        &mut self,
206        tag: Tag,
207        constraints: Constraints,
208        value: &types::GeneralString,
209        identifier: Identifier,
210    ) -> Result<Self::Ok, Self::Error>;
211
212    /// Encode a `GraphicString` value.
213    fn encode_graphic_string(
214        &mut self,
215        tag: Tag,
216        constraints: Constraints,
217        value: &types::GraphicString,
218        identifier: Identifier,
219    ) -> Result<Self::Ok, Self::Error>;
220
221    /// Encode a `Utf8String` value.
222    fn encode_utf8_string(
223        &mut self,
224        tag: Tag,
225        constraints: Constraints,
226        value: &str,
227        identifier: Identifier,
228    ) -> Result<Self::Ok, Self::Error>;
229
230    /// Encode a `VisibleString` value.
231    fn encode_visible_string(
232        &mut self,
233        tag: Tag,
234        constraints: Constraints,
235        value: &types::VisibleString,
236        identifier: Identifier,
237    ) -> Result<Self::Ok, Self::Error>;
238
239    /// Encode a `Ia5String` value.
240    fn encode_ia5_string(
241        &mut self,
242        tag: Tag,
243        constraints: Constraints,
244        value: &types::Ia5String,
245        identifier: Identifier,
246    ) -> Result<Self::Ok, Self::Error>;
247
248    /// Encode a `Ia5String` value.
249    fn encode_printable_string(
250        &mut self,
251        tag: Tag,
252        constraints: Constraints,
253        value: &types::PrintableString,
254        identifier: Identifier,
255    ) -> Result<Self::Ok, Self::Error>;
256
257    /// Encode a `NumericString` value.
258    fn encode_numeric_string(
259        &mut self,
260        tag: Tag,
261        constraints: Constraints,
262        value: &types::NumericString,
263        identifier: Identifier,
264    ) -> Result<Self::Ok, Self::Error>;
265
266    /// Encode a `TeletexString` value.
267    fn encode_teletex_string(
268        &mut self,
269        tag: Tag,
270        constraints: Constraints,
271        value: &types::TeletexString,
272        identifier: Identifier,
273    ) -> Result<Self::Ok, Self::Error>;
274
275    /// Encode a `BmpString` value.
276    fn encode_bmp_string(
277        &mut self,
278        tag: Tag,
279        constraints: Constraints,
280        value: &types::BmpString,
281        identifier: Identifier,
282    ) -> Result<Self::Ok, Self::Error>;
283
284    /// Encode a `GeneralizedTime` value.
285    fn encode_generalized_time(
286        &mut self,
287        tag: Tag,
288        value: &types::GeneralizedTime,
289        identifier: Identifier,
290    ) -> Result<Self::Ok, Self::Error>;
291
292    /// Encode a `UtcTime` value.
293    fn encode_utc_time(
294        &mut self,
295        tag: Tag,
296        value: &types::UtcTime,
297        identifier: Identifier,
298    ) -> Result<Self::Ok, Self::Error>;
299
300    /// Encode a 'Date' value.
301    fn encode_date(
302        &mut self,
303        tag: Tag,
304        value: &types::Date,
305        identifier: Identifier,
306    ) -> Result<Self::Ok, Self::Error>;
307
308    /// Encode a explicitly tagged value.
309    fn encode_explicit_prefix<V: Encode>(
310        &mut self,
311        tag: Tag,
312        value: &V,
313        identifier: Identifier,
314    ) -> Result<Self::Ok, Self::Error>;
315
316    /// Encode a `SEQUENCE` value.
317    ///
318    /// Const `RC` is the count of root components in a sequence.
319    /// Const `EC` is the count of extension addition components in a sequence.
320    /// Generic `C` is the sequence type.
321    /// Generic `F` is the closure that will encode the sequence by encoding the fields in the order as defined in the type.
322    /// NOTE: If you implement this manually, make sure to encode fields in the same order and pass the correct count of fields.
323    fn encode_sequence<'b, const RC: usize, const EC: usize, C, F>(
324        &'b mut self,
325        tag: Tag,
326        encoder_scope: F,
327        identifier: Identifier,
328    ) -> Result<Self::Ok, Self::Error>
329    where
330        C: crate::types::Constructed<RC, EC>,
331        F: FnOnce(&mut Self::AnyEncoder<'b, RC, EC>) -> Result<(), Self::Error>;
332
333    /// Encode a `SEQUENCE OF` value.
334    fn encode_sequence_of<E: Encode>(
335        &mut self,
336        tag: Tag,
337        value: &[E],
338        constraints: Constraints,
339        identifier: Identifier,
340    ) -> Result<Self::Ok, Self::Error>;
341
342    /// Encode a `SET` value.
343    ///
344    /// Const `RC` is the count of root components in a set.
345    /// Const `EC` is the count of extension addition components in a set.
346    /// Generic `C` is the set type.
347    /// Generic `F` is the closure that will encode the set fields in appearance order. Encoder will rearrange them in the correct order later.
348    /// NOTE: If you implement this manually, make sure to encode fields in the same order and pass the correct count of fields.
349    fn encode_set<'b, const RC: usize, const EC: usize, C, F>(
350        &'b mut self,
351        tag: Tag,
352        value: F,
353        identifier: Identifier,
354    ) -> Result<Self::Ok, Self::Error>
355    where
356        C: crate::types::Constructed<RC, EC>,
357        F: FnOnce(&mut Self::AnyEncoder<'b, RC, EC>) -> Result<(), Self::Error>;
358
359    /// Encode a `SET OF` value.
360    fn encode_set_of<E: Encode + Eq + core::hash::Hash>(
361        &mut self,
362        tag: Tag,
363        value: &types::SetOf<E>,
364        constraints: Constraints,
365        identifier: Identifier,
366    ) -> Result<Self::Ok, Self::Error>;
367
368    /// Encode the value a field or skip if it matches the default..
369    fn encode_or_default<E: Encode + Default + PartialEq>(
370        &mut self,
371        value: &E,
372        identifier: Identifier,
373    ) -> Result<Self::Ok, Self::Error> {
374        if value == &E::default() {
375            self.encode_none::<E>(identifier)
376        } else {
377            self.encode_some(value, identifier)
378        }
379    }
380
381    /// Encode the present value of an optional field.
382    fn encode_with_tag_or_default<E: Encode + Default + PartialEq>(
383        &mut self,
384        tag: Tag,
385        value: &E,
386        identifier: Identifier,
387    ) -> Result<Self::Ok, Self::Error> {
388        if value == &E::default() {
389            self.encode_none_with_tag(tag, identifier)
390        } else {
391            self.encode_some_with_tag(tag, value, identifier)
392        }
393    }
394
395    /// Encode the present value of an optional field.
396    fn encode_some<E: Encode>(
397        &mut self,
398        value: &E,
399        identifier: Identifier,
400    ) -> Result<Self::Ok, Self::Error>;
401
402    /// Encode the present value of an optional field.
403    fn encode_some_with_tag<E: Encode>(
404        &mut self,
405        tag: Tag,
406        value: &E,
407        identifier: Identifier,
408    ) -> Result<Self::Ok, Self::Error> {
409        self.encode_some_with_tag_and_constraints(tag, E::CONSTRAINTS, value, identifier)
410    }
411
412    /// Encode the present value of an optional field.
413    fn encode_some_with_tag_and_constraints<E: Encode>(
414        &mut self,
415        tag: Tag,
416        constraints: Constraints,
417        value: &E,
418        identifier: Identifier,
419    ) -> Result<Self::Ok, Self::Error>;
420
421    /// Encode the absent value of an optional field.
422    fn encode_none<E: Encode>(&mut self, identifier: Identifier) -> Result<Self::Ok, Self::Error>;
423
424    /// Encode the absent value with `tag` of an optional field.
425    fn encode_none_with_tag(
426        &mut self,
427        tag: Tag,
428        identifier: Identifier,
429    ) -> Result<Self::Ok, Self::Error>;
430
431    /// Encode the present value of an optional field.
432    fn encode_default<E: Encode + PartialEq>(
433        &mut self,
434        value: &E,
435        default: impl FnOnce() -> E,
436        identifier: Identifier,
437    ) -> Result<Self::Ok, Self::Error> {
438        match (*value != (default)()).then_some(value) {
439            Some(value) => self.encode_some(value, identifier),
440            None => self.encode_none::<E>(identifier),
441        }
442    }
443
444    /// Encode the present value of an optional field.
445    fn encode_default_with_tag<E: Encode + PartialEq>(
446        &mut self,
447        tag: Tag,
448        value: &E,
449        default: impl FnOnce() -> E,
450        identifier: Identifier,
451    ) -> Result<Self::Ok, Self::Error> {
452        match (*value != (default)()).then_some(value) {
453            Some(value) => self.encode_some_with_tag(tag, value, identifier),
454            None => self.encode_none_with_tag(tag, identifier),
455        }
456    }
457
458    /// Encode the preset value of an optional field using Explicit Tags
459    fn encode_default_with_explicit_prefix<E: Encode + PartialEq>(
460        &mut self,
461        tag: Tag,
462        value: &E,
463        default: impl FnOnce() -> E,
464        identifier: Identifier,
465    ) -> Result<Self::Ok, Self::Error> {
466        match (*value != (default)()).then_some(value) {
467            Some(value) => self.encode_explicit_prefix(tag, value, identifier),
468            None => self.encode_none_with_tag(tag, identifier),
469        }
470    }
471
472    /// Encode the present value of an optional field.
473    fn encode_default_with_tag_and_constraints<E: Encode + PartialEq>(
474        &mut self,
475        tag: Tag,
476        constraints: Constraints,
477        value: &E,
478        default: impl FnOnce() -> E,
479        identifier: Identifier,
480    ) -> Result<Self::Ok, Self::Error> {
481        match (*value != (default)()).then_some(value) {
482            Some(value) => {
483                self.encode_some_with_tag_and_constraints(tag, constraints, value, identifier)
484            }
485            None => self.encode_none_with_tag(tag, identifier),
486        }
487    }
488
489    /// Encode the present constrained value of an optional field.
490    fn encode_default_with_constraints<E: Encode + PartialEq>(
491        &mut self,
492        constraints: Constraints,
493        value: &E,
494        default: impl FnOnce() -> E,
495        identifier: Identifier,
496    ) -> Result<Self::Ok, Self::Error> {
497        match (*value != (default)()).then_some(value) {
498            Some(value) => {
499                self.encode_some_with_tag_and_constraints(E::TAG, constraints, value, identifier)
500            }
501            None => self.encode_none_with_tag(E::TAG, identifier),
502        }
503    }
504
505    /// Encode a `CHOICE` value.
506    fn encode_choice<E: Encode + crate::types::Choice>(
507        &mut self,
508        constraints: Constraints,
509        tag: Tag,
510        encode_fn: impl FnOnce(&mut Self) -> Result<Tag, Self::Error>,
511        identifier: Identifier,
512    ) -> Result<Self::Ok, Self::Error>;
513
514    /// Encode a extension addition value.
515    fn encode_extension_addition<E: Encode>(
516        &mut self,
517        tag: Tag,
518        constraints: Constraints,
519        value: E,
520        identifier: Identifier,
521    ) -> Result<Self::Ok, Self::Error>;
522
523    /// Encode a extension addition group value.
524    ///
525    /// Const `RC` is the count of root components in sequence or set.
526    /// Const `EC` is the count of extension components in sequence or set.
527    /// `E` is the type of the extension addition group value being encoded.
528    fn encode_extension_addition_group<const RC: usize, const EC: usize, E>(
529        &mut self,
530        value: Option<&E>,
531        identifier: Identifier,
532    ) -> Result<Self::Ok, Self::Error>
533    where
534        E: Encode + crate::types::Constructed<RC, EC>;
535}
536
537/// A generic error that occurred while trying to encode ASN.1.
538pub trait Error: core::fmt::Display {
539    /// Creates a new general error using `msg` and current `codec` when encoding ASN.1.
540    fn custom<D: core::fmt::Display>(msg: D, codec: crate::Codec) -> Self;
541}
542
543impl Error for core::convert::Infallible {
544    fn custom<D: core::fmt::Display>(msg: D, codec: crate::Codec) -> Self {
545        core::panic!("Infallible error! {}, from: {}", msg, codec)
546    }
547}
548
549impl<E: Encode> Encode for &'_ E {
550    fn encode<'b, EN: Encoder<'b>>(&self, encoder: &mut EN) -> Result<(), EN::Error> {
551        E::encode(self, encoder)
552    }
553
554    fn encode_with_tag<'b, EN: Encoder<'b>>(
555        &self,
556        encoder: &mut EN,
557        tag: Tag,
558    ) -> Result<(), EN::Error> {
559        E::encode_with_tag(self, encoder, tag)
560    }
561
562    fn encode_with_tag_and_identifier<'b, EN: Encoder<'b>>(
563        &self,
564        encoder: &mut EN,
565        tag: Tag,
566        identifier: Identifier,
567    ) -> Result<(), EN::Error> {
568        E::encode_with_tag_and_identifier(self, encoder, tag, identifier)
569    }
570
571    fn encode_with_identifier<'b, EN: Encoder<'b>>(
572        &self,
573        encoder: &mut EN,
574        identifier: Identifier,
575    ) -> Result<(), EN::Error> {
576        E::encode_with_identifier(self, encoder, identifier)
577    }
578
579    fn encode_with_constraints<'b, EN: Encoder<'b>>(
580        &self,
581        encoder: &mut EN,
582        constraints: Constraints,
583    ) -> Result<(), EN::Error> {
584        E::encode_with_constraints(self, encoder, constraints)
585    }
586
587    fn encode_with_constraints_and_identifier<'b, EN: Encoder<'b>>(
588        &self,
589        encoder: &mut EN,
590        constraints: Constraints,
591        identifier: Identifier,
592    ) -> Result<(), EN::Error> {
593        E::encode_with_constraints_and_identifier(self, encoder, constraints, identifier)
594    }
595
596    fn encode_with_tag_and_constraints<'b, EN: Encoder<'b>>(
597        &self,
598        encoder: &mut EN,
599        tag: Tag,
600        constraints: Constraints,
601        identifier: Identifier,
602    ) -> Result<(), EN::Error> {
603        E::encode_with_tag_and_constraints(self, encoder, tag, constraints, identifier)
604    }
605}
606
607impl Encode for () {
608    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
609        &self,
610        encoder: &mut E,
611        tag: Tag,
612        _: Constraints,
613        identifier: Identifier,
614    ) -> Result<(), E::Error> {
615        encoder.encode_null(tag, identifier).map(drop)
616    }
617}
618
619impl<E: Encode> Encode for Option<E> {
620    fn encode<'b, EN: Encoder<'b>>(&self, encoder: &mut EN) -> Result<(), EN::Error> {
621        match self {
622            Some(value) => encoder.encode_some::<E>(value, Self::IDENTIFIER),
623            None => encoder.encode_none::<E>(Self::IDENTIFIER),
624        }
625        .map(drop)
626    }
627
628    fn encode_with_tag<'b, EN: Encoder<'b>>(
629        &self,
630        encoder: &mut EN,
631        tag: Tag,
632    ) -> Result<(), EN::Error> {
633        match self {
634            Some(value) => encoder.encode_some_with_tag(tag, value, Self::IDENTIFIER),
635            None => encoder.encode_none_with_tag(tag, Self::IDENTIFIER),
636        }
637        .map(drop)
638    }
639
640    fn encode_with_tag_and_identifier<'b, EN: Encoder<'b>>(
641        &self,
642        encoder: &mut EN,
643        tag: Tag,
644        identifier: Identifier,
645    ) -> Result<(), EN::Error> {
646        match self {
647            Some(value) => encoder.encode_some_with_tag(tag, value, identifier),
648            None => encoder.encode_none_with_tag(tag, identifier),
649        }
650        .map(drop)
651    }
652
653    fn encode_with_constraints<'b, EN: Encoder<'b>>(
654        &self,
655        encoder: &mut EN,
656        constraints: Constraints,
657    ) -> Result<(), EN::Error> {
658        match self {
659            Some(value) => encoder.encode_some_with_tag_and_constraints(
660                Self::TAG,
661                constraints,
662                value,
663                Self::IDENTIFIER,
664            ),
665            None => encoder.encode_none_with_tag(Self::TAG, Self::IDENTIFIER),
666        }
667        .map(drop)
668    }
669
670    fn encode_with_constraints_and_identifier<'b, EN: Encoder<'b>>(
671        &self,
672        encoder: &mut EN,
673        constraints: Constraints,
674        identifier: Identifier,
675    ) -> Result<(), EN::Error> {
676        match self {
677            Some(value) => encoder.encode_some_with_tag_and_constraints(
678                Self::TAG,
679                constraints,
680                value,
681                identifier,
682            ),
683            None => encoder.encode_none_with_tag(Self::TAG, identifier),
684        }
685        .map(drop)
686    }
687
688    fn encode_with_identifier<'b, EN: Encoder<'b>>(
689        &self,
690        encoder: &mut EN,
691        identifier: Identifier,
692    ) -> Result<(), EN::Error> {
693        match self {
694            Some(value) => encoder.encode_some_with_tag(Self::TAG, value, identifier),
695            None => encoder.encode_none_with_tag(Self::TAG, identifier),
696        }
697        .map(drop)
698    }
699
700    fn encode_with_tag_and_constraints<'b, EN: Encoder<'b>>(
701        &self,
702        encoder: &mut EN,
703        tag: Tag,
704        constraints: Constraints,
705        identifier: Identifier,
706    ) -> Result<(), EN::Error> {
707        match self {
708            Some(value) => {
709                encoder.encode_some_with_tag_and_constraints(tag, constraints, value, identifier)
710            }
711
712            None => encoder.encode_none_with_tag(tag, identifier),
713        }
714        .map(drop)
715    }
716}
717
718impl Encode for bool {
719    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
720        &self,
721        encoder: &mut E,
722        tag: Tag,
723        _: Constraints,
724        identifier: Identifier,
725    ) -> Result<(), E::Error> {
726        encoder.encode_bool(tag, *self, identifier).map(drop)
727    }
728}
729
730macro_rules! impl_integers {
731    ($($int:ty),+) => {
732        $(
733            impl Encode for $int {
734                fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(&self, encoder: &mut E, tag: Tag, constraints: Constraints, identifier: Identifier) -> Result<(), E::Error> {
735                    encoder.encode_integer(
736                        tag,
737                        constraints,
738                        self,
739                        identifier
740                    ).map(drop)
741                }
742            }
743        )+
744    }
745}
746
747impl_integers! {
748    i8,
749    i16,
750    i32,
751    i64,
752    i128,
753    isize,
754    u8,
755    u16,
756    u32,
757    u64,
758    // TODO cannot support u128 as it is constrained type by default and current constraints uses i128 for bounds
759    u128,
760    usize
761}
762
763impl Encode for BigInt {
764    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
765        &self,
766        encoder: &mut E,
767        tag: Tag,
768        constraints: Constraints,
769        identifier: Identifier,
770    ) -> Result<(), E::Error> {
771        encoder
772            .encode_integer(tag, constraints, self, identifier.or(Self::IDENTIFIER))
773            .map(drop)
774    }
775}
776
777impl<const START: i128, const END: i128> Encode for types::ConstrainedInteger<START, END> {
778    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
779        &self,
780        encoder: &mut E,
781        tag: Tag,
782        constraints: Constraints,
783        identifier: Identifier,
784    ) -> Result<(), E::Error> {
785        encoder
786            .encode_integer(tag, constraints, &**self, identifier.or(Self::IDENTIFIER))
787            .map(drop)
788    }
789}
790
791impl Encode for types::Integer {
792    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
793        &self,
794        encoder: &mut E,
795        tag: Tag,
796        constraints: Constraints,
797        identifier: Identifier,
798    ) -> Result<(), E::Error> {
799        encoder
800            .encode_integer(tag, constraints, self, identifier.or(Self::IDENTIFIER))
801            .map(drop)
802    }
803}
804
805#[cfg(feature = "f32")]
806impl Encode for f32 {
807    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
808        &self,
809        encoder: &mut E,
810        tag: Tag,
811        constraints: Constraints,
812        identifier: Identifier,
813    ) -> Result<(), E::Error> {
814        encoder
815            .encode_real(tag, constraints, self, identifier.or(Self::IDENTIFIER))
816            .map(drop)
817    }
818}
819
820#[cfg(feature = "f64")]
821impl Encode for f64 {
822    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
823        &self,
824        encoder: &mut E,
825        tag: Tag,
826        constraints: Constraints,
827        identifier: Identifier,
828    ) -> Result<(), E::Error> {
829        encoder
830            .encode_real(tag, constraints, self, identifier.or(Self::IDENTIFIER))
831            .map(drop)
832    }
833}
834
835impl Encode for types::OctetString {
836    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
837        &self,
838        encoder: &mut E,
839        tag: Tag,
840        constraints: Constraints,
841        identifier: Identifier,
842    ) -> Result<(), E::Error> {
843        encoder
844            .encode_octet_string(tag, constraints, self, identifier.or(Self::IDENTIFIER))
845            .map(drop)
846    }
847}
848
849impl Encode for types::Utf8String {
850    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
851        &self,
852        encoder: &mut E,
853        tag: Tag,
854        constraints: Constraints,
855        identifier: Identifier,
856    ) -> Result<(), E::Error> {
857        encoder
858            .encode_utf8_string(tag, constraints, self, identifier.or(Self::IDENTIFIER))
859            .map(drop)
860    }
861}
862
863impl Encode for &'_ str {
864    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
865        &self,
866        encoder: &mut E,
867        tag: Tag,
868        constraints: Constraints,
869        identifier: Identifier,
870    ) -> Result<(), E::Error> {
871        encoder
872            .encode_utf8_string(tag, constraints, self, identifier.or(Self::IDENTIFIER))
873            .map(drop)
874    }
875}
876
877impl Encode for types::ObjectIdentifier {
878    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
879        &self,
880        encoder: &mut E,
881        tag: Tag,
882        _: Constraints,
883        identifier: Identifier,
884    ) -> Result<(), E::Error> {
885        encoder
886            .encode_object_identifier(tag, self, identifier.or(Self::IDENTIFIER))
887            .map(drop)
888    }
889}
890
891impl Encode for types::Oid {
892    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
893        &self,
894        encoder: &mut E,
895        tag: Tag,
896        _: Constraints,
897        identifier: Identifier,
898    ) -> Result<(), E::Error> {
899        encoder
900            .encode_object_identifier(tag, self, identifier.or(Self::IDENTIFIER))
901            .map(drop)
902    }
903}
904
905impl Encode for types::UtcTime {
906    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
907        &self,
908        encoder: &mut E,
909        tag: Tag,
910        _: Constraints,
911        identifier: Identifier,
912    ) -> Result<(), E::Error> {
913        encoder
914            .encode_utc_time(tag, self, identifier.or(Self::IDENTIFIER))
915            .map(drop)
916    }
917}
918
919impl Encode for types::GeneralizedTime {
920    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
921        &self,
922        encoder: &mut E,
923        tag: Tag,
924        _: Constraints,
925        identifier: Identifier,
926    ) -> Result<(), E::Error> {
927        encoder
928            .encode_generalized_time(tag, self, identifier.or(Self::IDENTIFIER))
929            .map(drop)
930    }
931}
932
933impl Encode for types::Any {
934    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
935        &self,
936        encoder: &mut E,
937        tag: Tag,
938        _: Constraints,
939        identifier: Identifier,
940    ) -> Result<(), E::Error> {
941        encoder
942            .encode_any(tag, self, identifier.or(Self::IDENTIFIER))
943            .map(drop)
944    }
945}
946
947impl<E: Encode> Encode for alloc::boxed::Box<E> {
948    fn encode<'b, EN: Encoder<'b>>(&self, encoder: &mut EN) -> Result<(), EN::Error> {
949        E::encode(self, encoder)
950    }
951
952    fn encode_with_tag<'b, EN: Encoder<'b>>(
953        &self,
954        encoder: &mut EN,
955        tag: Tag,
956    ) -> Result<(), EN::Error> {
957        E::encode_with_tag(self, encoder, tag)
958    }
959
960    fn encode_with_constraints<'b, EN: Encoder<'b>>(
961        &self,
962        encoder: &mut EN,
963        constraints: Constraints,
964    ) -> Result<(), EN::Error> {
965        E::encode_with_constraints(self, encoder, constraints)
966    }
967
968    fn encode_with_identifier<'b, EN: Encoder<'b>>(
969        &self,
970        encoder: &mut EN,
971        identifier: Identifier,
972    ) -> Result<(), EN::Error> {
973        E::encode_with_identifier(self, encoder, identifier)
974    }
975
976    fn encode_with_tag_and_constraints<'b, EN: Encoder<'b>>(
977        &self,
978        encoder: &mut EN,
979        tag: Tag,
980        constraints: Constraints,
981        identifier: Identifier,
982    ) -> Result<(), EN::Error> {
983        E::encode_with_tag_and_constraints(
984            self,
985            encoder,
986            tag,
987            constraints,
988            identifier.or(Self::IDENTIFIER),
989        )
990    }
991}
992
993impl<E: Encode> Encode for alloc::vec::Vec<E> {
994    fn encode_with_tag_and_constraints<'b, EN: Encoder<'b>>(
995        &self,
996        encoder: &mut EN,
997        tag: Tag,
998        constraints: Constraints,
999        identifier: Identifier,
1000    ) -> Result<(), EN::Error> {
1001        encoder
1002            .encode_sequence_of(tag, self, constraints, identifier.or(Self::IDENTIFIER))
1003            .map(drop)
1004    }
1005}
1006
1007impl<E: Encode + Eq + core::hash::Hash> Encode for SetOf<E> {
1008    fn encode_with_tag_and_constraints<'b, EN: Encoder<'b>>(
1009        &self,
1010        encoder: &mut EN,
1011        tag: Tag,
1012        constraints: Constraints,
1013        identifier: Identifier,
1014    ) -> Result<(), EN::Error> {
1015        encoder
1016            .encode_set_of(tag, self, constraints, identifier.or(Self::IDENTIFIER))
1017            .map(drop)
1018    }
1019}
1020
1021impl<E: Encode, const N: usize> Encode for [E; N] {
1022    fn encode_with_tag_and_constraints<'b, EN: Encoder<'b>>(
1023        &self,
1024        encoder: &mut EN,
1025        tag: Tag,
1026        constraints: Constraints,
1027        identifier: Identifier,
1028    ) -> Result<(), EN::Error> {
1029        encoder
1030            .encode_sequence_of(tag, self, constraints, identifier.or(Self::IDENTIFIER))
1031            .map(drop)
1032    }
1033}
1034
1035impl<T: AsnType, V: Encode> Encode for types::Implicit<T, V> {
1036    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
1037        &self,
1038        encoder: &mut E,
1039        tag: Tag,
1040        constraints: Constraints,
1041        identifier: Identifier,
1042    ) -> Result<(), E::Error> {
1043        V::encode_with_tag_and_constraints(
1044            &self.value,
1045            encoder,
1046            tag,
1047            constraints,
1048            identifier.or(Self::IDENTIFIER),
1049        )
1050        .map(drop)
1051    }
1052}
1053
1054impl<T: AsnType, V: Encode> Encode for types::Explicit<T, V> {
1055    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
1056        &self,
1057        encoder: &mut E,
1058        tag: Tag,
1059        _: Constraints,
1060        identifier: Identifier,
1061    ) -> Result<(), E::Error> {
1062        encoder
1063            .encode_explicit_prefix(tag, &self.value, identifier.or(Self::IDENTIFIER))
1064            .map(drop)
1065    }
1066}
1067
1068impl<T: AsnType> Encode for core::marker::PhantomData<T> {
1069    fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
1070        &self,
1071        _: &mut E,
1072        _: Tag,
1073        _: Constraints,
1074        _: Identifier,
1075    ) -> Result<(), E::Error> {
1076        Ok(())
1077    }
1078}