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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use hex;
use serde_json::Value;
use std::fmt;
use std::iter::FromIterator;
use std::ops::Deref;
use std::sync::Arc;
use super::api::{APIAction, IOAction, Mood};
use super::util::maybe_utf8;
pub use super::wordlist::Wordlist;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct AppID(pub String);
impl Deref for AppID {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for AppID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}
impl<'a> From<&'a str> for AppID {
fn from(s: &'a str) -> AppID {
AppID(s.to_string())
}
}
#[derive(PartialEq, Eq, Clone)]
pub struct Key(pub Vec<u8>);
impl Deref for Key {
type Target = Vec<u8>;
fn deref(&self) -> &Vec<u8> {
&self.0
}
}
impl fmt::Debug for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Key(REDACTED)")
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct MySide(pub String);
impl Deref for MySide {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for MySide {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct TheirSide(pub String);
impl Deref for TheirSide {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for TheirSide {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub struct Phase(pub String);
impl Deref for Phase {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for Phase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Mailbox(pub String);
impl Deref for Mailbox {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for Mailbox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Mailbox({})", &self.0)
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Nameplate(pub String);
impl Deref for Nameplate {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for Nameplate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Code(pub String);
impl Deref for Code {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}
impl fmt::Display for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}
#[derive(Debug, PartialEq)]
pub enum AllocatorEvent {
Allocate(Arc<Wordlist>),
Connected,
Lost,
RxAllocated(Nameplate),
}
#[allow(dead_code)] #[derive(PartialEq)]
pub enum BossEvent {
RxWelcome(Value),
RxError,
Error,
Closed,
GotCode(Code),
GotKey(Key), Scared,
Happy,
GotVerifier(Vec<u8>), GotMessage(Phase, Vec<u8>),
}
impl fmt::Debug for BossEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::BossEvent::*;
let t = match *self {
RxWelcome(ref v) => format!("RxWelcome({:?})", v),
RxError => "RxError".to_string(),
Error => "Error".to_string(),
Closed => "Closed".to_string(),
GotCode(ref code) => format!("GotCode({:?})", code),
GotKey(ref _key) => "GotKey(REDACTED)".to_string(),
Scared => "Scared".to_string(),
Happy => "Happy".to_string(),
GotVerifier(ref v) => format!("GotVerifier({})", maybe_utf8(v)),
GotMessage(ref phase, ref msg) => {
format!("GotMessage({:?}, {})", phase, maybe_utf8(msg))
}
};
write!(f, "BossEvent::{}", t)
}
}
#[derive(Debug, PartialEq)]
pub enum CodeEvent {
AllocateCode(Arc<Wordlist>),
InputCode,
SetCode(Code),
Allocated(Nameplate, Code),
GotNameplate(Nameplate),
FinishedInput(Code),
}
#[derive(Debug, PartialEq)]
pub enum InputEvent {
Start,
ChooseNameplate(Nameplate),
ChooseWords(String),
GotNameplates(Vec<Nameplate>),
GotWordlist(Arc<Wordlist>),
RefreshNameplates,
}
#[derive(PartialEq)]
pub enum KeyEvent {
GotCode(Code),
GotPake(Vec<u8>),
}
impl fmt::Debug for KeyEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::KeyEvent::*;
let t = match *self {
GotCode(ref code) => format!("GotCode({:?})", code),
GotPake(ref pake) => format!("GotPake({})", hex::encode(pake)),
};
write!(f, "KeyEvent::{}", t)
}
}
#[derive(Debug, PartialEq)]
pub enum ListerEvent {
Connected,
Lost,
RxNameplates(Vec<Nameplate>),
Refresh,
}
#[derive(PartialEq)]
pub enum MailboxEvent {
Connected,
Lost,
RxMessage(TheirSide, Phase, Vec<u8>), RxClosed,
Close(Mood),
GotMailbox(Mailbox),
AddMessage(Phase, Vec<u8>), }
impl fmt::Debug for MailboxEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::MailboxEvent::*;
let t = match *self {
Connected => "Connected".to_string(),
Lost => "Lost".to_string(),
RxMessage(ref side, ref phase, ref body) => format!(
"RxMessage(side={:?}, phase={}, body={})",
side,
phase,
maybe_utf8(body)
),
RxClosed => "RxClosed".to_string(),
Close(ref mood) => format!("Close({:?})", mood),
GotMailbox(ref mailbox) => format!("GotMailbox({})", mailbox),
AddMessage(ref phase, ref body) => {
format!("AddMessage({}, {})", phase, maybe_utf8(body))
}
};
write!(f, "MailboxEvent::{}", t)
}
}
#[derive(Debug, PartialEq)]
pub enum NameplateEvent {
Connected,
Lost,
RxClaimed(Mailbox),
RxReleased,
SetNameplate(Nameplate),
Release,
Close,
}
#[derive(PartialEq)]
pub enum OrderEvent {
GotMessage(TheirSide, Phase, Vec<u8>), }
impl fmt::Debug for OrderEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::OrderEvent::*;
let t = match *self {
GotMessage(ref side, ref phase, ref body) => format!(
"GotMessage(side={:?}, phase={}, body={})",
side,
phase,
maybe_utf8(body)
),
};
write!(f, "OrderEvent::{}", t)
}
}
#[derive(PartialEq)]
pub enum ReceiveEvent {
GotMessage(TheirSide, Phase, Vec<u8>), GotKey(Key),
}
impl fmt::Debug for ReceiveEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::ReceiveEvent::*;
let t = match *self {
GotMessage(ref side, ref phase, ref body) => format!(
"GotMessage(side={:?}, phase={}, body={})",
side,
phase,
maybe_utf8(body)
),
GotKey(ref _key) => "GotKey(REDACTED)".to_string(),
};
write!(f, "ReceiveEvent::{}", t)
}
}
#[derive(PartialEq)]
pub enum RendezvousEvent {
Start,
TxBind(AppID, MySide),
TxOpen(Mailbox),
TxAdd(Phase, Vec<u8>), TxClose(Mailbox, Mood),
Stop,
TxClaim(Nameplate), TxRelease(Nameplate), TxAllocate,
TxList,
}
impl fmt::Debug for RendezvousEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::RendezvousEvent::*;
let t = match *self {
Start => "Start".to_string(),
TxBind(ref appid, ref side) => {
format!("TxBind(appid={}, side={:?})", appid, side)
}
TxOpen(ref mailbox) => format!("TxOpen({})", mailbox),
TxAdd(ref phase, ref body) => {
format!("TxAdd({}, {})", phase, maybe_utf8(body))
}
TxClose(ref mailbox, ref mood) => {
format!("TxClose({}, {:?})", mailbox, mood)
}
Stop => "Stop".to_string(),
TxClaim(ref nameplate) => format!("TxClaim({:?})", nameplate),
TxRelease(ref nameplate) => format!("TxRelease({:?})", nameplate),
TxAllocate => "TxAllocate".to_string(),
TxList => "TxList".to_string(),
};
write!(f, "RendezvousEvent::{}", t)
}
}
#[derive(PartialEq)]
pub enum SendEvent {
Send(Phase, Vec<u8>), GotVerifiedKey(Key),
}
impl fmt::Debug for SendEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SendEvent::Send(ref phase, ref plaintext) => {
write!(f, "Send({}, {})", phase, maybe_utf8(plaintext))
}
SendEvent::GotVerifiedKey(_) => write!(f, "Send(GotVerifiedKey)"),
}
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum TerminatorEvent {
Close(Mood),
MailboxDone,
NameplateDone,
Stopped,
}
#[derive(Debug, PartialEq)]
pub enum Event {
API(APIAction),
IO(IOAction),
Allocator(AllocatorEvent),
Boss(BossEvent),
Code(CodeEvent),
Input(InputEvent),
Key(KeyEvent),
Lister(ListerEvent),
Mailbox(MailboxEvent),
Nameplate(NameplateEvent),
Order(OrderEvent),
Receive(ReceiveEvent),
Rendezvous(RendezvousEvent),
Send(SendEvent),
Terminator(TerminatorEvent),
}
impl From<APIAction> for Event {
fn from(r: APIAction) -> Self {
Event::API(r)
}
}
impl From<IOAction> for Event {
fn from(r: IOAction) -> Self {
Event::IO(r)
}
}
impl From<AllocatorEvent> for Event {
fn from(r: AllocatorEvent) -> Self {
Event::Allocator(r)
}
}
impl From<BossEvent> for Event {
fn from(r: BossEvent) -> Self {
Event::Boss(r)
}
}
impl From<CodeEvent> for Event {
fn from(r: CodeEvent) -> Self {
Event::Code(r)
}
}
impl From<InputEvent> for Event {
fn from(r: InputEvent) -> Self {
Event::Input(r)
}
}
impl From<KeyEvent> for Event {
fn from(r: KeyEvent) -> Self {
Event::Key(r)
}
}
impl From<ListerEvent> for Event {
fn from(r: ListerEvent) -> Self {
Event::Lister(r)
}
}
impl From<MailboxEvent> for Event {
fn from(r: MailboxEvent) -> Self {
Event::Mailbox(r)
}
}
impl From<NameplateEvent> for Event {
fn from(r: NameplateEvent) -> Self {
Event::Nameplate(r)
}
}
impl From<OrderEvent> for Event {
fn from(r: OrderEvent) -> Self {
Event::Order(r)
}
}
impl From<ReceiveEvent> for Event {
fn from(r: ReceiveEvent) -> Self {
Event::Receive(r)
}
}
impl From<RendezvousEvent> for Event {
fn from(r: RendezvousEvent) -> Self {
Event::Rendezvous(r)
}
}
impl From<SendEvent> for Event {
fn from(r: SendEvent) -> Self {
Event::Send(r)
}
}
impl From<TerminatorEvent> for Event {
fn from(r: TerminatorEvent) -> Self {
Event::Terminator(r)
}
}
#[derive(Debug, PartialEq)]
pub struct Events {
pub events: Vec<Event>,
}
use std::convert::From;
impl Events {
pub fn new() -> Events {
Events { events: vec![] }
}
pub fn push<T>(&mut self, item: T)
where
Event: From<T>,
{
self.events.push(Event::from(item));
}
pub fn append(&mut self, other: &mut Events) {
self.events.append(&mut other.events);
}
}
impl IntoIterator for Events {
type Item = Event;
type IntoIter = ::std::vec::IntoIter<Event>;
fn into_iter(self) -> Self::IntoIter {
self.events.into_iter()
}
}
impl FromIterator<Event> for Events {
fn from_iter<I: IntoIterator<Item = Event>>(iter: I) -> Self {
let mut c = Events::new();
for i in iter {
c.events.push(i);
}
c
}
}
macro_rules! events {
( ) => {
{
use crate::core::events::Events;
Events::new()
}
};
( $( $x:expr ),* $(,)*) => {
{
use crate::core::events::Events;
let mut temp_vec = Events::new();
$(
temp_vec.push($x);
)*
temp_vec
}
};
}