Skip to main content

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