diff --git a/src/evaluator.rs b/src/evaluator.rs index 7f4b306..ce4f94d 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -21,7 +21,7 @@ pub fn evaluate(ast: &AstNode) -> Result { /// /// All return values of this function are hard-coded. pub fn factorial(input: d128) -> d128 { - return lookup_factorial(input.into()); + lookup_factorial(input.into()) } /// Returns the square root of a [`struct@d128`] @@ -31,7 +31,7 @@ pub fn sqrt(input: d128) -> d128 { for _ in 0..10 { n = (n + input/n) * half; } - return n + n } /// Returns the cube root of a [`struct@d128`] @@ -43,7 +43,7 @@ pub fn cbrt(input: d128) -> d128 { let z2 = n*n; n = n - ((n*z2 - input) / (three*z2)); } - return n + n } /// Returns the sine of a [`struct@d128`] @@ -72,19 +72,19 @@ pub fn sin(mut input: d128) -> d128 { result += neg_one.pow(i) * (input.pow(calc_result) / factorial(calc_result)); } - return negative_correction * result; + negative_correction * result } /// Returns the cosine of a [`struct@d128`] pub fn cos(input: d128) -> d128 { let half_pi = d128!(1.570796326794896619231321691639751); - return sin(half_pi - input); + sin(half_pi - input) } /// Returns the tangent of a [`struct@d128`] pub fn tan(input: d128) -> d128 { - return sin(input) / cos(input); + sin(input) / cos(input) } /// Evaluate an [`AstNode`] into a [`Number`] @@ -93,7 +93,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result { let children = &ast_node.children; match token { Token::Number(number) => { - Ok(Number::new(number.clone(), Unit::NoUnit)) + Ok(Number::new(*number, Unit::NoUnit)) }, Token::Constant(constant) => { match constant { @@ -112,41 +112,41 @@ fn evaluate_node(ast_node: &AstNode) -> Result { Cbrt => { if child_answer.unit.category() == UnitType::NoType { let result = cbrt(child_answer.value); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) } else { - return Err("log() only accepts UnitType::NoType".to_string()) + Err("log() only accepts UnitType::NoType".to_string()) } }, Sqrt => { if child_answer.unit.category() == UnitType::NoType { let result = sqrt(child_answer.value); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) } else { - return Err("log() only accepts UnitType::NoType".to_string()) + Err("log() only accepts UnitType::NoType".to_string()) } }, Log => { if child_answer.unit.category() == UnitType::NoType { let result = child_answer.value.log10(); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) } else { - return Err("log() only accepts UnitType::NoType".to_string()) + Err("log() only accepts UnitType::NoType".to_string()) } }, Ln => { if child_answer.unit.category() == UnitType::NoType { let result = child_answer.value.ln(); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) } else { - return Err("ln() only accepts UnitType::NoType".to_string()) + Err("ln() only accepts UnitType::NoType".to_string()) } }, Exp => { if child_answer.unit.category() == UnitType::NoType { let result = child_answer.value.exp(child_answer.value); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) } else { - return Err("exp() only accepts UnitType::NoType".to_string()) + Err("exp() only accepts UnitType::NoType".to_string()) } }, Round => { @@ -155,44 +155,44 @@ fn evaluate_node(ast_node: &AstNode) -> Result { let rounding_change = result - child_answer.value; // If the result was rounded down by 0.5, correct by +1 if rounding_change == d128!(-0.5) { result += d128!(1); } - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, Ceil => { let mut result = child_answer.value.quantize(d128!(1)); let rounding_change = result - child_answer.value; if rounding_change.is_negative() { result += d128!(1); } - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, Floor => { let mut result = child_answer.value.quantize(d128!(1)); let rounding_change = result - child_answer.value; if !rounding_change.is_negative() { result -= d128!(1); } - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, Abs => { let mut result = child_answer.value.abs(); let rounding_change = result - child_answer.value; if rounding_change == d128!(-0.5) { result += d128!(1); } - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, Sin => { let result = sin(child_answer.value); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, Cos => { let result = cos(child_answer.value); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, Tan => { let result = tan(child_answer.value); - return Ok(Number::new(result, child_answer.unit)) + Ok(Number::new(result, child_answer.unit)) }, } } Token::Unit(unit) => { let child_node = children.get(0).ok_or("Unit has no child[0]")?; let child_answer = evaluate_node(child_node)?; - Ok(Number::new(child_answer.value, unit.clone())) + Ok(Number::new(child_answer.value, *unit)) }, Token::Negative => { let child_node = children.get(0).ok_or("Negative has no child[0]")?; @@ -201,7 +201,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result { }, Token::Paren => { let child_node = children.get(0).ok_or("Paren has no child[0]")?; - return evaluate_node(child_node) + evaluate_node(child_node) }, Token::UnaryOperator(operator) => { let child_node = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?; @@ -241,18 +241,18 @@ fn evaluate_node(ast_node: &AstNode) -> Result { if let Token::Unit(right_unit) = right_child.token { let left = evaluate_node(left_child)?; let result = convert(left, right_unit)?; - return Ok(result) + Ok(result) } else { - return Err("Right side of To operator needs to be a unit".to_string()) + Err("Right side of To operator needs to be a unit".to_string()) } }, Of => { let left = evaluate_node(left_child)?; let right = evaluate_node(right_child)?; if left.unit == Unit::NoUnit { - return Ok(Number::new(left.value * right.value, right.unit)) + Ok(Number::new(left.value * right.value, right.unit)) } else { - return Err("Left side of the Of operator must be NoUnit".to_string()) + Err("Left side of the Of operator must be NoUnit".to_string()) } }, } diff --git a/src/lexer.rs b/src/lexer.rs index 407a772..de9596b 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -46,7 +46,7 @@ fn read_word_plain(chars: &mut Peekable) -> String { break; } } - return word; + word } /// Read next as a word, otherwise return empty string. @@ -54,7 +54,7 @@ fn read_word_plain(chars: &mut Peekable) -> String { fn read_word(first_c: &str, lexer: &mut Lexer) -> String { let chars = &mut lexer.chars; let mut word = first_c.trim().to_owned(); - if word == "" { + if word.is_empty() { // skip whitespace while let Some(current_char) = chars.peek() { if current_char.trim().is_empty() { @@ -71,7 +71,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String { break; } } - if word != "" { + if !word.is_empty() { match *chars.peek().unwrap_or(&"") { "2" | "²" => { word += "2"; @@ -84,7 +84,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String { _ => {}, } } - return word; + word } fn parse_token(c: &str, lexer: &mut Lexer) -> Result<(), String> { @@ -606,7 +606,7 @@ fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> { } }; lexer.tokens.push(token); - return Ok(()); + Ok(()) } struct Lexer<'a> { @@ -655,8 +655,8 @@ pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> } } - if tokens.len() == 0 { - return Err(format!("Input was empty")) + if tokens.is_empty() { + return Err("Input was empty".to_string()); } let mut token_index = 0; diff --git a/src/lib.rs b/src/lib.rs index 5c827de..60fce3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -232,27 +232,27 @@ pub fn eval(input: &str, allow_trailing_operators: bool, default_degree: Unit, v match lexer::lex(input, allow_trailing_operators, default_degree) { Ok(tokens) => { let lex_time = Instant::now().duration_since(lex_start).as_nanos() as f32; - if verbose == true { println!("Lexed TokenVector: {:?}", tokens); } + if verbose { println!("Lexed TokenVector: {:?}", tokens); } let parse_start = Instant::now(); match parser::parse(&tokens) { Ok(ast) => { let parse_time = Instant::now().duration_since(parse_start).as_nanos() as f32; - if verbose == true { println!("Parsed AstNode: {:#?}", ast); } + if verbose { println!("Parsed AstNode: {:#?}", ast); } let eval_start = Instant::now(); match evaluator::evaluate(&ast) { Ok(answer) => { let eval_time = Instant::now().duration_since(eval_start).as_nanos() as f32; - if verbose == true { + if verbose { println!("Evaluated value: {} {:?}", answer.value, answer.unit); println!("\u{23f1} {:.3}ms lexing", lex_time/1000.0/1000.0); println!("\u{23f1} {:.3}ms parsing", parse_time/1000.0/1000.0); println!("\u{23f1} {:.3}ms evaluation", eval_time/1000.0/1000.0); } - return Ok(answer) + Ok(answer) }, Err(e) => Err(format!("Eval error: {}", e)), } diff --git a/src/lookup.rs b/src/lookup.rs index 3042f5f..67cb10a 100644 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -4,7 +4,7 @@ use decimal::d128; /// Returns the corresponding [`d128`] of a [`NamedNumber`] pub fn lookup_named_number(named_number: &NamedNumber) -> d128 { - return match named_number { + match named_number { Hundred => d128!(100), Thousand => d128!(1000), Million => d128!(1000000), @@ -34,7 +34,7 @@ pub fn lookup_named_number(named_number: &NamedNumber) -> d128 { /// Returns the factorial of an `i32` as a [`struct@d128`] pub fn lookup_factorial(n: i32) -> d128 { - return match n { + match n { 0 => d128!(1), 1 => d128!(1), 2 => d128!(2), @@ -1037,5 +1037,5 @@ pub fn lookup_factorial(n: i32) -> d128 { 999 => d128!(4.023872600770937735437024339230041E+2564), 1000 => d128!(4.023872600770937735437024339230041E+2567), _ => d128!("NaN"), - }; + } } diff --git a/src/parser.rs b/src/parser.rs index 32725c4..1aa215f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -17,7 +17,7 @@ impl AstNode { pub fn new(token: Token) -> AstNode { AstNode { children: Vec::new(), - token: token, + token, } } } @@ -253,11 +253,9 @@ pub fn parse_level_5(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), S let (right_node, next_pos) = parse_level_6(tokens, pos + 1)?; let mut new_node = AstNode::new(Token::Negative); new_node.children.push(right_node); - return Ok((new_node, next_pos)); + Ok((new_node, next_pos)) }, - _ => { - return Ok(parse_level_6(tokens, pos)?); - } + _ => parse_level_6(tokens, pos) } } diff --git a/src/units.rs b/src/units.rs index fdc9894..5ed1819 100644 --- a/src/units.rs +++ b/src/units.rs @@ -325,7 +325,7 @@ fn get_inverted_millivolt_weight() -> d128 { /// /// This is not sufficient for [`Temperature`] units. pub fn get_conversion_factor(unit: Unit, to_unit: Unit) -> d128 { - return unit.weight() / to_unit.weight(); + unit.weight() / to_unit.weight() } /// Convert a [`Number`] to a specified [`Unit`]. @@ -550,7 +550,7 @@ pub fn to_ideal_joule_unit(number: Number) -> Number { /// - If you multiply [`ElectricCurrent`] with [`Resistance`], the result has a unit of [`Voltage`] /// - If you multiply [`Power`] with [`Time`], the result has a unit of [`Energy`] pub fn multiply(left: Number, right: Number) -> Result { - Ok(actual_multiply(left, right, false)?) + actual_multiply(left, right, false) } fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result { @@ -644,15 +644,13 @@ fn actual_multiply(left: Number, right: Number, swapped: bool) -> Result return Ok(to_ideal_joule_unit(Number::new(result, Joule))), - _ => return Ok(to_ideal_unit(Number::new(result, Joule))), - }; - } else { - if swapped == true { - Err(format!("Cannot multiply {:?} and {:?}", right.unit, left.unit)) - } else { - actual_multiply(right, left, true) + Second => Ok(to_ideal_joule_unit(Number::new(result, Joule))), + _ => Ok(to_ideal_unit(Number::new(result, Joule))), } + } else if swapped { + Err(format!("Cannot multiply {:?} and {:?}", right.unit, left.unit)) + } else { + actual_multiply(right, left, true) } }