From 771c0477bf4fa3febc74f3c8cd8e7a25d9463a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Mon, 14 Oct 2024 12:42:49 +0200 Subject: [PATCH] Use Formatter::pad (instead of write_str) for Weekdays That way, width, alignment and fill from the format string are respected. Fixes #1620. --- src/weekday.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/weekday.rs b/src/weekday.rs index f41b5845cf..5c62ea3ed9 100644 --- a/src/weekday.rs +++ b/src/weekday.rs @@ -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", @@ -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() {