Skip to content

Commit

Permalink
Formatting: treat key as a shortcut for key:true (#2126)
Browse files Browse the repository at this point in the history
Fixes: #2084
  • Loading branch information
bim9262 authored Feb 12, 2025
1 parent 0afe30d commit 2fac1a0
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 91 deletions.
2 changes: 2 additions & 0 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
//! to method calls in many programming languages: `<variable>.<formatter>(<args>)`. For example:
//! `$title.str(min_w:10, max_w:20)`.
//!
//! Note: for arguments that accept a boolean value, just specifying the argument will be treated as `arg:true`.
//!
//! ## `str` - Format text
//!
//! Argument | Description |Default value
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! new_fmt {
}};
($name:ident, $($key:ident : $value:tt),* $(,)?) => {
new_formatter(stringify!($name), &[
$( Arg { key: stringify!($key), val: stringify!($value) } ),*
$( Arg { key: stringify!($key), val: Some(stringify!($value)) } ),*
])
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/formatting/formatter/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ impl BarFormatter {
for arg in args {
match arg.key {
"width" | "w" => {
width = Some(arg.val.parse().error("Width must be a positive integer")?);
width = Some(arg.parse_value()?);
}
"max_value" => {
max_value = arg.val.parse().error("Max value must be a number")?;
max_value = arg.parse_value()?;
}
"vertical" | "v" => {
vertical = arg.val.parse().error("Vertical value must be a bool")?;
vertical = arg.parse_value()?;
}
other => {
return Err(Error::new(format!("Unknown argument for 'bar': '{other}'")));
Expand Down
4 changes: 2 additions & 2 deletions src/formatting/formatter/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ impl DatetimeFormatter {
for arg in args {
match arg.key {
"format" | "f" => {
format = Some(arg.val);
format = Some(arg.val.error("format must be specified")?);
}
"locale" | "l" => {
locale = Some(arg.val);
locale = Some(arg.val.error("locale must be specified")?);
}
other => {
return Err(Error::new(format!(
Expand Down
32 changes: 10 additions & 22 deletions src/formatting/formatter/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,35 @@ impl DurationFormatter {
for arg in args {
match arg.key {
"hms" => {
hms = arg.val.parse().ok().error("hms must be true or false")?;
hms = arg.parse_value()?;
}
"max_unit" => {
max_unit = Some(arg.val);
max_unit = Some(arg.val.error("max_unit must be specified")?);
}
"min_unit" => {
min_unit = arg.val;
min_unit = arg.val.error("min_unit must be specified")?;
}
"units" => {
units = Some(
arg.val
.parse()
.ok()
.error("units must be a positive integer")?,
);
units = Some(arg.parse_value()?);
}
"round_up" => {
round_up = arg
.val
.parse()
.ok()
.error("round_up must be true or false")?;
round_up = arg.parse_value()?;
}
"unit_space" => {
unit_has_space = arg
.val
.parse()
.ok()
.error("unit_space must be true or false")?;
unit_has_space = arg.parse_value()?;
}
"pad_with" => {
if arg.val.graphemes(true).count() < 2 {
pad_with = Some(Cow::Owned(arg.val.into()));
let pad_with_str = arg.val.error("pad_with must be specified")?;
if pad_with_str.graphemes(true).count() < 2 {
pad_with = Some(Cow::Owned(pad_with_str.into()));
} else {
return Err(Error::new(
"pad_with must be an empty string or a single character",
));
};
}
"leading_zeroes" => {
leading_zeroes = arg.val.parse().ok().error("units must be true or false")?;
leading_zeroes = arg.parse_value()?;
}

_ => return Err(Error::new(format!("Unexpected argument {:?}", arg.key))),
Expand Down
49 changes: 17 additions & 32 deletions src/formatting/formatter/eng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,60 +44,45 @@ impl EngFormatter {
for arg in args {
match arg.key {
"width" | "w" => {
result.width = arg.val.parse().error("Width must be a positive integer")?;
result.width = arg.parse_value()?;
}
"unit" | "u" => {
result.unit = Some(arg.val.parse()?);
result.unit = Some(arg.parse_value()?);
}
"hide_unit" => {
result.unit_hidden = arg
.val
.parse()
.ok()
.error("hide_unit must be true or false")?;
result.unit_hidden = arg.parse_value()?;
}
"unit_space" => {
result.unit_has_space = arg
.val
.parse()
.ok()
.error("unit_space must be true or false")?;
result.unit_has_space = arg.parse_value()?;
}
"prefix" | "p" => {
result.prefix = Some(arg.val.parse()?);
result.prefix = Some(arg.parse_value()?);
}
"hide_prefix" => {
result.prefix_hidden = arg
.val
.parse()
.ok()
.error("hide_prefix must be true or false")?;
result.prefix_hidden = arg.parse_value()?;
}
"prefix_space" => {
result.prefix_has_space = arg
.val
.parse()
.ok()
.error("prefix_space must be true or false")?;
result.prefix_has_space = arg.parse_value()?;
}
"force_prefix" => {
result.prefix_forced = arg
.val
.parse()
.ok()
.error("force_prefix must be true or false")?;
result.prefix_forced = arg.parse_value()?;
}
"pad_with" => {
if arg.val.graphemes(true).count() < 2 {
result.pad_with = Cow::Owned(arg.val.into());
let pad_with_str = arg.val.error("pad_with must be specified")?;
if pad_with_str.graphemes(true).count() < 2 {
result.pad_with = Cow::Owned(pad_with_str.into());
} else {
return Err(Error::new(
"pad_with must be an empty string or a single character",
));
}
}
"range" => {
let (start, end) = arg.val.split_once("..").error("invalid range")?;
let (start, end) = arg
.val
.error("range must be specified")?
.split_once("..")
.error("invalid range")?;
if !start.is_empty() {
result.range = start.parse::<f64>().error("invalid range start")?
..=*result.range.end();
Expand All @@ -108,7 +93,7 @@ impl EngFormatter {
}
}
"show" => {
result.show = arg.val.parse().ok().error("show must be true or false")?;
result.show = arg.parse_value()?;
}
other => {
return Err(Error::new(format!("Unknown argument for 'eng': '{other}'")));
Expand Down
14 changes: 5 additions & 9 deletions src/formatting/formatter/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,20 @@ impl StrFormatter {
for arg in args {
match arg.key {
"min_width" | "min_w" => {
min_width = arg.val.parse().error("Width must be a positive integer")?;
min_width = arg.parse_value()?;
}
"max_width" | "max_w" => {
max_width = arg.val.parse().error("Width must be a positive integer")?;
max_width = arg.parse_value()?;
}
"width" | "w" => {
min_width = arg.val.parse().error("Width must be a positive integer")?;
min_width = arg.parse_value()?;
max_width = min_width;
}
"rot_interval" => {
rot_interval = Some(
arg.val
.parse()
.error("Interval must be a positive number")?,
);
rot_interval = Some(arg.parse_value()?);
}
"rot_separator" => {
rot_separator = Some(arg.val.to_string());
rot_separator = Some(arg.parse_value()?);
}
other => {
return Err(Error::new(format!("Unknown argument for 'str': '{other}'")));
Expand Down
2 changes: 1 addition & 1 deletion src/formatting/formatter/tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl TallyFormatter {
for arg in args {
match arg.key {
"style" | "s" => {
style = arg.val.parse()?;
style = arg.parse_value()?;
}
other => {
return Err(Error::new(format!(
Expand Down
Loading

0 comments on commit 2fac1a0

Please sign in to comment.