Skip to content

Commit

Permalink
Formatting: treat key as a shortcut for key:true
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Jan 10, 2025
1 parent 9117968 commit 5de111a
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 76 deletions.
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
18 changes: 15 additions & 3 deletions src/formatting/formatter/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@ 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.val
.error("width must be specified")?
.parse()
.error("width must be a positive integer")?,
);
}
"max_value" => {
max_value = arg.val.parse().error("Max value must be a number")?;
max_value = arg
.val
.error("max_value must be specified")?
.parse()
.error("max_value must be a number")?;
}
"vertical" | "v" => {
vertical = arg.val.parse().error("Vertical value must be a bool")?;
vertical = match arg.val {
Some(v) => v.parse().error("vertical value must be a bool")?,
None => true,
};
}
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
41 changes: 25 additions & 16 deletions src/formatting/formatter/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,56 @@ impl DurationFormatter {
for arg in args {
match arg.key {
"hms" => {
hms = arg.val.parse().ok().error("hms must be true or false")?;
hms = match arg.val {
Some(val) => val.parse().ok().error("hms must be true or false")?,
None => true,
};
}
"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
.error("units must be specified")?
.parse()
.ok()
.error("units must be a positive integer")?,
);
}
"round_up" => {
round_up = arg
.val
.parse()
.ok()
.error("round_up must be true or false")?;
round_up = match arg.val {
Some(val) => val.parse().ok().error("round_up must be true or false")?,
None => true,
};
}
"unit_space" => {
unit_has_space = arg
.val
.parse()
.ok()
.error("unit_space must be true or false")?;
unit_has_space = match arg.val {
Some(val) => val.parse().ok().error("unit_space must be true or false")?,
None => true,
};
}
"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 = match arg.val {
Some(val) => val
.parse()
.ok()
.error("leading_zeroes must be true or false")?,
None => true,
};
}

_ => return Err(Error::new(format!("Unexpected argument {:?}", arg.key))),
Expand Down
63 changes: 36 additions & 27 deletions src/formatting/formatter/eng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,60 +44,66 @@ 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
.val
.error("width must be specified")?
.parse()
.error("width must be a positive integer")?;
}
"unit" | "u" => {
result.unit = Some(arg.val.parse()?);
result.unit = Some(arg.val.error("unit must be specified")?.parse()?);
}
"hide_unit" => {
result.unit_hidden = arg
.val
.parse()
.ok()
.error("hide_unit must be true or false")?;
result.unit_hidden = match arg.val {
Some(v) => v.parse().error("hide_unit must be true or false")?,
None => true,
};
}
"unit_space" => {
result.unit_has_space = arg
.val
.error("unit_space must be specified")?
.parse()
.ok()
.error("unit_space must be true or false")?;
}
"prefix" | "p" => {
result.prefix = Some(arg.val.parse()?);
result.prefix = Some(arg.val.error("prefix must be specified")?.parse()?);
}
"hide_prefix" => {
result.prefix_hidden = arg
.val
.parse()
.ok()
.error("hide_prefix must be true or false")?;
result.prefix_hidden = match arg.val {
Some(v) => v.parse().error("hide_prefix must be true or false")?,
None => true,
};
}
"prefix_space" => {
result.prefix_has_space = arg
.val
.parse()
.ok()
.error("prefix_space must be true or false")?;
result.prefix_has_space = match arg.val {
Some(v) => v.parse().error("prefix_space must be true or false")?,
None => true,
};
}
"force_prefix" => {
result.prefix_forced = arg
.val
.parse()
.ok()
.error("force_prefix must be true or false")?;
result.prefix_forced = match arg.val {
Some(v) => v.parse().error("force_prefix must be true or false")?,
None => true,
};
}
"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 +114,10 @@ impl EngFormatter {
}
}
"show" => {
result.show = arg.val.parse().ok().error("show must be true or false")?;
result.show = match arg.val {
Some(v) => v.parse().error("show must be true or false")?,
None => true,
};
}
other => {
return Err(Error::new(format!("Unknown argument for 'eng': '{other}'")));
Expand Down
27 changes: 22 additions & 5 deletions src/formatting/formatter/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,41 @@ 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
.val
.error("min_width must be specified")?
.parse()
.error("min_width must be a positive integer")?;
}
"max_width" | "max_w" => {
max_width = arg.val.parse().error("Width must be a positive integer")?;
max_width = arg
.val
.error("max_width must be specified")?
.parse()
.error("max_width must be a positive integer")?;
}
"width" | "w" => {
min_width = arg.val.parse().error("Width must be a positive integer")?;
min_width = arg
.val
.error("width must be specified")?
.parse()
.error("width must be a positive integer")?;
max_width = min_width;
}
"rot_interval" => {
rot_interval = Some(
arg.val
.error("rot_interval must be specified")?
.parse()
.error("Interval must be a positive number")?,
.error("rot_interval must be a positive number")?,
);
}
"rot_separator" => {
rot_separator = Some(arg.val.to_string());
rot_separator = Some(
arg.val
.error("rot_separator must be specified")?
.to_string(),
);
}
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.val.error("style must be specified")?.parse()?;
}
other => {
return Err(Error::new(format!(
Expand Down
Loading

0 comments on commit 5de111a

Please sign in to comment.