Skip to content

Commit 771c047

Browse files
horazontdjc
authored andcommitted
Use Formatter::pad (instead of write_str) for Weekdays
That way, width, alignment and fill from the format string are respected. Fixes #1620.
1 parent d8a177e commit 771c047

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/weekday.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Weekday {
171171

172172
impl fmt::Display for Weekday {
173173
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174-
f.write_str(match *self {
174+
f.pad(match *self {
175175
Weekday::Mon => "Mon",
176176
Weekday::Tue => "Tue",
177177
Weekday::Wed => "Wed",
@@ -331,6 +331,16 @@ mod tests {
331331
}
332332
}
333333

334+
#[test]
335+
fn test_formatting_alignment() {
336+
// No exhaustive testing here as we just delegate the
337+
// implementation to Formatter::pad. Just some basic smoke
338+
// testing to ensure that it's in fact being done.
339+
assert_eq!(format!("{:x>7}", Weekday::Mon), "xxxxMon");
340+
assert_eq!(format!("{:^7}", Weekday::Mon), " Mon ");
341+
assert_eq!(format!("{:Z<7}", Weekday::Mon), "MonZZZZ");
342+
}
343+
334344
#[test]
335345
#[cfg(feature = "serde")]
336346
fn test_serde_serialize() {

0 commit comments

Comments
 (0)