Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/der/src/decode.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / der / src / decode.rs` 99 lines ยท 3064 bytes Raw Blame History 1 //! Trait definition for [`Decode`]. 2 3 use crate::{FixedTag, Header, Reader, Result, SliceReader}; 4 use core::marker::PhantomData; 5 6 #[cfg(feature = "pem")] 7 use crate::{pem::PemLabel, PemReader}; 8 9 #[cfg(doc)] 10 use crate::{Length, Tag}; 11 12 #[cfg(feature = "alloc")] 13 use alloc::boxed::Box; 14 15 /// Decoding trait. 16 /// 17 /// This trait provides the core abstraction upon which all decoding operations 18 /// are based. 19 pub trait Decode<'a>: Sized { 20 /// Attempt to decode this message using the provided decoder. 21 fn decode>(decoder: &mut R) -> Result; 22 23 /// Parse `Self` from the provided DER-encoded byte slice. 24 fn from_der(bytes: &'a [u8]) -> Result { 25 let mut reader = SliceReader::new(bytes)?; 26 let result = Self::decode(&mut reader)?; 27 reader.finish(result) 28 } 29 } 30 31 impl<'a, T> Decode<'a> for T 32 where 33 T: DecodeValue<'a> + FixedTag, 34 { 35 fn decode>(reader: &mut R) -> Result { 36 let header = Header::decode(reader)?; 37 header.tag.assert_eq(T::TAG)?; 38 T::decode_value(reader, header) 39 } 40 } 41 42 /// Dummy implementation for [`PhantomData`] which allows deriving 43 /// implementations on structs with phantom fields. 44 impl<'a, T> Decode<'a> for PhantomData 45 where 46 T: ?Sized, 47 { 48 fn decode>(_reader: &mut R) -> Result> { 49 Ok(PhantomData) 50 } 51 } 52 53 /// Marker trait for data structures that can be decoded from DER without 54 /// borrowing any data from the decoder. 55 /// 56 /// This is primarily useful for trait bounds on functions which require that 57 /// no data is borrowed from the decoder, for example a PEM decoder which needs 58 /// to first decode data from Base64. 59 /// 60 /// This trait is inspired by the [`DeserializeOwned` trait from `serde`](https://docs.rs/serde/latest/serde/de/trait.DeserializeOwned.html). 61 pub trait DecodeOwned: for<'a> Decode<'a> {} 62 63 impl DecodeOwned for T where T: for<'a> Decode<'a> {} 64 65 /// PEM decoding trait. 66 /// 67 /// This trait is automatically impl'd for any type which impls both 68 /// [`DecodeOwned`] and [`PemLabel`]. 69 #[cfg(feature = "pem")] 70 pub trait DecodePem: DecodeOwned + PemLabel { 71 /// Try to decode this type from PEM. 72 fn from_pem(pem: impl AsRef<[u8]>) -> Result; 73 } 74 75 #[cfg(feature = "pem")] 76 impl DecodePem for T { 77 fn from_pem(pem: impl AsRef<[u8]>) -> Result { 78 let mut reader = PemReader::new(pem.as_ref())?; 79 Self::validate_pem_label(reader.type_label())?; 80 T::decode(&mut reader) 81 } 82 } 83 84 /// Decode the value part of a Tag-Length-Value encoded field, sans the [`Tag`] 85 /// and [`Length`]. 86 pub trait DecodeValue<'a>: Sized { 87 /// Attempt to decode this message using the provided [`Reader`]. 88 fn decode_value>(reader: &mut R, header: Header) -> Result; 89 } 90 91 #[cfg(feature = "alloc")] 92 impl<'a, T> DecodeValue<'a> for Box 93 where 94 T: DecodeValue<'a>, 95 { 96 fn decode_value>(reader: &mut R, header: Header) -> Result { 97 Ok(Box::new(T::decode_value(reader, header)?)) 98 } 99 }