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
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)
}
}
}
}