Skip to content

Commit 2fac1a0

Browse files
authored
Formatting: treat key as a shortcut for key:true (#2126)
Fixes: #2084
1 parent 0afe30d commit 2fac1a0

File tree

9 files changed

+155
-91
lines changed

9 files changed

+155
-91
lines changed

src/formatting.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
//! to method calls in many programming languages: `<variable>.<formatter>(<args>)`. For example:
2929
//! `$title.str(min_w:10, max_w:20)`.
3030
//!
31+
//! Note: for arguments that accept a boolean value, just specifying the argument will be treated as `arg:true`.
32+
//!
3133
//! ## `str` - Format text
3234
//!
3335
//! Argument | Description |Default value

src/formatting/formatter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! new_fmt {
1818
}};
1919
($name:ident, $($key:ident : $value:tt),* $(,)?) => {
2020
new_formatter(stringify!($name), &[
21-
$( Arg { key: stringify!($key), val: stringify!($value) } ),*
21+
$( Arg { key: stringify!($key), val: Some(stringify!($value)) } ),*
2222
])
2323
};
2424
}

src/formatting/formatter/bar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ impl BarFormatter {
2020
for arg in args {
2121
match arg.key {
2222
"width" | "w" => {
23-
width = Some(arg.val.parse().error("Width must be a positive integer")?);
23+
width = Some(arg.parse_value()?);
2424
}
2525
"max_value" => {
26-
max_value = arg.val.parse().error("Max value must be a number")?;
26+
max_value = arg.parse_value()?;
2727
}
2828
"vertical" | "v" => {
29-
vertical = arg.val.parse().error("Vertical value must be a bool")?;
29+
vertical = arg.parse_value()?;
3030
}
3131
other => {
3232
return Err(Error::new(format!("Unknown argument for 'bar': '{other}'")));

src/formatting/formatter/datetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl DatetimeFormatter {
3434
for arg in args {
3535
match arg.key {
3636
"format" | "f" => {
37-
format = Some(arg.val);
37+
format = Some(arg.val.error("format must be specified")?);
3838
}
3939
"locale" | "l" => {
40-
locale = Some(arg.val);
40+
locale = Some(arg.val.error("locale must be specified")?);
4141
}
4242
other => {
4343
return Err(Error::new(format!(

src/formatting/formatter/duration.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,35 @@ impl DurationFormatter {
5151
for arg in args {
5252
match arg.key {
5353
"hms" => {
54-
hms = arg.val.parse().ok().error("hms must be true or false")?;
54+
hms = arg.parse_value()?;
5555
}
5656
"max_unit" => {
57-
max_unit = Some(arg.val);
57+
max_unit = Some(arg.val.error("max_unit must be specified")?);
5858
}
5959
"min_unit" => {
60-
min_unit = arg.val;
60+
min_unit = arg.val.error("min_unit must be specified")?;
6161
}
6262
"units" => {
63-
units = Some(
64-
arg.val
65-
.parse()
66-
.ok()
67-
.error("units must be a positive integer")?,
68-
);
63+
units = Some(arg.parse_value()?);
6964
}
7065
"round_up" => {
71-
round_up = arg
72-
.val
73-
.parse()
74-
.ok()
75-
.error("round_up must be true or false")?;
66+
round_up = arg.parse_value()?;
7667
}
7768
"unit_space" => {
78-
unit_has_space = arg
79-
.val
80-
.parse()
81-
.ok()
82-
.error("unit_space must be true or false")?;
69+
unit_has_space = arg.parse_value()?;
8370
}
8471
"pad_with" => {
85-
if arg.val.graphemes(true).count() < 2 {
86-
pad_with = Some(Cow::Owned(arg.val.into()));
72+
let pad_with_str = arg.val.error("pad_with must be specified")?;
73+
if pad_with_str.graphemes(true).count() < 2 {
74+
pad_with = Some(Cow::Owned(pad_with_str.into()));
8775
} else {
8876
return Err(Error::new(
8977
"pad_with must be an empty string or a single character",
9078
));
9179
};
9280
}
9381
"leading_zeroes" => {
94-
leading_zeroes = arg.val.parse().ok().error("units must be true or false")?;
82+
leading_zeroes = arg.parse_value()?;
9583
}
9684

9785
_ => return Err(Error::new(format!("Unexpected argument {:?}", arg.key))),

src/formatting/formatter/eng.rs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,60 +44,45 @@ impl EngFormatter {
4444
for arg in args {
4545
match arg.key {
4646
"width" | "w" => {
47-
result.width = arg.val.parse().error("Width must be a positive integer")?;
47+
result.width = arg.parse_value()?;
4848
}
4949
"unit" | "u" => {
50-
result.unit = Some(arg.val.parse()?);
50+
result.unit = Some(arg.parse_value()?);
5151
}
5252
"hide_unit" => {
53-
result.unit_hidden = arg
54-
.val
55-
.parse()
56-
.ok()
57-
.error("hide_unit must be true or false")?;
53+
result.unit_hidden = arg.parse_value()?;
5854
}
5955
"unit_space" => {
60-
result.unit_has_space = arg
61-
.val
62-
.parse()
63-
.ok()
64-
.error("unit_space must be true or false")?;
56+
result.unit_has_space = arg.parse_value()?;
6557
}
6658
"prefix" | "p" => {
67-
result.prefix = Some(arg.val.parse()?);
59+
result.prefix = Some(arg.parse_value()?);
6860
}
6961
"hide_prefix" => {
70-
result.prefix_hidden = arg
71-
.val
72-
.parse()
73-
.ok()
74-
.error("hide_prefix must be true or false")?;
62+
result.prefix_hidden = arg.parse_value()?;
7563
}
7664
"prefix_space" => {
77-
result.prefix_has_space = arg
78-
.val
79-
.parse()
80-
.ok()
81-
.error("prefix_space must be true or false")?;
65+
result.prefix_has_space = arg.parse_value()?;
8266
}
8367
"force_prefix" => {
84-
result.prefix_forced = arg
85-
.val
86-
.parse()
87-
.ok()
88-
.error("force_prefix must be true or false")?;
68+
result.prefix_forced = arg.parse_value()?;
8969
}
9070
"pad_with" => {
91-
if arg.val.graphemes(true).count() < 2 {
92-
result.pad_with = Cow::Owned(arg.val.into());
71+
let pad_with_str = arg.val.error("pad_with must be specified")?;
72+
if pad_with_str.graphemes(true).count() < 2 {
73+
result.pad_with = Cow::Owned(pad_with_str.into());
9374
} else {
9475
return Err(Error::new(
9576
"pad_with must be an empty string or a single character",
9677
));
9778
}
9879
}
9980
"range" => {
100-
let (start, end) = arg.val.split_once("..").error("invalid range")?;
81+
let (start, end) = arg
82+
.val
83+
.error("range must be specified")?
84+
.split_once("..")
85+
.error("invalid range")?;
10186
if !start.is_empty() {
10287
result.range = start.parse::<f64>().error("invalid range start")?
10388
..=*result.range.end();
@@ -108,7 +93,7 @@ impl EngFormatter {
10893
}
10994
}
11095
"show" => {
111-
result.show = arg.val.parse().ok().error("show must be true or false")?;
96+
result.show = arg.parse_value()?;
11297
}
11398
other => {
11499
return Err(Error::new(format!("Unknown argument for 'eng': '{other}'")));

src/formatting/formatter/str.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,20 @@ impl StrFormatter {
3636
for arg in args {
3737
match arg.key {
3838
"min_width" | "min_w" => {
39-
min_width = arg.val.parse().error("Width must be a positive integer")?;
39+
min_width = arg.parse_value()?;
4040
}
4141
"max_width" | "max_w" => {
42-
max_width = arg.val.parse().error("Width must be a positive integer")?;
42+
max_width = arg.parse_value()?;
4343
}
4444
"width" | "w" => {
45-
min_width = arg.val.parse().error("Width must be a positive integer")?;
45+
min_width = arg.parse_value()?;
4646
max_width = min_width;
4747
}
4848
"rot_interval" => {
49-
rot_interval = Some(
50-
arg.val
51-
.parse()
52-
.error("Interval must be a positive number")?,
53-
);
49+
rot_interval = Some(arg.parse_value()?);
5450
}
5551
"rot_separator" => {
56-
rot_separator = Some(arg.val.to_string());
52+
rot_separator = Some(arg.parse_value()?);
5753
}
5854
other => {
5955
return Err(Error::new(format!("Unknown argument for 'str': '{other}'")));

src/formatting/formatter/tally.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl TallyFormatter {
3737
for arg in args {
3838
match arg.key {
3939
"style" | "s" => {
40-
style = arg.val.parse()?;
40+
style = arg.parse_value()?;
4141
}
4242
other => {
4343
return Err(Error::new(format!(

0 commit comments

Comments
 (0)