diff --git a/src/conditions.rs b/src/conditions.rs index 0123123..0f56f3a 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -9,8 +9,8 @@ const LAST_CLOSES_DEFAULT: bool = false; pub struct Conditions { pub timeout: MiliSec, pub renew_timeout: Option, - pub first_opens: Option, - pub last_closes: Option, + pub first_opens: bool, + pub last_closes: bool, pub max_size: Option, pub patterns: Vec, } @@ -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 @@ -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 { @@ -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 { @@ -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); } @@ -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 { @@ -330,34 +330,19 @@ mod deser { { let mut timeout: Option = 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())), } }