From f57b85f8961558c8b9f27f63d41b9e59c7d56b40 Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux <xavier.lheureux@icloud.com> Date: Wed, 3 Apr 2019 22:12:38 -0400 Subject: [PATCH] Chain ifs rather than using a match & merge matches --- src/lib/parser/statement/splitter.rs | 47 ++++++++++++++++------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/lib/parser/statement/splitter.rs b/src/lib/parser/statement/splitter.rs index 5ca6a628..a3a9d946 100644 --- a/src/lib/parser/statement/splitter.rs +++ b/src/lib/parser/statement/splitter.rs @@ -113,7 +113,7 @@ impl<'a> StatementSplitter<'a> { } } - fn get_statement_from(&mut self, input: &'a str) -> StatementVariant<'a> { + fn get_statement_from(&self, input: &'a str) -> StatementVariant<'a> { if self.logical == LogicalOp::And { StatementVariant::And(input) } else if self.logical == LogicalOp::Or { @@ -274,26 +274,33 @@ 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)), - None if self.method => Some(Err(StatementError::UnterminatedMethod)), - None if self.vbrace => Some(Err(StatementError::UnterminatedBracedVar)), - None if self.brace_level != 0 => Some(Err(StatementError::UnterminatedBrace)), - None if self.math_expr => Some(Err(StatementError::UnterminatedArithmetic)), None => { - 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)) + 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)) + } } } } -- GitLab