From 858ca189400c7da11704a340b712255c4eb4f9a9 Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux <xavier.lheureux@icloud.com> Date: Wed, 3 Apr 2019 22:33:40 -0400 Subject: [PATCH] Reduce nesting and return None on empty statement --- src/lib/parser/statement/splitter.rs | 59 +++++++++++++--------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/lib/parser/statement/splitter.rs b/src/lib/parser/statement/splitter.rs index a3a9d946..713f8e8d 100644 --- a/src/lib/parser/statement/splitter.rs +++ b/src/lib/parser/statement/splitter.rs @@ -272,39 +272,36 @@ impl<'a> Iterator for StatementSplitter<'a> { } self.read = self.data.len(); - match error { - Some(error) => Some(Err(error)), - None => { - if self.paren_level != 0 { - Some(Err(StatementError::UnterminatedSubshell)) - } else if self.method { - Some(Err(StatementError::UnterminatedMethod)) - } else if self.vbrace { - Some(Err(StatementError::UnterminatedBracedVar)) - } else if self.brace_level != 0 { - Some(Err(StatementError::UnterminatedBrace)) - } else if self.math_expr { - Some(Err(StatementError::UnterminatedArithmetic)) - } else { - let output = self.data[start..].trim(); - match output.as_bytes().get(0) { - Some(b'>') | Some(b'<') | Some(b'^') => { - Some(Err(StatementError::ExpectedCommandButFound("redirection"))) - } - Some(b'|') => Some(Err(StatementError::ExpectedCommandButFound("pipe"))), - Some(b'&') => Some(Err(StatementError::ExpectedCommandButFound("&"))), - Some(b'*') | Some(b'%') | Some(b'?') | Some(b'{') | Some(b'}') => { - Some(Err(StatementError::IllegalCommandName(String::from(output)))) - } - _ => { - let stmt = self.get_statement_from(output); - self.logical = LogicalOp::None; - Some(Ok(stmt)) - } + error.map(Err).or_else(|| { + if self.paren_level != 0 { + Some(Err(StatementError::UnterminatedSubshell)) + } else if self.method { + Some(Err(StatementError::UnterminatedMethod)) + } else if self.vbrace { + Some(Err(StatementError::UnterminatedBracedVar)) + } else if self.brace_level != 0 { + Some(Err(StatementError::UnterminatedBrace)) + } else if self.math_expr { + Some(Err(StatementError::UnterminatedArithmetic)) + } else { + let output = self.data[start..].trim(); + output.as_bytes().get(0).map(|c| match c { + b'>' | b'<' | b'^' => { + Err(StatementError::ExpectedCommandButFound("redirection")) } - } + b'|' => Err(StatementError::ExpectedCommandButFound("pipe")), + b'&' => Err(StatementError::ExpectedCommandButFound("&")), + b'*' | b'%' | b'?' | b'{' | b'}' => { + Err(StatementError::IllegalCommandName(String::from(output))) + } + _ => { + let stmt = self.get_statement_from(output); + self.logical = LogicalOp::None; + Ok(stmt) + } + }) } - } + }) } } -- GitLab