1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2026-01-15 09:41:07 +00:00

expr: clean up returning Err

This commit is contained in:
Jeong YunWon 2021-06-11 14:23:24 +09:00
parent f01121f5b7
commit bbae78db67

View file

@ -160,10 +160,8 @@ impl AstNode {
if let AstNode::Node { operands, .. } = self {
let mut out = Vec::with_capacity(operands.len());
for operand in operands {
match operand.evaluate() {
Ok(value) => out.push(value),
Err(reason) => return Err(reason),
}
let value = operand.evaluate()?;
out.push(value);
}
Ok(out)
} else {
@ -252,10 +250,8 @@ fn maybe_ast_node(
) -> Result<Box<AstNode>, String> {
let mut operands = Vec::with_capacity(arity);
for _ in 0..arity {
match ast_from_rpn(rpn) {
Err(reason) => return Err(reason),
Ok(operand) => operands.push(operand),
}
let operand = ast_from_rpn(rpn)?;
operands.push(operand);
}
operands.reverse();
Ok(AstNode::new_node(token_idx, op_type, operands))
@ -399,10 +395,12 @@ fn move_till_match_paren(
op_stack: &mut TokenStack,
) -> Result<(), String> {
loop {
match op_stack.pop() {
None => return Err("syntax error (Mismatched close-parenthesis)".to_string()),
Some((_, Token::ParOpen)) => return Ok(()),
Some(other) => out_stack.push(other),
let op = op_stack
.pop()
.ok_or_else(|| "syntax error (Mismatched close-parenthesis)".to_string())?;
match op {
(_, Token::ParOpen) => return Ok(()),
other => out_stack.push(other),
}
}
}
@ -462,22 +460,17 @@ fn infix_operator_and(values: &[String]) -> String {
fn operator_match(values: &[String]) -> Result<String, String> {
assert!(values.len() == 2);
let re = match Regex::with_options(&values[1], RegexOptions::REGEX_OPTION_NONE, Syntax::grep())
{
Ok(m) => m,
Err(err) => return Err(err.description().to_string()),
};
if re.captures_len() > 0 {
Ok(match re.captures(&values[0]) {
Some(captures) => captures.at(1).unwrap().to_string(),
None => "".to_string(),
})
let re = Regex::with_options(&values[1], RegexOptions::REGEX_OPTION_NONE, Syntax::grep())
.map_err(|err| err.description().to_string())?;
Ok(if re.captures_len() > 0 {
re.captures(&values[0])
.map(|captures| captures.at(1).unwrap())
.unwrap_or("")
.to_string()
} else {
Ok(match re.find(&values[0]) {
Some((start, end)) => (end - start).to_string(),
None => "0".to_string(),
})
}
re.find(&values[0])
.map_or("0".to_string(), |(start, end)| (end - start).to_string())
})
}
fn prefix_operator_length(values: &[String]) -> String {