Skip to content

Commit

Permalink
Improve formatting of numbers, closes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Mar 29, 2023
1 parent 681e117 commit 2ccf25a
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 216 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Next
- Improve formatting of numbers

## 1.9.0 - 2022 Dec 30
- Add `marathon` unit
- Add `aarch64` binaries
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ match eval("3m + 1cm", true, Unit::Celsius, false) {
println!("Evaluated value: {} {:?}", answer.value, answer.unit)
},
Err(e) => {
println!("{}", e)
println!("{e}")
}
}
```
Expand Down
10 changes: 5 additions & 5 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,22 +952,22 @@ mod tests {
}
};
let info_msg = format!("run_lex input: {}\nexpected: {:?}\nreceived: {:?}", input, expected_tokens, tokens);
assert!(tokens == expected_tokens, "{}", info_msg);
assert!(tokens == expected_tokens, "{info_msg}");

// Prove we can handle multiple spaces wherever we handle a single space
let input_extra_spaces = input.replace(" ", " ");
let tokens_extra_spaces = lex(&input_extra_spaces, false, Unit::Celsius).unwrap();
assert!(tokens_extra_spaces == expected_tokens, "{}", info_msg);
assert!(tokens_extra_spaces == expected_tokens, "{info_msg}");

// Prove we don't need spaces around operators
let input_stripped_spaces = strip_operator_spacing.replace_all(input, "$1");
let tokens_stripped_spaces = lex(&input_stripped_spaces, false, Unit::Celsius).unwrap();
assert!(tokens_stripped_spaces == expected_tokens, "{}", info_msg);
assert!(tokens_stripped_spaces == expected_tokens, "{info_msg}");

// Prove we don't need a space after a digit
let input_afterdigit_stripped_spaces = strip_afterdigit_spacing.replace_all(input, "$1");
let tokens_afterdigit_stripped_spaces = lex(&input_afterdigit_stripped_spaces, false, Unit::Celsius).unwrap();
assert!(tokens_afterdigit_stripped_spaces == expected_tokens, "{}", info_msg);
assert!(tokens_afterdigit_stripped_spaces == expected_tokens, "{info_msg}");
};

let run_datarate_lex = |input: &str, expected_tokens: Vec<Token>| {
Expand All @@ -977,7 +977,7 @@ mod tests {
let input_nonplural_units = nonplural_data_units.replace_all(input, "$1");
let tokens_nonplural_units = lex(&input_nonplural_units, false, Unit::Celsius).unwrap();
let info_msg = format!("run_datarate_lex input: {}\nexpected: {:?}\nreceived: {:?}", input, expected_tokens, tokens_nonplural_units);
assert!(tokens_nonplural_units == expected_tokens, "{}", info_msg);
assert!(tokens_nonplural_units == expected_tokens, "{info_msg}");
};

run_lex("88 kilometres * 2", vec![numtok!(88), Token::Unit(Kilometer), Token::Operator(Multiply), numtok!(2)]);
Expand Down
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! println!("Evaluated value: {} {:?}", answer.value, answer.unit)
//! },
//! Err(e) => {
//! println!("{}", e)
//! println!("{e}")
//! }
//! }
//! ```
Expand Down Expand Up @@ -77,12 +77,16 @@ impl Number {
impl Display for Number {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// 0.2/0.01 results in 2E+1, but if we add zero it becomes 20
let fixed_value = self.value + d128!(0);
let output = match self.unit {
Unit::NoUnit => format!("{}", fixed_value),
unit => format!("{} {:?}", fixed_value, unit),
let value = self.value + d128!(0);
let word = match self.value == d128!(1) {
true => self.unit.singular(),
false => self.unit.plural(),
};
write!(f, "{}", output)
let output = match word {
"" => format!("{value}"),
_ => format!("{value} {word}"),
};
write!(f, "{output}")
}
}

Expand Down Expand Up @@ -232,7 +236,7 @@ macro_rules! numtok {
/// println!("Evaluated value: {} {:?}", answer.value, answer.unit)
/// },
/// Err(e) => {
/// println!("{}", e)
/// println!("{e}")
/// }
/// }
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
for arg in get_args() {
match arg.as_str() {
"--version" => {
println!("{}", VERSION);
println!("{VERSION}");
exit(0);
}
"--help" => {
Expand Down Expand Up @@ -64,11 +64,11 @@ fn main() {
match eval(&expression, true, Unit::Celsius, verbose) {
Ok(answer) => {
if !verbose {
println!("{}", answer);
println!("{answer}");
}
}
Err(e) => {
eprintln!("{}", e);
eprintln!("{e}");
exit(1);
}
}
Expand Down
Loading

0 comments on commit 2ccf25a

Please sign in to comment.