use std::hash::Hash;
use cfg_if::cfg_if;
cfg_if!{
if #[cfg(feature = "serde")] {
use serde::de::DeserializeOwned;
use serde::Serialize;
}
}
use crate::router::Router;
pub trait RoutingSystem {
const MAX_WARN_LENGTH: usize = 1000;
const TRUST_RESYNC_SEQNO: bool = true;
type NodeAddress: RootData + RootKey;
type Link: RootData + RootKey;
type MACSystem: MACSystem<Self>;
}
cfg_if!{
if #[cfg(feature = "serde")] {
pub trait RootData: Clone + Serialize + DeserializeOwned + Sized {}
impl<T: Clone + Serialize + DeserializeOwned + Sized> RootData for T {}
}
else{
pub trait RootData: Clone + Sized {}
impl<T: Clone + Sized> RootData for T {}
}
}
pub trait RootKey: Eq + PartialEq + Hash {}
impl<T: Eq + PartialEq + Hash> RootKey for T {}
pub trait MACSignature<V: RootData, T: RoutingSystem + ?Sized>: RootData
{
fn data(&self) -> &V;
fn data_mut(&mut self) -> &mut V;
}
pub trait MACSystem<T: RoutingSystem + ?Sized>: Default {
type MACSignatureType<V: RootData>: MACSignature<V, T>;
fn sign<V: RootData>(&self, data: V, router: &Router<T>) -> Self::MACSignatureType<V>;
fn validate<V: RootData>(&self, sig: &MAC<V, T>, subject: &T::NodeAddress) -> bool;
}
pub type MAC<V, T> = <<T as RoutingSystem>::MACSystem as MACSystem<T>>::MACSignatureType<V>;