Skip to content

Commit

Permalink
Use Formatter::pad (instead of write_str) for Weekdays
Browse files Browse the repository at this point in the history
That way, width, alignment and fill from the format string are
respected.

Fixes #1620.
  • Loading branch information
horazont authored and djc committed Oct 14, 2024
1 parent d8a177e commit 771c047
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/weekday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Weekday {

impl fmt::Display for Weekday {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
f.pad(match *self {
Weekday::Mon => "Mon",
Weekday::Tue => "Tue",
Weekday::Wed => "Wed",
Expand Down Expand Up @@ -331,6 +331,16 @@ mod tests {
}
}

#[test]
fn test_formatting_alignment() {
// No exhaustive testing here as we just delegate the
// implementation to Formatter::pad. Just some basic smoke
// testing to ensure that it's in fact being done.
assert_eq!(format!("{:x>7}", Weekday::Mon), "xxxxMon");
assert_eq!(format!("{:^7}", Weekday::Mon), " Mon ");
assert_eq!(format!("{:Z<7}", Weekday::Mon), "MonZZZZ");
}

#[test]
#[cfg(feature = "serde")]
fn test_serde_serialize() {
Expand Down

0 comments on commit 771c047

Please sign in to comment.