1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use bc_components::{SealedMessage, Digest, ARID, Salt, URI, UUID, PublicKeyBase, PrivateKeyBase};
#[cfg(feature = "encrypt")]
use bc_components::EncryptedMessage;
#[cfg(feature = "compress")]
use bc_components::Compressed;
use bytes::Bytes;
use dcbor::prelude::*;

use crate::{Envelope, Assertion};

/// A type that can be converted into an envelope.
pub trait EnvelopeEncodable: Into<Envelope> {
    fn envelope(self) -> Envelope;
}

impl EnvelopeEncodable for Envelope {
    fn envelope(self) -> Envelope {
        self
    }
}

impl EnvelopeEncodable for Assertion {
    fn envelope(self) -> Envelope {
        Envelope::new_with_assertion(self)
    }
}

impl From<Assertion> for Envelope {
    fn from(assertion: Assertion) -> Self {
        assertion.envelope()
    }
}

#[cfg(feature = "encrypt")]
impl EnvelopeEncodable for EncryptedMessage {
    fn envelope(self) -> Envelope {
        Envelope::new_with_encrypted(self).unwrap()
    }
}

#[cfg(feature = "encrypt")]
impl From<EncryptedMessage> for Envelope {
    fn from(encrypted: EncryptedMessage) -> Self {
        encrypted.envelope()
    }
}

#[cfg(feature = "compress")]
impl EnvelopeEncodable for Compressed {
    fn envelope(self) -> Envelope {
        Envelope::new_with_compressed(self).unwrap()
    }
}

#[cfg(feature = "compress")]
impl From<Compressed> for Envelope {
    fn from(compressed: Compressed) -> Self {
        compressed.envelope()
    }
}

impl EnvelopeEncodable for CBOR {
    fn envelope(self) -> Envelope {
        Envelope::new_leaf(self)
    }
}

impl From<CBOR> for Envelope {
    fn from(cbor: CBOR) -> Self {
        cbor.envelope()
    }
}

impl EnvelopeEncodable for Box<CBOR> {
    fn envelope(self) -> Envelope {
        self.as_ref().envelope()
    }
}

impl From<Box<CBOR>> for Envelope {
    fn from(cbor: Box<CBOR>) -> Self {
        cbor.envelope()
    }
}

impl EnvelopeEncodable for String {
    fn envelope(self) -> Envelope {
        self.cbor().envelope()
    }
}

impl From<String> for Envelope {
    fn from(string: String) -> Self {
        string.envelope()
    }
}

impl EnvelopeEncodable for &str {
    fn envelope(self) -> Envelope {
        self.cbor().envelope()
    }
}

impl EnvelopeEncodable for dcbor::Simple {
    fn envelope(self) -> Envelope {
        self.cbor().envelope()
    }
}

impl From<dcbor::Simple> for Envelope {
    fn from(simple: dcbor::Simple) -> Self {
        simple.envelope()
    }
}

impl From<&str> for Envelope {
    fn from(string: &str) -> Self {
        string.envelope()
    }
}

impl<const N: usize> EnvelopeEncodable for [u8; N] {
    fn envelope(self) -> Envelope {
        self.cbor().envelope()
    }
}

impl<const N: usize> From<[u8; N]> for Envelope {
    fn from(bytes: [u8; N]) -> Self {
        bytes.envelope()
    }
}

impl EnvelopeEncodable for &[u8] {
    fn envelope(self) -> Envelope {
        self.to_vec().cbor().envelope()
    }
}

impl From<&[u8]> for Envelope {
    fn from(bytes: &[u8]) -> Self {
        bytes.envelope()
    }
}

impl<T> EnvelopeEncodable for &T where T: EnvelopeEncodable + Clone {
    fn envelope(self) -> Envelope {
        self.clone().envelope()
    }
}

impl<T> From<&T> for Envelope where T: EnvelopeEncodable + Clone {
    fn from(value: &T) -> Self {
        value.envelope()
    }
}

/// A macro that implements EnvelopeEncodable for a type and its reference.
#[macro_export]
macro_rules! impl_envelope_encodable {
    ($type:ty) => {
        impl $crate::EnvelopeEncodable for $type {
            fn envelope(self) -> $crate::Envelope {
                <Self as dcbor::CBOREncodable>::cbor(&self).envelope()
            }
        }

        impl From<$type> for $crate::Envelope {
            fn from(value: $type) -> Self {
                value.envelope()
            }
        }
    };
}

impl_envelope_encodable!(u8);
impl_envelope_encodable!(u16);
impl_envelope_encodable!(u32);
impl_envelope_encodable!(u64);
impl_envelope_encodable!(usize);
impl_envelope_encodable!(i8);
impl_envelope_encodable!(i16);
impl_envelope_encodable!(i32);
impl_envelope_encodable!(i64);
impl_envelope_encodable!(bool);
impl_envelope_encodable!(f64);
impl_envelope_encodable!(f32);

impl_envelope_encodable!(Bytes);

impl_envelope_encodable!(dcbor::Date);
impl_envelope_encodable!(PublicKeyBase);
impl_envelope_encodable!(PrivateKeyBase);
impl_envelope_encodable!(SealedMessage);
impl_envelope_encodable!(Digest);
impl_envelope_encodable!(ARID);
impl_envelope_encodable!(Salt);
impl_envelope_encodable!(URI);
impl_envelope_encodable!(UUID);