Skip to content

Commit

Permalink
use Mood as a enum in more places, rather than a String
Browse files Browse the repository at this point in the history
* added Unwelcome as a mood
* stringify moods uniformly
  • Loading branch information
warner committed May 26, 2018
1 parent 1af2281 commit 46d31b8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 63 deletions.
21 changes: 15 additions & 6 deletions core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,28 @@ pub enum Mood {
Lonely,
Error,
Scared,
Unwelcome,
}

impl fmt::Display for Mood {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl Mood {
fn to_string(&self) -> String {
// this is used for protocol messages as well as debug output
match *self {
Mood::Happy => write!(f, "happy"),
Mood::Lonely => write!(f, "lonely"),
Mood::Error => write!(f, "error"),
Mood::Scared => write!(f, "scared"),
Mood::Happy => "happy".to_string(),
Mood::Lonely => "lonely".to_string(),
Mood::Error => "error".to_string(),
Mood::Scared => "scared".to_string(),
Mood::Unwelcome => "unwelcome".to_string(),
}
}
}

impl fmt::Display for Mood {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}

#[derive(PartialEq)]
pub enum APIAction {
// from WormholeCore out through IO glue to application
Expand Down
4 changes: 2 additions & 2 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub enum MailboxEvent {
Lost,
RxMessage(String, String, Vec<u8>), // side, phase, body
RxClosed,
Close(String), // mood
Close(Mood), // mood
GotMailbox(String), // mailbox id
GotMessage,
AddMessage(String, Vec<u8>), // PAKE+VERSION from Key, PHASE from Send
Expand All @@ -127,7 +127,7 @@ impl fmt::Debug for MailboxEvent {
maybe_utf8(body)
),
RxClosed => "RxClosed".to_string(),
Close(ref mood) => format!("Close({})", mood),
Close(ref mood) => format!("Close({:?})", mood),
GotMailbox(ref mailbox) => format!("GotMailbox({})", mailbox),
GotMessage => "GotMessage".to_string(),
AddMessage(ref phase, ref body) => {
Expand Down
43 changes: 13 additions & 30 deletions core/src/mailbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::collections::HashSet;

use api::Mood;
use events::Events;
// we process these
use events::MailboxEvent;
Expand All @@ -22,8 +23,8 @@ enum State {
S2A(String),
S2B(String), // opened
// S3: closing
S3A(String, String), // mailbox, mood
S3B(String, String), // mailbox, mood
S3A(String, Mood), // mailbox, mood
S3B(String, Mood), // mailbox, mood
// S4: closed
S4A,
S4B,
Expand Down Expand Up @@ -68,8 +69,8 @@ impl Mailbox {
S1A(ref mailbox) => self.do_s1a(&mailbox, event),
S2A(ref mailbox) => self.do_s2a(&mailbox, event),
S2B(ref mailbox) => self.do_s2b(&mailbox, event),
S3A(ref mailbox, ref mood) => self.do_s3a(&mailbox, &mood, event),
S3B(ref mailbox, ref mood) => self.do_s3b(&mailbox, &mood, event),
S3A(ref mailbox, mood) => self.do_s3a(&mailbox, mood, event),
S3B(ref mailbox, mood) => self.do_s3b(&mailbox, mood, event),
S4A => self.do_s4a(event),
S4B => self.do_s4b(event),
};
Expand Down Expand Up @@ -300,10 +301,7 @@ impl Mailbox {
}
RxClosed => panic!(),
Close(mood) => (
Some(State::S3B(
mailbox.to_string(),
mood.to_string(),
)),
Some(State::S3B(mailbox.to_string(), mood)),
events![RC_TxClose],
QueueCtrl::NoAction,
),
Expand All @@ -326,17 +324,14 @@ impl Mailbox {
fn do_s3a(
&self,
mailbox: &str,
mood: &str,
mood: Mood,
event: MailboxEvent,
) -> (Option<State>, Events, QueueCtrl) {
use events::MailboxEvent::*;

match event {
Connected => (
Some(State::S3B(
mailbox.to_string(),
mood.to_string(),
)),
Some(State::S3B(mailbox.to_string(), mood)),
events![RC_TxClose],
QueueCtrl::NoAction,
),
Expand All @@ -353,28 +348,22 @@ impl Mailbox {
fn do_s3b(
&self,
mailbox: &str,
mood: &str,
mood: Mood,
event: MailboxEvent,
) -> (Option<State>, Events, QueueCtrl) {
use events::MailboxEvent::*;

match event {
Connected => panic!(),
Lost => (
Some(State::S3A(
mailbox.to_string(),
mood.to_string(),
)),
Some(State::S3A(mailbox.to_string(), mood)),
events![],
QueueCtrl::NoAction,
),
RxMessage(_side, _phase, _body) => {
// irrespective of the side, enter into S3B, do nothing, generate no events
(
Some(State::S3B(
mailbox.to_string(),
mood.to_string(),
)),
Some(State::S3B(mailbox.to_string(), mood)),
events![],
QueueCtrl::NoAction,
)
Expand All @@ -385,20 +374,14 @@ impl Mailbox {
QueueCtrl::NoAction,
),
Close(mood) => (
Some(State::S3B(
mailbox.to_string(),
mood.to_string(),
)),
Some(State::S3B(mailbox.to_string(), mood)),
events![],
QueueCtrl::NoAction,
),
GotMailbox(_) => panic!(),
GotMessage => panic!(),
AddMessage(_, _) => (
Some(State::S3B(
mailbox.to_string(),
mood.to_string(),
)),
Some(State::S3B(mailbox.to_string(), mood)),
events![],
QueueCtrl::NoAction,
),
Expand Down
6 changes: 4 additions & 2 deletions core/src/server_messages.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde_json;

use api::Mood;
use serde::{Deserialize, Deserializer};
use util;

Expand Down Expand Up @@ -154,7 +155,7 @@ pub fn add(phase: &str, body: &[u8]) -> Message {
}

#[allow(dead_code)]
pub fn close(mailbox: &str, mood: &str) -> Message {
pub fn close(mailbox: &str, mood: Mood) -> Message {
Message::Close {
mailbox: mailbox.to_string(),
mood: mood.to_string(),
Expand Down Expand Up @@ -186,6 +187,7 @@ pub fn deserialize(s: &str) -> Message {
#[cfg(test)]
mod test {
use super::*;
use api::Mood;

#[test]
fn test_bind() {
Expand Down Expand Up @@ -245,7 +247,7 @@ mod test {

#[test]
fn test_close() {
let m1 = close("mailbox1", "mood");
let m1 = close("mailbox1", Mood::Happy);
let s = serde_json::to_string(&m1).unwrap();
let m2 = deserialize(&s);
assert_eq!(m1, m2);
Expand Down
33 changes: 10 additions & 23 deletions core/src/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ impl Terminator {
match event {
MailboxDone => (State::Sno, events![]),
NameplateDone => (State::Smo, events![]),
Close(mood) => (
State::Snm,
events![N_Close, M_Close(mood.to_string())],
),
Close(mood) => (State::Snm, events![N_Close, M_Close(mood)]),
Stopped => panic!(
"Got stopped to early. Nameplate and Mailbox are still active"
),
Expand All @@ -69,21 +66,15 @@ impl Terminator {
fn in_nameplate_open(&self, event: TerminatorEvent) -> (State, Events) {
match event {
NameplateDone => (State::S0o, events![]),
Close(mood) => (
State::Sn,
events![N_Close, M_Close(mood.to_string())],
),
Close(mood) => (State::Sn, events![N_Close, M_Close(mood)]),
_ => panic!("Got {:?} too early, Nameplate still active"),
}
}

fn in_mailbox_open(&self, event: TerminatorEvent) -> (State, Events) {
match event {
MailboxDone => (State::S0o, events![]),
Close(mood) => (
State::Sm,
events![N_Close, M_Close(mood.to_string())],
),
Close(mood) => (State::Sm, events![N_Close, M_Close(mood)]),
_ => panic!("Got {:?} too early, Mailbox still active"),
}
}
Expand All @@ -92,7 +83,7 @@ impl Terminator {
match event {
Close(mood) => (
State::SStopping,
events![N_Close, M_Close(mood.to_string()), RC_Stop],
events![N_Close, M_Close(mood), RC_Stop],
),
MailboxDone | NameplateDone => panic!("Got {:?} too late", event),
_ => panic!("Too early to stop"),
Expand Down Expand Up @@ -152,7 +143,7 @@ mod test {

assert_eq!(
terminator.process(Close(Happy)),
events![N_Close, M_Close(String::from("happy"))]
events![N_Close, M_Close(Happy)]
);
assert_eq!(terminator.state, State::Sn);

Expand All @@ -172,7 +163,7 @@ mod test {

assert_eq!(
terminator.process(Close(Happy)),
events![N_Close, M_Close(String::from("happy"))]
events![N_Close, M_Close(Happy)]
);

assert_eq!(terminator.process(NameplateDone), events![]);
Expand All @@ -192,7 +183,7 @@ mod test {
assert_eq!(terminator.process(NameplateDone), events![]);
assert_eq!(
terminator.process(Close(Happy)),
events![N_Close, M_Close(String::from("happy"))]
events![N_Close, M_Close(Happy)]
);
assert_eq!(
terminator.process(MailboxDone),
Expand All @@ -208,7 +199,7 @@ mod test {

assert_eq!(
terminator.process(Close(Lonely)),
events![N_Close, M_Close(String::from("lonely"))]
events![N_Close, M_Close(Lonely)]
);

assert_eq!(terminator.process(MailboxDone), events![]);
Expand All @@ -230,11 +221,7 @@ mod test {
assert_eq!(terminator.state, State::S0o);
assert_eq!(
terminator.process(Close(Scared)),
events![
N_Close,
M_Close(String::from("scared")),
RC_Stop
]
events![N_Close, M_Close(Scared), RC_Stop]
);
assert_eq!(terminator.state, State::SStopping);
assert_eq!(terminator.process(Stopped), events![B_Closed]);
Expand All @@ -251,7 +238,7 @@ mod test {

assert_eq!(
terminator.process(Close(Error)),
events![N_Close, M_Close(String::from("error")), RC_Stop]
events![N_Close, M_Close(Error), RC_Stop]
);
assert_eq!(terminator.process(Stopped), events![B_Closed]);
assert_eq!(terminator.state, State::SStopped);
Expand Down

0 comments on commit 46d31b8

Please sign in to comment.