1use crate::buffer::Buffer;
7
8macro_rules! generated_encodings {
9 ($(($name:ident, $name_type:ident)),*) => {
10 use core::fmt::{Debug, Error, Formatter};
11 use core::ops::Deref;
12 mod buffer_type {
13 $(
14 pub struct $name_type {
15 _priv: (),
16 }
17 )*
18 }
19 $(
20 pub struct $name<'a>(Buffer<'a, buffer_type::$name_type>);
22
23 impl<'a> Deref for $name<'a> {
24 type Target = Buffer<'a, buffer_type::$name_type>;
25
26 fn deref(&self) -> &Self::Target {
27 &self.0
28 }
29 }
30
31 impl $name<'static> {
32 #[allow(dead_code)]
33 pub(crate) fn new(owned: Vec<u8>) -> Self {
34 Self(Buffer::new(owned))
35 }
36 #[allow(dead_code)]
37 pub(crate) fn take_from_slice(owned: &mut [u8]) -> Self {
38 Self(Buffer::take_from_slice(owned))
39 }
40 }
41
42 impl Debug for $name<'_> {
43 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
44 f.debug_struct(stringify!($name)).finish()
45 }
46 }
47
48 impl<'a> From<Buffer<'a, buffer_type::$name_type>> for $name<'a> {
49 fn from(value: Buffer<'a, buffer_type::$name_type>) -> Self {
50 Self(value)
51 }
52 }
53 )*
54 }
55}
56pub(crate) use generated_encodings;
57generated_encodings!(
58 (Curve25519SeedBin, Curve25519SeedBinType),
59 (EcPrivateKeyBin, EcPrivateKeyBinType),
60 (EcPrivateKeyRfc5915Der, EcPrivateKeyRfc5915DerType),
61 (EcPublicKeyCompressedBin, EcPublicKeyCompressedBinType),
62 (EcPublicKeyUncompressedBin, EcPublicKeyUncompressedBinType),
63 (Pkcs8V1Der, Pkcs8V1DerType),
64 (Pkcs8V2Der, Pkcs8V2DerType),
65 (PqdsaPrivateKeyRaw, PqdsaPrivateKeyRawType),
66 (PqdsaSeedRaw, PqdsaSeedRawType),
67 (PublicKeyX509Der, PublicKeyX509DerType)
68);
69
70pub trait AsDer<T> {
72 fn as_der(&self) -> Result<T, crate::error::Unspecified>;
77}
78
79pub trait AsBigEndian<T> {
81 fn as_be_bytes(&self) -> Result<T, crate::error::Unspecified>;
86}
87
88pub trait AsRawBytes<T> {
90 fn as_raw_bytes(&self) -> Result<T, crate::error::Unspecified>;
95}