Skip to content

Commit 2ccf25a

Browse files
Improve formatting of numbers, closes #20
1 parent 681e117 commit 2ccf25a

File tree

6 files changed

+241
-216
lines changed

6 files changed

+241
-216
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Next
4+
- Improve formatting of numbers
5+
36
## 1.9.0 - 2022 Dec 30
47
- Add `marathon` unit
58
- Add `aarch64` binaries

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ match eval("3m + 1cm", true, Unit::Celsius, false) {
3939
println!("Evaluated value: {} {:?}", answer.value, answer.unit)
4040
},
4141
Err(e) => {
42-
println!("{}", e)
42+
println!("{e}")
4343
}
4444
}
4545
```

src/lexer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -952,22 +952,22 @@ mod tests {
952952
}
953953
};
954954
let info_msg = format!("run_lex input: {}\nexpected: {:?}\nreceived: {:?}", input, expected_tokens, tokens);
955-
assert!(tokens == expected_tokens, "{}", info_msg);
955+
assert!(tokens == expected_tokens, "{info_msg}");
956956

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

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

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

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

983983
run_lex("88 kilometres * 2", vec![numtok!(88), Token::Unit(Kilometer), Token::Operator(Multiply), numtok!(2)]);

src/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! println!("Evaluated value: {} {:?}", answer.value, answer.unit)
2626
//! },
2727
//! Err(e) => {
28-
//! println!("{}", e)
28+
//! println!("{e}")
2929
//! }
3030
//! }
3131
//! ```
@@ -77,12 +77,16 @@ impl Number {
7777
impl Display for Number {
7878
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7979
// 0.2/0.01 results in 2E+1, but if we add zero it becomes 20
80-
let fixed_value = self.value + d128!(0);
81-
let output = match self.unit {
82-
Unit::NoUnit => format!("{}", fixed_value),
83-
unit => format!("{} {:?}", fixed_value, unit),
80+
let value = self.value + d128!(0);
81+
let word = match self.value == d128!(1) {
82+
true => self.unit.singular(),
83+
false => self.unit.plural(),
8484
};
85-
write!(f, "{}", output)
85+
let output = match word {
86+
"" => format!("{value}"),
87+
_ => format!("{value} {word}"),
88+
};
89+
write!(f, "{output}")
8690
}
8791
}
8892

@@ -232,7 +236,7 @@ macro_rules! numtok {
232236
/// println!("Evaluated value: {} {:?}", answer.value, answer.unit)
233237
/// },
234238
/// Err(e) => {
235-
/// println!("{}", e)
239+
/// println!("{e}")
236240
/// }
237241
/// }
238242
/// ```

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
for arg in get_args() {
2929
match arg.as_str() {
3030
"--version" => {
31-
println!("{}", VERSION);
31+
println!("{VERSION}");
3232
exit(0);
3333
}
3434
"--help" => {
@@ -64,11 +64,11 @@ fn main() {
6464
match eval(&expression, true, Unit::Celsius, verbose) {
6565
Ok(answer) => {
6666
if !verbose {
67-
println!("{}", answer);
67+
println!("{answer}");
6868
}
6969
}
7070
Err(e) => {
71-
eprintln!("{}", e);
71+
eprintln!("{e}");
7272
exit(1);
7373
}
7474
}

0 commit comments

Comments
 (0)