Skip to content

Commit

Permalink
Merge pull request #6 from ihrwein/conditions-refact
Browse files Browse the repository at this point in the history
Refactor Conditions
  • Loading branch information
ihrwein committed Jan 28, 2016
2 parents cb61968 + 9d4367d commit 6548b3a
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions src/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const LAST_CLOSES_DEFAULT: bool = false;
pub struct Conditions {
pub timeout: MiliSec,
pub renew_timeout: Option<MiliSec>,
pub first_opens: Option<bool>,
pub last_closes: Option<bool>,
pub first_opens: bool,
pub last_closes: bool,
pub max_size: Option<usize>,
pub patterns: Vec<String>,
}
Expand All @@ -20,15 +20,15 @@ impl Conditions {
Conditions {
timeout: timeout,
renew_timeout: None,
first_opens: None,
last_closes: None,
first_opens: FIRST_OPENS_DEFAULT,
last_closes: LAST_CLOSES_DEFAULT,
max_size: None,
patterns: Vec::new(),
}
}

pub fn is_opening(&self, message: &Message) -> bool {
if self.first_opens.unwrap_or(FIRST_OPENS_DEFAULT) {
if self.first_opens {
message.ids().any(|x| x == self.patterns.first().unwrap())
} else {
true
Expand All @@ -51,7 +51,7 @@ impl Conditions {
}

fn is_closing_message(&self, state: &State) -> bool {
if self.last_closes.unwrap_or(LAST_CLOSES_DEFAULT) {
if self.last_closes {
let last_message = state.messages().last().unwrap();
last_message.ids().any(|x| x == self.patterns.last().unwrap())
} else {
Expand Down Expand Up @@ -90,12 +90,12 @@ impl ConditionsBuilder {
}

pub fn first_opens(&mut self, first_opens: bool) -> &mut ConditionsBuilder {
self.conditions.first_opens = Some(first_opens);
self.conditions.first_opens = first_opens;
self
}

pub fn last_closes(&mut self, last_closes: bool) -> &mut ConditionsBuilder {
self.conditions.last_closes = Some(last_closes);
self.conditions.last_closes = last_closes;
self
}
pub fn max_size(&mut self, max_size: usize) -> &mut ConditionsBuilder {
Expand Down Expand Up @@ -211,8 +211,8 @@ mod test {
.expect("Failed to deserialize a Conditions struct");
assert_eq!(conditions.timeout, 100);
assert_eq!(conditions.renew_timeout, Some(50));
assert_eq!(conditions.first_opens, Some(true));
assert_eq!(conditions.last_closes, Some(false));
assert_eq!(conditions.first_opens, true);
assert_eq!(conditions.last_closes, false);
assert_eq!(conditions.max_size, Some(42));
assert_eq!(conditions.patterns, expected_patterns);
}
Expand Down Expand Up @@ -272,7 +272,7 @@ mod test {

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

impl Deserialize for Conditions {
Expand Down Expand Up @@ -330,34 +330,19 @@ mod deser {
{
let mut timeout: Option<MiliSec> = None;
let mut renew_timeout = None;
let mut first_opens = None;
let mut last_closes = None;
let mut first_opens = FIRST_OPENS_DEFAULT;
let mut last_closes = LAST_CLOSES_DEFAULT;
let mut max_size = None;
let mut patterns = None;

loop {
match try!(visitor.visit_key()) {
Some(Field::Timeout) => {
timeout = Some(try!(visitor.visit_value()));
}
Some(Field::RenewTimeout) => {
renew_timeout = Some(try!(visitor.visit_value()));
}
Some(Field::FirstOpens) => {
first_opens = Some(try!(visitor.visit_value()));
}
Some(Field::LastCloses) => {
last_closes = Some(try!(visitor.visit_value()));
}
Some(Field::MaxSize) => {
max_size = Some(try!(visitor.visit_value()));
}
Some(Field::Patterns) => {
patterns = Some(try!(visitor.visit_value()));
}
None => {
break;
}
while let Some(field) = try!(visitor.visit_key()) {
match field {
Field::Timeout => timeout = Some(try!(visitor.visit_value())),
Field::RenewTimeout => renew_timeout = Some(try!(visitor.visit_value())),
Field::FirstOpens => first_opens = try!(visitor.visit_value()),
Field::LastCloses => last_closes = try!(visitor.visit_value()),
Field::MaxSize => max_size = Some(try!(visitor.visit_value())),
Field::Patterns => patterns = Some(try!(visitor.visit_value())),
}
}

Expand Down

0 comments on commit 6548b3a

Please sign in to comment.