Skip to content

Commit

Permalink
Use format args capture
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Aug 10, 2022
1 parent 7e52d3a commit 08e2136
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ impl Duration {
/// # use time::Duration;
/// #
/// let duration = Duration::new(123456, 789011223);
/// println!("{:.3}", duration);
/// println!("{duration:.3}");
/// ```
///
/// For the purposes of this implementation, a day is exactly 24 hours and a minute is exactly 60
Expand Down
3 changes: 1 addition & 2 deletions src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ impl fmt::Display for Format {
),
Self::InvalidComponent(component) => write!(
f,
"The {} component cannot be formatted into the requested format.",
component
"The {component} component cannot be formatted into the requested format."
),
Self::StdIo(err) => err.fmt(f),
}
Expand Down
14 changes: 6 additions & 8 deletions src/error/invalid_format_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,16 @@ impl fmt::Display for InvalidFormatDescription {
use InvalidFormatDescription::*;
match self {
UnclosedOpeningBracket { index } => {
write!(f, "unclosed opening bracket at byte index {}", index)
write!(f, "unclosed opening bracket at byte index {index}")
}
InvalidComponentName { name, index } => {
write!(f, "invalid component name `{name}` at byte index {index}")
}
InvalidComponentName { name, index } => write!(
f,
"invalid component name `{}` at byte index {}",
name, index
),
InvalidModifier { value, index } => {
write!(f, "invalid modifier `{}` at byte index {}", value, index)
write!(f, "invalid modifier `{value}` at byte index {index}")
}
MissingComponentName { index } => {
write!(f, "missing component name at byte index {}", index)
write!(f, "missing component name at byte index {index}")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/error/parse_from_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl fmt::Display for ParseFromDescription {
match self {
Self::InvalidLiteral => f.write_str("a character literal was not valid"),
Self::InvalidComponent(name) => {
write!(f, "the '{}' component could not be parsed", name)
write!(f, "the '{name}' component could not be parsed")
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,8 @@ impl fmt::Display for Time {
};
write!(
f,
"{}:{:02}:{:02}.{:0width$}",
self.hour,
self.minute,
self.second,
value,
width = width
"{}:{:02}:{:02}.{value:0width$}",
self.hour, self.minute, self.second,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ fn failed_write() -> time::Result<()> {
"offset_second",
];
for component in &component_names {
let component = format!("[{}]", component);
let component = format!("[{component}]");
assert_err!(
OffsetDateTime::UNIX_EPOCH,
format_description::parse(&component)?
Expand Down
37 changes: 15 additions & 22 deletions tests/integration/parse_format_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,48 +298,45 @@ fn errors() {
fn component_with_modifiers() {
for (padding, padding_str) in iterator::padding() {
assert_eq!(
format_description::parse(&format!("[day {}]", padding_str)),
format_description::parse(&format!("[day {padding_str}]")),
Ok(vec![FormatItem::Component(Component::Day(modifier!(
Day { padding }
)))])
);
assert_eq!(
format_description::parse(&format!("[minute {}]", padding_str)),
format_description::parse(&format!("[minute {padding_str}]")),
Ok(vec![FormatItem::Component(Component::Minute(modifier!(
Minute { padding }
)))])
);
assert_eq!(
format_description::parse(&format!("[offset_minute {}]", padding_str)),
format_description::parse(&format!("[offset_minute {padding_str}]")),
Ok(vec![FormatItem::Component(Component::OffsetMinute(
modifier!(OffsetMinute { padding })
))])
);
assert_eq!(
format_description::parse(&format!("[offset_second {}]", padding_str)),
format_description::parse(&format!("[offset_second {padding_str}]")),
Ok(vec![FormatItem::Component(Component::OffsetSecond(
modifier!(OffsetSecond { padding })
))])
);
assert_eq!(
format_description::parse(&format!("[ordinal {}]", padding_str)),
format_description::parse(&format!("[ordinal {padding_str}]")),
Ok(vec![FormatItem::Component(Component::Ordinal(modifier!(
Ordinal { padding }
)))])
);
assert_eq!(
format_description::parse(&format!("[second {}]", padding_str)),
format_description::parse(&format!("[second {padding_str}]")),
Ok(vec![FormatItem::Component(Component::Second(modifier!(
Second { padding }
)))])
);

for (is_12_hour_clock, is_12_hour_clock_str) in iterator::hour_is_12_hour_clock() {
assert_eq!(
format_description::parse(&format!(
"[hour {} {}]",
padding_str, is_12_hour_clock_str
)),
format_description::parse(&format!("[hour {padding_str} {is_12_hour_clock_str}]")),
Ok(vec![FormatItem::Component(Component::Hour(modifier!(
Hour {
padding,
Expand All @@ -352,8 +349,7 @@ fn component_with_modifiers() {
for (repr, repr_str) in iterator::month_repr() {
assert_eq!(
format_description::parse(&format!(
"[month {} {} {}]",
padding_str, case_sensitive_repr, repr_str
"[month {padding_str} {case_sensitive_repr} {repr_str}]"
)),
Ok(vec![FormatItem::Component(Component::Month(modifier!(
Month {
Expand All @@ -367,8 +363,7 @@ fn component_with_modifiers() {
for (is_uppercase, is_uppercase_str) in iterator::period_is_uppercase() {
assert_eq!(
format_description::parse(&format!(
"[period {} {}]",
is_uppercase_str, case_sensitive_repr
"[period {is_uppercase_str} {case_sensitive_repr}]"
)),
Ok(vec![FormatItem::Component(Component::Period(modifier!(
Period {
Expand All @@ -382,8 +377,7 @@ fn component_with_modifiers() {
for (one_indexed, one_indexed_str) in iterator::weekday_is_one_indexed() {
assert_eq!(
format_description::parse(&format!(
"[weekday {} {} {} ]",
repr_str, one_indexed_str, case_sensitive_repr
"[weekday {repr_str} {one_indexed_str} {case_sensitive_repr} ]"
)),
Ok(vec![FormatItem::Component(Component::Weekday(modifier!(
Weekday {
Expand All @@ -398,7 +392,7 @@ fn component_with_modifiers() {
}
for (repr, repr_str) in iterator::week_number_repr() {
assert_eq!(
format_description::parse(&format!("[week_number {} {}]", padding_str, repr_str)),
format_description::parse(&format!("[week_number {padding_str} {repr_str}]")),
Ok(vec![FormatItem::Component(Component::WeekNumber(
modifier!(WeekNumber { padding, repr })
))])
Expand All @@ -407,8 +401,7 @@ fn component_with_modifiers() {
for (sign_is_mandatory, sign_is_mandatory_str) in iterator::sign_is_mandatory() {
assert_eq!(
format_description::parse(&format!(
"[offset_hour {} {}]",
padding_str, sign_is_mandatory_str
"[offset_hour {padding_str} {sign_is_mandatory_str}]"
)),
Ok(vec![FormatItem::Component(Component::OffsetHour(
modifier!(OffsetHour {
Expand All @@ -422,8 +415,8 @@ fn component_with_modifiers() {
for (iso_week_based, iso_week_based_str) in iterator::year_is_iso_week_based() {
assert_eq!(
format_description::parse(&format!(
"[year {} {} {} {}]",
padding_str, repr_str, iso_week_based_str, sign_is_mandatory_str
"[year {padding_str} {repr_str} {iso_week_based_str} \
{sign_is_mandatory_str}]",
)),
Ok(vec![FormatItem::Component(Component::Year(modifier!(
Year {
Expand All @@ -441,7 +434,7 @@ fn component_with_modifiers() {

for (digits, digits_str) in iterator::subsecond_digits() {
assert_eq!(
format_description::parse(&format!("[subsecond {}]", digits_str)),
format_description::parse(&format!("[subsecond {digits_str}]")),
Ok(vec![FormatItem::Component(Component::Subsecond(
modifier!(Subsecond { digits })
))])
Expand Down
6 changes: 3 additions & 3 deletions time-macros/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ pub(crate) enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingComponent { name, .. } => write!(f, "missing component: {}", name),
Self::MissingComponent { name, .. } => write!(f, "missing component: {name}"),
Self::InvalidComponent { name, value, .. } => {
write!(f, "invalid component: {} was {}", name, value)
write!(f, "invalid component: {name} was {value}")
}
Self::ExpectedString { .. } => f.write_str("expected string"),
Self::UnexpectedToken { tree } => write!(f, "unexpected token: {}", tree),
Self::UnexpectedToken { tree } => write!(f, "unexpected token: {tree}"),
Self::UnexpectedEndOfInput => f.write_str("unexpected end of input"),
Self::InvalidFormatDescription { error, .. } => error.fmt(f),
Self::Custom { message, .. } => f.write_str(message),
Expand Down
14 changes: 6 additions & 8 deletions time-macros/src/format_description/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ impl fmt::Display for InvalidFormatDescription {
use InvalidFormatDescription::*;
match self {
UnclosedOpeningBracket { index } => {
write!(f, "unclosed opening bracket at byte index {}", index)
write!(f, "unclosed opening bracket at byte index {index}")
}
InvalidComponentName { name, index } => {
write!(f, "invalid component name `{name}` at byte index {index}",)
}
InvalidComponentName { name, index } => write!(
f,
"invalid component name `{}` at byte index {}",
name, index
),
InvalidModifier { value, index } => {
write!(f, "invalid modifier `{}` at byte index {}", value, index)
write!(f, "invalid modifier `{value}` at byte index {index}")
}
MissingComponentName { index } => {
write!(f, "missing component name at byte index {}", index)
write!(f, "missing component name at byte index {index}")
}
}
}
Expand Down

0 comments on commit 08e2136

Please sign in to comment.