Apply select clippy recommendations (#27)
* Bump dependencies to minor version; 2021 edition * apply clippy recommendations * go back to match statement * consolidate clippy allows * pare down lints - remove nursery level lints - sort lints * pared down lint allows
This commit is contained in:
parent
213789b568
commit
a64081ac73
21
src/lexer.rs
21
src/lexer.rs
@ -14,7 +14,7 @@ use crate::units::Unit::*;
|
|||||||
use unicode_segmentation::{Graphemes, UnicodeSegmentation};
|
use unicode_segmentation::{Graphemes, UnicodeSegmentation};
|
||||||
|
|
||||||
fn is_word_char_str(input: &str) -> bool {
|
fn is_word_char_str(input: &str) -> bool {
|
||||||
let x = match input {
|
match input {
|
||||||
"A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L"
|
"A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L"
|
||||||
| "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X"
|
| "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X"
|
||||||
| "Y" | "Z" => true,
|
| "Y" | "Z" => true,
|
||||||
@ -23,16 +23,11 @@ fn is_word_char_str(input: &str) -> bool {
|
|||||||
| "y" | "z" => true,
|
| "y" | "z" => true,
|
||||||
"Ω" | "Ω" | "µ" | "μ" => true,
|
"Ω" | "Ω" | "µ" | "μ" => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
}
|
||||||
return x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_numeric_str(input: &str) -> bool {
|
fn is_numeric_str(input: &str) -> bool {
|
||||||
match input {
|
matches!(input, "." | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")
|
||||||
"." => true,
|
|
||||||
"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read next characters as a word, otherwise return empty string.
|
/// Read next characters as a word, otherwise return empty string.
|
||||||
@ -40,7 +35,7 @@ fn is_numeric_str(input: &str) -> bool {
|
|||||||
fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
|
fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
|
||||||
let mut word = String::new();
|
let mut word = String::new();
|
||||||
while let Some(next_char) = chars.peek() {
|
while let Some(next_char) = chars.peek() {
|
||||||
if is_word_char_str(&next_char) {
|
if is_word_char_str(next_char) {
|
||||||
word += chars.next().unwrap();
|
word += chars.next().unwrap();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -65,7 +60,7 @@ fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
while let Some(next_char) = chars.peek() {
|
while let Some(next_char) = chars.peek() {
|
||||||
if is_word_char_str(&next_char) {
|
if is_word_char_str(next_char) {
|
||||||
word += chars.next().unwrap();
|
word += chars.next().unwrap();
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -91,7 +86,7 @@ fn parse_token(c: &str, lexer: &mut Lexer) -> Result<(), String> {
|
|||||||
let tokens = &mut lexer.tokens;
|
let tokens = &mut lexer.tokens;
|
||||||
match c {
|
match c {
|
||||||
value if value.trim().is_empty() => {},
|
value if value.trim().is_empty() => {},
|
||||||
value if is_word_char_str(&value) => {
|
value if is_word_char_str(value) => {
|
||||||
parse_word(read_word(c, lexer).as_str(), lexer)?;
|
parse_word(read_word(c, lexer).as_str(), lexer)?;
|
||||||
},
|
},
|
||||||
value if is_numeric_str(value) => {
|
value if is_numeric_str(value) => {
|
||||||
@ -361,7 +356,7 @@ fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> {
|
|||||||
other => {
|
other => {
|
||||||
lexer.tokens.push(Token::Unit(Pound));
|
lexer.tokens.push(Token::Unit(Pound));
|
||||||
lexer.tokens.push(Token::Operator(Minus));
|
lexer.tokens.push(Token::Operator(Minus));
|
||||||
parse_word_if_non_empty(&other, lexer)?;
|
parse_word_if_non_empty(other, lexer)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -619,7 +614,7 @@ struct Lexer<'a> {
|
|||||||
|
|
||||||
/// Lex an input string and returns [`Token`]s
|
/// Lex an input string and returns [`Token`]s
|
||||||
pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> Result<Vec<Token>, String> {
|
pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> Result<Vec<Token>, String> {
|
||||||
let mut input = input.replace(",", "").to_ascii_lowercase();
|
let mut input = input.replace(',', "").to_ascii_lowercase();
|
||||||
|
|
||||||
if remove_trailing_operator {
|
if remove_trailing_operator {
|
||||||
match &input.chars().last().unwrap_or('x') {
|
match &input.chars().last().unwrap_or('x') {
|
||||||
|
|||||||
12
src/lib.rs
12
src/lib.rs
@ -1,3 +1,11 @@
|
|||||||
|
#![cfg_attr(
|
||||||
|
feature = "cargo-clippy",
|
||||||
|
allow(
|
||||||
|
clippy::comparison_chain,
|
||||||
|
clippy::if_same_then_else,
|
||||||
|
clippy::match_like_matches_macro,
|
||||||
|
)
|
||||||
|
)]
|
||||||
//! calculation + conversion
|
//! calculation + conversion
|
||||||
//!
|
//!
|
||||||
//! cpc parses and evaluates strings of math, with support for units and conversion. 128-bit decimal floating points are used for high accuracy.
|
//! cpc parses and evaluates strings of math, with support for units and conversion. 128-bit decimal floating points are used for high accuracy.
|
||||||
@ -59,7 +67,7 @@ pub struct Number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Number {
|
impl Number {
|
||||||
pub fn new(value: d128, unit: Unit) -> Number {
|
pub const fn new(value: d128, unit: Unit) -> Number {
|
||||||
Number { value, unit }
|
Number { value, unit }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,7 +79,7 @@ impl Display for Number {
|
|||||||
Unit::NoUnit => format!("{}", fixed_value),
|
Unit::NoUnit => format!("{}", fixed_value),
|
||||||
unit => format!("{} {:?}", fixed_value, unit),
|
unit => format!("{} {:?}", fixed_value, unit),
|
||||||
};
|
};
|
||||||
return write!(f, "{}", output);
|
write!(f, "{}", output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ use cpc::units::Unit;
|
|||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
fn print_help() {
|
fn print_help() {
|
||||||
println!(concat!(
|
println!(concat!(
|
||||||
@ -17,9 +17,9 @@ fn print_help() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_args() -> env::Args {
|
fn get_args() -> env::Args {
|
||||||
let mut args = env::args().into_iter();
|
let mut args = env::args();
|
||||||
args.next(); // skip binary name
|
args.next(); // skip binary name
|
||||||
return args;
|
args
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CLI interface
|
/// CLI interface
|
||||||
@ -44,7 +44,7 @@ fn main() {
|
|||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"-v" | "--verbose" => verbose = true,
|
"-v" | "--verbose" => verbose = true,
|
||||||
_ => {
|
_ => {
|
||||||
if expression_opt == None {
|
if expression_opt.is_none() {
|
||||||
expression_opt = Some(arg);
|
expression_opt = Some(arg);
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Unexpected argument: {}", arg);
|
eprintln!("Unexpected argument: {}", arg);
|
||||||
@ -60,6 +60,7 @@ fn main() {
|
|||||||
exit(0);
|
exit(0);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
match eval(&expression, true, Unit::Celsius, verbose) {
|
match eval(&expression, true, Unit::Celsius, verbose) {
|
||||||
Ok(answer) => {
|
Ok(answer) => {
|
||||||
if !verbose {
|
if !verbose {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ pub struct AstNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AstNode {
|
impl AstNode {
|
||||||
pub fn new(token: Token) -> AstNode {
|
pub const fn new(token: Token) -> AstNode {
|
||||||
AstNode {
|
AstNode {
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
token,
|
token,
|
||||||
@ -331,7 +331,7 @@ pub fn parse_level_7(tokens: &[Token], pos: usize) -> Result<(AstNode, usize), S
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
return Err(format!("Expected ( after {} at {:?} but found {:?}", left_paren_pos, token, left_paren_token));
|
Err(format!("Expected ( after {} at {:?} but found {:?}", left_paren_pos, token, left_paren_token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
14
src/units.rs
14
src/units.rs
@ -302,19 +302,19 @@ create_units!(
|
|||||||
// These functions are here to avoid dividing by small numbers like 0.01,
|
// These functions are here to avoid dividing by small numbers like 0.01,
|
||||||
// because d128 gives numbers in E notation in that case.
|
// because d128 gives numbers in E notation in that case.
|
||||||
fn get_inverted_millijoule_weight() -> d128 {
|
fn get_inverted_millijoule_weight() -> d128 {
|
||||||
return d128!(1000);
|
d128!(1000)
|
||||||
}
|
}
|
||||||
fn get_inverted_milliwatt_weight() -> d128 {
|
fn get_inverted_milliwatt_weight() -> d128 {
|
||||||
return d128!(1000);
|
d128!(1000)
|
||||||
}
|
}
|
||||||
fn get_inverted_milliohm_weight() -> d128 {
|
fn get_inverted_milliohm_weight() -> d128 {
|
||||||
return d128!(1000);
|
d128!(1000)
|
||||||
}
|
}
|
||||||
fn get_inverted_milliampere_weight() -> d128 {
|
fn get_inverted_milliampere_weight() -> d128 {
|
||||||
return d128!(1000);
|
d128!(1000)
|
||||||
}
|
}
|
||||||
fn get_inverted_millivolt_weight() -> d128 {
|
fn get_inverted_millivolt_weight() -> d128 {
|
||||||
return d128!(1000);
|
d128!(1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the conversion factor between two units.
|
/// Returns the conversion factor between two units.
|
||||||
@ -378,7 +378,7 @@ pub fn add(left: Number, right: Number) -> Result<Number, String> {
|
|||||||
let (left, right) = convert_to_lowest(left, right)?;
|
let (left, right) = convert_to_lowest(left, right)?;
|
||||||
Ok(Number::new(left.value + right.value, left.unit))
|
Ok(Number::new(left.value + right.value, left.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("Cannot add {:?} and {:?}", left.unit, right.unit))
|
Err(format!("Cannot add {:?} and {:?}", left.unit, right.unit))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -390,7 +390,7 @@ pub fn subtract(left: Number, right: Number) -> Result<Number, String> {
|
|||||||
let (left, right) = convert_to_lowest(left, right)?;
|
let (left, right) = convert_to_lowest(left, right)?;
|
||||||
Ok(Number::new(left.value - right.value, left.unit))
|
Ok(Number::new(left.value - right.value, left.unit))
|
||||||
} else {
|
} else {
|
||||||
return Err(format!("Cannot subtract {:?} by {:?}", left.unit, right.unit))
|
Err(format!("Cannot subtract {:?} by {:?}", left.unit, right.unit))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user