Skip to content

Commit

Permalink
Merge pull request #8 from ihrwein/remove-warnings
Browse files Browse the repository at this point in the history
Stop using deprecated types and get rid of all the other warnings
  • Loading branch information
ihrwein committed Jan 29, 2016
2 parents 7eecba0 + d72f1aa commit fa66aea
Show file tree
Hide file tree
Showing 26 changed files with 318 additions and 179 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ handlebars = "0.10"
rustc-serialize = "0.3"
log = "0.3"
env_logger = "0.3.1"

num = "0.1"
14 changes: 9 additions & 5 deletions src/action/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ pub struct MessageAction {
name: Option<String>,
values: Handlebars,
when: ExecCondition,
inject_mode: InjectMode
inject_mode: InjectMode,
}

impl MessageAction {
pub fn new(sender: Box<ResponseSender<Response>>,
action: config::action::MessageAction)
-> MessageAction {
let config::action::MessageAction { uuid, name, message, values, when, inject_mode } = action;
let config::action::MessageAction { uuid, name, message, values, when, inject_mode } =
action;
let mut handlebars = Handlebars::new();
for (name, template) in values.into_iter() {
handlebars.register_template(&name, template);
Expand All @@ -51,7 +52,7 @@ impl MessageAction {
name: name,
values: handlebars,
when: when,
inject_mode: inject_mode
inject_mode: inject_mode,
}
}

Expand Down Expand Up @@ -108,7 +109,10 @@ impl MessageAction {
fn execute(&self, _state: &State, _context: &BaseContext) {
match self.render_message(_state, _context) {
Ok(message) => {
let response = Alert { message: message, inject_mode: self.inject_mode.clone() };
let response = Alert {
message: message,
inject_mode: self.inject_mode.clone(),
};
self.sender.send_response(Response::Alert(response));
}
Err(error) => {
Expand All @@ -121,7 +125,7 @@ impl MessageAction {
#[derive(Debug)]
pub struct Alert {
message: Message,
inject_mode: InjectMode
inject_mode: InjectMode,
}

impl Alert {
Expand Down
32 changes: 18 additions & 14 deletions src/action/message/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use state::State;
use env_logger;
use handlebars::Template;
use std::cell::RefCell;
use std::time::Duration;
use std::rc::Rc;
use uuid::Uuid;

Expand All @@ -29,12 +30,19 @@ impl ResponseSender<Response> for DummyResponseSender {
}
}

#[test]
fn test_given_dummy_response_handler_can_be_cloned() {
let responses = Rc::new(RefCell::new(Vec::new()));
let response_sender = DummyResponseSender { responses: responses.clone() };
let _ = response_sender.boxed_clone();
}

#[test]
fn test_given_a_message_action_when_it_is_executed_then_it_adds_the_name_and_uuid_of_the_context_to_the_message
() {
let name = Some("name".to_string());
let base_context = {
let conditions = ConditionsBuilder::new(100).build();
let conditions = ConditionsBuilder::new(Duration::from_millis(100)).build();
let uuid = Uuid::new_v4();
BaseContextBuilder::new(uuid, conditions).name(name.clone()).build()
};
Expand All @@ -47,8 +55,7 @@ fn test_given_a_message_action_when_it_is_executed_then_it_adds_the_name_and_uui
.expect("Failed to compile a handlebars template");
let config_action = config::action::message::MessageActionBuilder::new("uuid", message)
.build();
MessageAction::new(Box::new(response_sender),
config_action)
MessageAction::new(Box::new(response_sender), config_action)
};

message_action.execute(&state, &base_context);
Expand All @@ -71,19 +78,17 @@ fn test_given_message_action_when_it_is_executed_then_it_uses_the_messages_to_re
let _ = env_logger::init();
let name = Some("name".to_string());
let base_context = {
let conditions = ConditionsBuilder::new(100).build();
let conditions = ConditionsBuilder::new(Duration::from_millis(100)).build();
let uuid = Uuid::new_v4();
BaseContextBuilder::new(uuid, conditions).name(name.clone()).build()
};
let state = {
let messages = vec![
Rc::new(MessageBuilder::new("uuid1", "message1")
.pair("key1", "value1")
.build()),
Rc::new(MessageBuilder::new("uuid2", "message2")
.pair("key2", "value2")
.build())
];
let messages = vec![Rc::new(MessageBuilder::new("uuid1", "message1")
.pair("key1", "value1")
.build()),
Rc::new(MessageBuilder::new("uuid2", "message2")
.pair("key2", "value2")
.build())];
State::with_messages(messages)
};
let responses = Rc::new(RefCell::new(Vec::new()));
Expand All @@ -101,8 +106,7 @@ fn test_given_message_action_when_it_is_executed_then_it_uses_the_messages_to_re
.ok()
.expect("Failed to compile a handlebars template"))
.build();
MessageAction::new(Box::new(response_sender),
config_action)
MessageAction::new(Box::new(response_sender), config_action)
};

message_action.execute(&state, &base_context);
Expand Down
4 changes: 1 addition & 3 deletions src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ pub trait Action {
fn on_closed(&self, state: &State, context: &BaseContext);
}

pub fn from_config(config: ActionType,
_sender: Box<ResponseSender<Response>>)
-> Box<Action> {
pub fn from_config(config: ActionType, _sender: Box<ResponseSender<Response>>) -> Box<Action> {
match config {
ActionType::Message(action) => Box::new(self::message::MessageAction::new(_sender, action)),
}
Expand Down
18 changes: 10 additions & 8 deletions src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ extern crate uuid;

use correlation::{config, Correlator};
use correlation::conditions::ConditionsBuilder;
use correlation::message::{MessageBuilder};
use correlation::message::MessageBuilder;
use uuid::Uuid;
use std::thread;
use std::time::Duration;

#[allow(dead_code)]
fn main() {
Expand All @@ -19,21 +20,22 @@ fn main() {
uuid2.clone(),
uuid3.clone(),
];
let condition = ConditionsBuilder::new(100).patterns(patterns)
.first_opens(true)
.last_closes(true)
.build();
let actions = vec![ ];
let condition = ConditionsBuilder::new(Duration::from_millis(100))
.patterns(patterns)
.first_opens(true)
.last_closes(true)
.build();
let actions = vec![];
let contexts = vec![
config::ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(actions.clone()).build(),
config::ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(actions.clone()).build(),
config::ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(actions.clone()).build(),
];
let mut correlator = Correlator::new(contexts);
let _ = correlator.push_message(MessageBuilder::new(&uuid1, "message").build());
thread::sleep_ms(20);
thread::sleep(Duration::from_millis(20));
let _ = correlator.push_message(MessageBuilder::new(&uuid2, "message").build());
thread::sleep_ms(80);
thread::sleep(Duration::from_millis(80));
let _ = correlator.push_message(MessageBuilder::new(&uuid3, "message").build());
let _ = correlator.stop();
}
68 changes: 35 additions & 33 deletions src/conditions.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use message::Message;
use MiliSec;
use state::State;
use std::time::Duration;

const FIRST_OPENS_DEFAULT: bool = false;
const LAST_CLOSES_DEFAULT: bool = false;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Conditions {
pub timeout: MiliSec,
pub renew_timeout: Option<MiliSec>,
pub timeout: Duration,
pub renew_timeout: Option<Duration>,
pub first_opens: bool,
pub last_closes: bool,
pub max_size: Option<usize>,
pub patterns: Vec<String>,
}

impl Conditions {
fn new(timeout: MiliSec) -> Conditions {
fn new(timeout: Duration) -> Conditions {
Conditions {
timeout: timeout,
renew_timeout: None,
Expand All @@ -41,8 +41,7 @@ impl Conditions {
}

fn is_closing_condition_met(&self, state: &State) -> bool {
self.is_max_size_reached(state) ||
self.is_closing_message(state) ||
self.is_max_size_reached(state) || self.is_closing_message(state) ||
self.is_any_timer_expired(state)
}

Expand All @@ -68,10 +67,9 @@ impl Conditions {
}

fn is_renew_timeout_expired(&self, state: &State) -> bool {
self.renew_timeout.map_or(false,
|renew_timeout| {
state.elapsed_time_since_last_message() >= renew_timeout
})
self.renew_timeout.map_or(false, |renew_timeout| {
state.elapsed_time_since_last_message() >= renew_timeout
})
}
}

Expand All @@ -80,11 +78,11 @@ pub struct ConditionsBuilder {
}

impl ConditionsBuilder {
pub fn new(timeout: MiliSec) -> ConditionsBuilder {
pub fn new(timeout: Duration) -> ConditionsBuilder {
ConditionsBuilder { conditions: Conditions::new(timeout) }
}

pub fn renew_timeout(&mut self, timeout: MiliSec) -> &mut ConditionsBuilder {
pub fn renew_timeout(&mut self, timeout: Duration) -> &mut ConditionsBuilder {
self.conditions.renew_timeout = Some(timeout);
self
}
Expand Down Expand Up @@ -124,10 +122,11 @@ mod test {
use super::ConditionsBuilder;
use context::BaseContextBuilder;
use uuid::Uuid;
use std::time::Duration;

#[test]
fn test_given_condition_when_an_opening_message_is_received_then_the_state_becomes_opened() {
let timeout = 100;
let timeout = Duration::from_millis(100);
let msg_id1 = "11eaf6f8-0640-460f-aee2-a72d2f2ab258".to_string();
let msg_id2 = "21eaf6f8-0640-460f-aee2-a72d2f2ab258".to_string();
let patterns = vec![
Expand All @@ -145,7 +144,7 @@ mod test {

#[test]
fn test_given_condition_when_a_closing_message_is_received_then_the_state_becomes_closed() {
let timeout = 100;
let timeout = Duration::from_millis(100);
let msg_id1 = "11eaf6f8-0640-460f-aee2-a72d2f2ab258".to_string();
let msg_id2 = "21eaf6f8-0640-460f-aee2-a72d2f2ab258".to_string();
let patterns = vec![
Expand All @@ -154,9 +153,9 @@ mod test {
];
let mut state = State::new();
let conditions = ConditionsBuilder::new(timeout)
.patterns(patterns)
.last_closes(true)
.build();
.patterns(patterns)
.last_closes(true)
.build();
let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build();
let msg_opening = Rc::new(MessageBuilder::new(&msg_id1, "message").build());
let msg_closing = Rc::new(MessageBuilder::new(&msg_id2, "message").build());
Expand All @@ -180,7 +179,7 @@ mod test {
println!("{:?}", &conditions);
let conditions: Conditions = conditions.ok().expect("Failed to deserialize a Conditions \
struct with only a timeout field");
assert_eq!(conditions.timeout, 100);
assert_eq!(conditions.timeout, Duration::from_millis(100));
}

#[test]
Expand Down Expand Up @@ -209,8 +208,8 @@ mod test {
println!("{:?}", &conditions);
let conditions: Conditions = conditions.ok()
.expect("Failed to deserialize a Conditions struct");
assert_eq!(conditions.timeout, 100);
assert_eq!(conditions.renew_timeout, Some(50));
assert_eq!(conditions.timeout, Duration::from_millis(100));
assert_eq!(conditions.renew_timeout, Some(Duration::from_millis(50)));
assert_eq!(conditions.first_opens, true);
assert_eq!(conditions.last_closes, false);
assert_eq!(conditions.max_size, Some(42));
Expand All @@ -219,7 +218,7 @@ mod test {

#[test]
fn test_given_condition_when_there_are_no_patterns_then_any_message_can_open_the_context() {
let timeout = 100;
let timeout = Duration::from_millis(100);
let msg_id = "11eaf6f8-0640-460f-aee2-a72d2f2ab258".to_string();
let condition = ConditionsBuilder::new(timeout).build();
let msg = MessageBuilder::new(&msg_id, "message").build();
Expand All @@ -228,8 +227,8 @@ mod test {

#[test]
fn test_given_condition_when_first_opens_is_set_then_the_right_message_can_open_the_context
() {
let timeout = 100;
() {
let timeout = Duration::from_millis(100);
let patterns = vec![
"p1".to_string(),
"p2".to_string(),
Expand All @@ -248,18 +247,18 @@ mod test {
#[test]
fn test_given_conditions_when_last_closes_is_set_and_the_message_has_a_name_then_we_check_that_name
() {
let timeout = 100;
let timeout = Duration::from_millis(100);
let patterns = vec!["p1".to_string(), "p2".to_string()];
let p1_uuid = "e4f3f8b2-3135-4916-a5ea-621a754dab0d".to_string();
let p2_uuid = "f4f3f8b2-3135-4916-a5ea-621a754dab0d".to_string();
let p1 = "p1".to_string();
let p2 = "p2".to_string();
let mut state = State::new();
let conditions = ConditionsBuilder::new(timeout)
.patterns(patterns)
.first_opens(true)
.last_closes(true)
.build();
.patterns(patterns)
.first_opens(true)
.last_closes(true)
.build();
let p1_msg = MessageBuilder::new(&p1_uuid, "message").name(Some(&p1)).build();
let p2_msg = MessageBuilder::new(&p2_uuid, "message").name(Some(&p2)).build();
let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build();
Expand All @@ -271,9 +270,10 @@ mod test {
}

mod deser {
use MiliSec;
use super::{Conditions, FIRST_OPENS_DEFAULT, LAST_CLOSES_DEFAULT};
use serde::de::{Deserialize, Deserializer, Error, MapVisitor, Visitor};
use std::time::Duration;
use duration::SerializableDuration;

impl Deserialize for Conditions {
fn deserialize<D>(deserializer: &mut D) -> Result<Conditions, D::Error>
Expand Down Expand Up @@ -328,8 +328,8 @@ mod deser {
fn visit_map<V>(&mut self, mut visitor: V) -> Result<Conditions, V::Error>
where V: MapVisitor
{
let mut timeout: Option<MiliSec> = None;
let mut renew_timeout = None;
let mut timeout: Option<SerializableDuration> = None;
let mut renew_timeout: Option<SerializableDuration> = None;
let mut first_opens = FIRST_OPENS_DEFAULT;
let mut last_closes = LAST_CLOSES_DEFAULT;
let mut max_size = None;
Expand All @@ -346,11 +346,13 @@ mod deser {
}
}

let timeout: MiliSec = match timeout {
Some(timeout) => timeout,
let timeout: Duration = match timeout {
Some(timeout) => timeout.0,
None => return visitor.missing_field("timeout"),
};

let renew_timeout = renew_timeout.map(|timeout| timeout.0);

try!(visitor.end());

Ok(Conditions {
Expand Down
Loading

0 comments on commit fa66aea

Please sign in to comment.