diff --git a/src/format.rs b/src/format.rs index b0759b4..367fb95 100644 --- a/src/format.rs +++ b/src/format.rs @@ -51,7 +51,7 @@ fn format_number(number: f64) -> String { // Remove trailing zeroes let decimal = decimal.trim_end_matches('0'); - if decimal.len() == 0 { + if decimal.is_empty() { format!("{sign}{grouped}") } else { format!("{sign}{grouped}.{decimal}") @@ -67,54 +67,54 @@ fn prefixed_unit(quantity: MetricQuantity) -> PrefixedUnit { match quantity.unit { Metric::Metre => { if absolute >= 1000.0 { - return PrefixedUnit(1000.0, "km"); + PrefixedUnit(1000.0, "km") } else if absolute >= 1.0 { - return PrefixedUnit(1.0, "m"); + PrefixedUnit(1.0, "m") } else if absolute >= 0.01 { - return PrefixedUnit(0.01, "cm"); + PrefixedUnit(0.01, "cm") } else { - return PrefixedUnit(0.001, "mm"); + PrefixedUnit(0.001, "mm") } } Metric::Gram => { if absolute >= 1000.0 { - return PrefixedUnit(1000.0, "kg"); + PrefixedUnit(1000.0, "kg") } else { - return PrefixedUnit(1.0, "g"); + PrefixedUnit(1.0, "g") } } Metric::Celsius => PrefixedUnit(1.0, "°C"), Metric::SquareMetre => { if absolute >= 1000.0 * 1000.0 { - return PrefixedUnit(1000.0 * 1000.0, "km²"); + PrefixedUnit(1000.0 * 1000.0, "km²") } else if absolute >= 1.0 { - return PrefixedUnit(1.0, "m²"); + PrefixedUnit(1.0, "m²") } else if absolute >= 0.01 * 0.01 { - return PrefixedUnit(0.01 * 0.01, "cm²"); + PrefixedUnit(0.01 * 0.01, "cm²") } else { - return PrefixedUnit(0.001 * 0.001, "mm²"); + PrefixedUnit(0.001 * 0.001, "mm²") } } Metric::CubicMetre => { if absolute >= 1000.0 * 1000.0 * 1000.0 { - return PrefixedUnit(1000.0 * 1000.0 * 1000.0, "km³"); + PrefixedUnit(1000.0 * 1000.0 * 1000.0, "km³") } else if absolute >= 1.0 { - return PrefixedUnit(1.0, "m³"); + PrefixedUnit(1.0, "m³") } else if absolute >= 0.000_001 { // 0.01 * 0.01 * 0.01 rounds wrong - return PrefixedUnit(0.000_001, "cm³"); + PrefixedUnit(0.000_001, "cm³") } else { - return PrefixedUnit(0.001 * 0.001 * 0.001, "mm³"); + PrefixedUnit(0.001 * 0.001 * 0.001, "mm³") } } Metric::Litre => { if absolute >= 1.0 { - return PrefixedUnit(1.0, "l"); + PrefixedUnit(1.0, "l") } else if absolute >= 0.1 { - return PrefixedUnit(0.1, "dl"); + PrefixedUnit(0.1, "dl") } else if absolute >= 0.01 { - return PrefixedUnit(0.01, "cl"); + PrefixedUnit(0.01, "cl") } else { - return PrefixedUnit(0.001, "ml"); + PrefixedUnit(0.001, "ml") } } } diff --git a/src/lib.rs b/src/lib.rs index e68eb21..e7acb73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ pub fn run(input: &str) -> Result { } }; - if non_metric.len() == 0 { + if non_metric.is_empty() { return Err("Expected quantity or quantities to convert".to_string()); } @@ -59,7 +59,7 @@ pub fn run(input: &str) -> Result { let amount = metric.into_iter().map(|quantity| { quantity.amount }).sum(); let quantity = MetricQuantity { - amount: amount, + amount, unit: metric_unit.expect("we must have at least one quantity by this point"), }; diff --git a/src/main.rs b/src/main.rs index 8130ef7..1b1ef6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ fn main() { let args = args[1..].join(" "); - if args.len() == 0 { + if args.is_empty() { loop { print!("> "); match io::stdout().flush() { @@ -28,7 +28,7 @@ fn main() { process::exit(1); } } - if input.len() == 0 { + if input.is_empty() { println!(); // Add a newline if user pressed ^D break; } diff --git a/src/parse.rs b/src/parse.rs index b83ca99..ab2c956 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -36,7 +36,7 @@ pub fn parse(input: &str) -> Result, ParseError> { let unit = parse_unit(unit)?; let quantity = NonMetricQuantity { amount: amount.take().expect("must have read a number to be in this state"), - unit: unit, + unit, }; quantities.push(quantity); state = Expect::Number; @@ -56,7 +56,7 @@ pub fn parse(input: &str) -> Result, ParseError> { fn parse_number(input: String) -> Result { let no_whitespace: String = input.chars().filter(|c| !c.is_whitespace()).collect(); - no_whitespace.parse().or_else(|_| Err(ParseError::NotValidNumber(input))) + no_whitespace.parse().map_err(|_| ParseError::NotValidNumber(input)) } fn parse_unit(input: String) -> Result { @@ -411,7 +411,7 @@ fn tokenize(input: &str) -> Vec { } match state { - TokState::Neutral => { assert!(token.len() == 0); } + TokState::Neutral => { assert!(token.is_empty()); } TokState::Number => { tokens.push(Token::Number(token.trim().to_string())); } TokState::Unit(_) => { tokens.push(Token::Unit(token.trim().to_string())); } }