use std::collections::{HashMap, HashSet};
#[cfg(feature = "compress")]
use bc_components::Compressed;
#[cfg(feature = "encrypt")]
use bc_components::EncryptedMessage;
use bc_components::{
ARID, Digest, EncryptedKey, JSON, PrivateKeyBase, PrivateKeys, PublicKeys,
Reference, SSKRShare, Salt, SealedMessage, Signature, URI, UUID, XID,
};
use dcbor::prelude::*;
use crate::{Assertion, Envelope};
#[cfg(any(feature = "encrypt", feature = "compress"))]
use crate::{Error, Result};
pub trait EnvelopeEncodable {
fn into_envelope(self) -> Envelope;
fn to_envelope(&self) -> Envelope
where
Self: Clone,
{
self.clone().into_envelope()
}
}
impl<T> EnvelopeEncodable for T
where
T: Into<Envelope> + Clone,
{
fn into_envelope(self) -> Envelope { self.into() }
}
impl EnvelopeEncodable for Assertion {
fn into_envelope(self) -> Envelope { Envelope::new_with_assertion(self) }
}
#[cfg(feature = "encrypt")]
impl TryFrom<EncryptedMessage> for Envelope {
type Error = Error;
fn try_from(value: EncryptedMessage) -> Result<Self> {
Envelope::new_with_encrypted(value)
}
}
#[cfg(feature = "compress")]
impl TryFrom<Compressed> for Envelope {
type Error = Error;
fn try_from(compressed: Compressed) -> Result<Self> {
Envelope::new_with_compressed(compressed)
}
}
impl EnvelopeEncodable for CBOR {
fn into_envelope(self) -> Envelope { Envelope::new_leaf(self) }
}
impl EnvelopeEncodable for String {
fn into_envelope(self) -> Envelope { Envelope::new_leaf(self) }
}
impl EnvelopeEncodable for &str {
fn into_envelope(self) -> Envelope { Envelope::new_leaf(self) }
}
impl<T> EnvelopeEncodable for Vec<T>
where
T: CBOREncodable,
{
fn into_envelope(self) -> Envelope { Envelope::new(CBOR::from(self)) }
}
impl<K, V> EnvelopeEncodable for HashMap<K, V>
where
K: CBOREncodable,
V: CBOREncodable,
{
fn into_envelope(self) -> Envelope { Envelope::new(CBOR::from(self)) }
}
impl<T> EnvelopeEncodable for HashSet<T>
where
T: CBOREncodable,
{
fn into_envelope(self) -> Envelope { Envelope::new(CBOR::from(self)) }
}
impl EnvelopeEncodable for Map {
fn into_envelope(self) -> Envelope { Envelope::new(CBOR::from(self)) }
}
impl EnvelopeEncodable for Set {
fn into_envelope(self) -> Envelope { Envelope::new(CBOR::from(self)) }
}
macro_rules! impl_envelope_encodable {
($type:ty) => {
impl From<$type> for Envelope {
fn from(value: $type) -> Self { Envelope::new_leaf(value) }
}
};
}
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!(ByteString);
impl_envelope_encodable!(Date);
impl_envelope_encodable!(JSON);
impl_envelope_encodable!(PublicKeys);
impl_envelope_encodable!(PrivateKeys);
impl_envelope_encodable!(PrivateKeyBase);
impl_envelope_encodable!(SealedMessage);
impl_envelope_encodable!(EncryptedKey);
impl_envelope_encodable!(Signature);
impl_envelope_encodable!(SSKRShare);
impl_envelope_encodable!(Digest);
impl_envelope_encodable!(Salt);
impl_envelope_encodable!(ARID);
impl_envelope_encodable!(URI);
impl_envelope_encodable!(UUID);
impl_envelope_encodable!(XID);
impl_envelope_encodable!(Reference);