Skip to content

Commit

Permalink
Merge branch 'newtypes'
Browse files Browse the repository at this point in the history
Adds newtypes for MySide, TheirSide, Nameplate, and Code. Still a few more to
add.

refs #32
  • Loading branch information
warner committed May 27, 2018
2 parents 94be84e + 3bde52c commit 26dcb19
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 171 deletions.
4 changes: 2 additions & 2 deletions core/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use events::{Events, Wordlist};
use events::{Code, Events, Wordlist};
use std::sync::Arc;

// we process these
Expand Down Expand Up @@ -101,7 +101,7 @@ impl AllocatorMachine {
),
RxAllocated(nameplate) => {
let words = wordlist.choose_words();
let code = nameplate.clone() + "-" + &words;
let code = Code(nameplate.to_string() + "-" + &words);
(
Some(State::S2Done),
events![C_Allocated(nameplate, code)],
Expand Down
10 changes: 5 additions & 5 deletions core/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use events::Key;
use events::{Code, Key};
use hex;
use std::collections::HashMap;
use std::error::Error;
Expand All @@ -14,7 +14,7 @@ pub enum APIEvent {
InputHelperRefreshNameplates,
InputHelperChooseNameplate(String),
InputHelperChooseWords(String),
SetCode(String),
SetCode(Code),
Close,
Send(Vec<u8>),
}
Expand All @@ -37,7 +37,7 @@ impl fmt::Debug for APIEvent {
InputHelperChooseWords(ref words) => {
format!("InputHelperChooseWords({})", words)
}
SetCode(ref code) => format!("SetCode({})", code),
SetCode(ref code) => format!("SetCode({:?})", code),
Close => "Close".to_string(),
Send(ref msg) => format!("Send({})", maybe_utf8(msg)),
};
Expand Down Expand Up @@ -119,7 +119,7 @@ impl fmt::Display for Mood {
pub enum APIAction {
// from WormholeCore out through IO glue to application
GotWelcome(HashMap<String, String>), // actually anything JSON-able: Value
GotCode(String), // must be easy to canonically encode into UTF-8 bytes
GotCode(Code), // must be easy to canonically encode into UTF-8 bytes
GotUnverifiedKey(Key),
GotVerifier(Vec<u8>),
GotVersions(HashMap<String, String>), // actually anything JSON-able
Expand All @@ -132,7 +132,7 @@ impl fmt::Debug for APIAction {
use self::APIAction::*;
let t = match *self {
GotWelcome(ref welcome) => format!("GotWelcome({:?})", welcome),
GotCode(ref code) => format!("GotCode({})", code),
GotCode(ref code) => format!("GotCode({:?})", code),
GotUnverifiedKey(ref _key) => {
"GotUnverifiedKey(REDACTED)".to_string()
}
Expand Down
14 changes: 8 additions & 6 deletions core/src/boss.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use api::Mood;
use events::Events;
use events::{Code, Events, Nameplate};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl BossMachine {
actions
}

fn set_code(&mut self, code: &str) -> Events {
fn set_code(&mut self, code: &Code) -> Events {
// TODO: validate code, maybe signal KeyFormatError
use self::State::*;
let (actions, newstate) = match self.state {
Expand All @@ -131,7 +131,7 @@ impl BossMachine {
// Code::SetCode will signal us with Boss:GotCode in just a
// moment, and by not special-casing set_code we get to use the
// same flow for allocate_code and input_code
Empty(i) => (events![C_SetCode(code.to_string())], Coding(i)),
Empty(i) => (events![C_SetCode(code.clone())], Coding(i)),
_ => panic!(), // TODO: signal AlreadyStartedCodeError
};
self.state = newstate;
Expand Down Expand Up @@ -167,7 +167,9 @@ impl BossMachine {
let (actions, newstate) = match self.state {
Unstarted(_) => panic!("w.start() must be called first"),
Coding(i) => (
events![I_ChooseNameplate(nameplate.to_string())],
events![
I_ChooseNameplate(Nameplate(nameplate.to_string()))
],
Coding(i),
),
_ => panic!(),
Expand All @@ -190,12 +192,12 @@ impl BossMachine {
actions
}

fn got_code(&mut self, code: &str) -> Events {
fn got_code(&mut self, code: &Code) -> Events {
use self::State::*;
let (actions, newstate) = match self.state {
Unstarted(_) => panic!("w.start() must be called first"),
Coding(i) => (
events![APIAction::GotCode(code.to_string())],
events![APIAction::GotCode(code.clone())],
Lonely(i),
),
_ => panic!(), // TODO: signal AlreadyStartedCodeError
Expand Down
24 changes: 11 additions & 13 deletions core/src/code.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use events::Events;
use events::{Events, Nameplate};
// we process these
use events::CodeEvent;
// we emit these
Expand Down Expand Up @@ -59,14 +59,15 @@ impl CodeMachine {
), // TODO: return Input object
SetCode(code) => {
// TODO: try!(validate_code(code))
let nc: Vec<&str> = code.splitn(2, "-").collect();
let nameplate = nc[0];
let code_string = code.to_string();
let nc: Vec<&str> = code_string.splitn(2, "-").collect();
let nameplate = Nameplate(nc[0].to_string());
(
Some(State::Known),
events![
N_SetNameplate(nameplate.to_string()),
B_GotCode(code.to_string()),
K_GotCode(code.to_string())
N_SetNameplate(nameplate.clone()),
B_GotCode(code.clone()),
K_GotCode(code.clone())
],
)
}
Expand Down Expand Up @@ -107,10 +108,7 @@ impl CodeMachine {
GotNameplate(..) => panic!(),
FinishedInput(code) => (
Some(State::Known),
events![
B_GotCode(code.to_string()),
K_GotCode(code.to_string())
],
events![B_GotCode(code.clone()), K_GotCode(code.clone())],
),
}
}
Expand All @@ -126,9 +124,9 @@ impl CodeMachine {
(
Some(State::Known),
events![
N_SetNameplate(nameplate.to_string()),
B_GotCode(code.to_string()),
K_GotCode(code.to_string())
N_SetNameplate(nameplate.clone()),
B_GotCode(code.clone()),
K_GotCode(code.clone())
],
)
}
Expand Down
88 changes: 63 additions & 25 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@ impl fmt::Debug for Key {
}
}

// MySide is used for the String that we send in all our outbound messages
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct MySide(pub String);
impl Deref for MySide {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}

// TheirSide is used for the String that arrives inside inbound messages
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct TheirSide(pub String);
impl Deref for TheirSide {
type Target = String;
fn deref(&self) -> &String {
&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
}
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Code(pub String);
impl Deref for Code {
type Target = String;
fn deref(&self) -> &String {
&self.0
}
}

// machines (or IO, or the API) emit these events, and each is routed to a
// specific machine (or IO or the API)
#[allow(dead_code)] // TODO: Drop dead code directive once core is complete
Expand All @@ -31,7 +69,7 @@ pub enum AllocatorEvent {
Allocate(Arc<Wordlist>),
Connected,
Lost,
RxAllocated(String),
RxAllocated(Nameplate),
}
#[allow(dead_code)] // TODO: drop dead code directive once core is complete
#[derive(PartialEq)]
Expand All @@ -40,7 +78,7 @@ pub enum BossEvent {
RxError,
Error,
Closed,
GotCode(String),
GotCode(Code),
GotKey(Key), // TODO: fixed length?
Scared,
Happy,
Expand All @@ -56,7 +94,7 @@ impl fmt::Debug for BossEvent {
RxError => "RxError".to_string(),
Error => "Error".to_string(),
Closed => "Closed".to_string(),
GotCode(ref code) => format!("GotCode({})", code),
GotCode(ref code) => format!("GotCode({:?})", code),
GotKey(ref _key) => "GotKey(REDACTED)".to_string(),
Scared => "Scared".to_string(),
Happy => "Happy".to_string(),
Expand All @@ -73,34 +111,34 @@ impl fmt::Debug for BossEvent {
pub enum CodeEvent {
AllocateCode(Arc<Wordlist>),
InputCode,
SetCode(String),
Allocated(String, String),
GotNameplate(String),
FinishedInput(String),
SetCode(Code),
Allocated(Nameplate, Code),
GotNameplate(Nameplate),
FinishedInput(Code),
}

#[derive(Debug, PartialEq)]
pub enum InputEvent {
Start,
ChooseNameplate(String),
ChooseNameplate(Nameplate),
ChooseWords(String),
GotNameplates(Vec<String>),
GotNameplates(Vec<Nameplate>),
GotWordlist(Arc<Wordlist>),
RefreshNameplates,
}

#[allow(dead_code)] // TODO: Drop dead code directive once core is complete
#[derive(PartialEq)]
pub enum KeyEvent {
GotCode(String),
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),
GotCode(ref code) => format!("GotCode({:?})", code),
GotPake(ref pake) => format!("GotPake({})", hex::encode(pake)),
};
write!(f, "KeyEvent::{}", t)
Expand All @@ -112,7 +150,7 @@ impl fmt::Debug for KeyEvent {
pub enum ListerEvent {
Connected,
Lost,
RxNameplates(Vec<String>),
RxNameplates(Vec<Nameplate>),
Refresh,
}

Expand All @@ -121,7 +159,7 @@ pub enum ListerEvent {
pub enum MailboxEvent {
Connected,
Lost,
RxMessage(String, String, Vec<u8>), // side, phase, body
RxMessage(TheirSide, String, Vec<u8>), // side, phase, body
RxClosed,
Close(Mood), // mood
GotMailbox(String), // mailbox id
Expand All @@ -136,7 +174,7 @@ impl fmt::Debug for MailboxEvent {
Connected => "Connected".to_string(),
Lost => "Lost".to_string(),
RxMessage(ref side, ref phase, ref body) => format!(
"RxMessage(side={}, phase={}, body={})",
"RxMessage(side={:?}, phase={}, body={})",
side,
phase,
maybe_utf8(body)
Expand All @@ -161,22 +199,22 @@ pub enum NameplateEvent {
Lost,
RxClaimed(String),
RxReleased,
SetNameplate(String),
SetNameplate(Nameplate),
Release,
Close,
}

#[derive(PartialEq)]
pub enum OrderEvent {
GotMessage(String, String, Vec<u8>), // side, phase, body
GotMessage(TheirSide, String, Vec<u8>), // side, phase, body
}

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={})",
"GotMessage(side={:?}, phase={}, body={})",
side,
phase,
maybe_utf8(body)
Expand All @@ -188,7 +226,7 @@ impl fmt::Debug for OrderEvent {

#[derive(PartialEq)]
pub enum ReceiveEvent {
GotMessage(String, String, Vec<u8>), // side, phase, body
GotMessage(TheirSide, String, Vec<u8>), // side, phase, body
GotKey(Key),
}

Expand All @@ -197,7 +235,7 @@ impl fmt::Debug for ReceiveEvent {
use self::ReceiveEvent::*;
let t = match *self {
GotMessage(ref side, ref phase, ref body) => format!(
"GotMessage(side={}, phase={}, body={})",
"GotMessage(side={:?}, phase={}, body={})",
side,
phase,
maybe_utf8(body)
Expand All @@ -211,13 +249,13 @@ impl fmt::Debug for ReceiveEvent {
#[derive(PartialEq)]
pub enum RendezvousEvent {
Start,
TxBind(String, String), // appid, side
TxBind(String, MySide), // appid, side
TxOpen(String), // mailbox
TxAdd(String, Vec<u8>), // phase, body
TxClose(String, Mood), // mailbox, mood
Stop,
TxClaim(String), // nameplate
TxRelease(String), // nameplate
TxClaim(Nameplate), // nameplate
TxRelease(Nameplate), // nameplate
TxAllocate,
TxList,
}
Expand All @@ -228,7 +266,7 @@ impl fmt::Debug for RendezvousEvent {
let t = match *self {
Start => "Start".to_string(),
TxBind(ref appid, ref side) => {
format!("TxBind(appid={}, side={})", appid, side)
format!("TxBind(appid={}, side={:?})", appid, side)
}
TxOpen(ref mailbox) => format!("TxOpen({})", mailbox),
TxAdd(ref phase, ref body) => {
Expand All @@ -238,8 +276,8 @@ impl fmt::Debug for RendezvousEvent {
format!("TxClose({}, {:?})", mailbox, mood)
}
Stop => "Stop".to_string(),
TxClaim(ref nameplate) => format!("TxClaim({})", nameplate),
TxRelease(ref nameplate) => format!("TxRelease({})", nameplate),
TxClaim(ref nameplate) => format!("TxClaim({:?})", nameplate),
TxRelease(ref nameplate) => format!("TxRelease({:?})", nameplate),
TxAllocate => "TxAllocate".to_string(),
TxList => "TxList".to_string(),
};
Expand Down
Loading

0 comments on commit 26dcb19

Please sign in to comment.