use super::{Prefix, Xorable};
use crust::PeerId;
use rust_sodium::crypto::{hash, sign};
use std::fmt::{self, Binary, Debug, Display, Formatter};
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
pub enum Authority<N: Xorable + Clone + Copy + Binary + Default> {
ClientManager(N),
NaeManager(N),
NodeManager(N),
Section(N),
PrefixSection(Prefix<N>),
ManagedNode(N),
Client {
client_key: sign::PublicKey,
peer_id: PeerId,
proxy_node_name: N,
},
}
impl<N: Xorable + Clone + Copy + Binary + Default> Authority<N> {
pub fn is_multiple(&self) -> bool {
match *self {
Authority::Section(_) |
Authority::PrefixSection(_) |
Authority::ClientManager(_) |
Authority::NaeManager(_) |
Authority::NodeManager(_) => true,
Authority::ManagedNode(_) |
Authority::Client { .. } => false,
}
}
pub fn is_single(&self) -> bool {
match *self {
Authority::ClientManager(_) |
Authority::NaeManager(_) |
Authority::Section(_) |
Authority::PrefixSection(_) |
Authority::NodeManager(_) => false,
Authority::ManagedNode(_) |
Authority::Client { .. } => true,
}
}
pub fn is_client(&self) -> bool {
if let Authority::Client { .. } = *self {
true
} else {
false
}
}
pub fn name(&self) -> N {
match *self {
Authority::ClientManager(ref name) |
Authority::NaeManager(ref name) |
Authority::NodeManager(ref name) |
Authority::Section(ref name) |
Authority::ManagedNode(ref name) => *name,
Authority::PrefixSection(ref prefix) => prefix.lower_bound(),
Authority::Client { ref proxy_node_name, .. } => *proxy_node_name,
}
}
}
impl<N: Xorable + Clone + Copy + Binary + Default + Display> Debug for Authority<N> {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match *self {
Authority::ClientManager(ref name) => {
write!(formatter, "ClientManager(name: {})", name)
}
Authority::NaeManager(ref name) => write!(formatter, "NaeManager(name: {})", name),
Authority::NodeManager(ref name) => write!(formatter, "NodeManager(name: {})", name),
Authority::Section(ref name) => write!(formatter, "Section(name: {})", name),
Authority::PrefixSection(ref prefix) => {
write!(formatter, "PrefixSection(prefix: {:?})", prefix)
}
Authority::ManagedNode(ref name) => write!(formatter, "ManagedNode(name: {})", name),
Authority::Client { ref client_key, ref proxy_node_name, ref peer_id } => {
write!(formatter,
"Client {{ client_name: {}, proxy_node_name: {}, peer_id: {:?} }}",
N::from_hash(hash::sha256::hash(&client_key[..]).0),
proxy_node_name,
peer_id)
}
}
}
}