diff --git a/src/config.rs b/src/config.rs index 19f9cc2..ba39bc5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -113,28 +113,29 @@ impl Iterator for SExpIterator { fn next(&mut self) -> Option { let mut data = vec![]; loop { - match (self.elements.pop_front(), self.elements.pop_front()) { - (Some(Sp(kw_l, Expr::Keyword(kw), kw_r)), Some(value)) => { + let first_is_kw = self.elements.front().map_or(false, |x| x.1.is_keyword()); + if first_is_kw { + let (l, kw, r) = match self.elements.pop_front() { + Some(Sp(l, Expr::Keyword(kw), r)) => (l, kw, r), + _ => unreachable!(), + }; + if let Some(value) = self.elements.pop_front() { data.push((kw, value)); + } else { + return if data.is_empty() { + Some(ExpressionElement::Single(Sp(l, Expr::Keyword(kw), r))) + } else { + Some(ExpressionElement::KeyValue(data)) + }; } - (Some(x), Some(y)) => { - self.elements.push_front(y); - self.elements.push_front(x); - break; - } - (Some(x), None) => { - self.elements.push_front(x); - break; - } - (None, None) => break, - (None, Some(_)) => unreachable!(), + } else { + return if data.is_empty() { + Some(ExpressionElement::Single(self.elements.pop_front()?)) + } else { + Some(ExpressionElement::KeyValue(data)) + }; } } - if data.is_empty() { - Some(ExpressionElement::Single(self.elements.pop_front()?)) - } else { - Some(ExpressionElement::KeyValue(data)) - } } } diff --git a/src/main.rs b/src/main.rs index e9ea411..aa5b2b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,12 @@ impl Expr { _ => Err(WrongExprType), } } + fn is_keyword(&self) -> bool { + match self { + Expr::Keyword(_) => true, + _ => false, + } + } } impl std::fmt::Display for Expr {