parse_format/
display.rs

1use super::*;
2
3use std::fmt::{self, Display, Formatter, Write};
4
5impl<'a> Display for Piece<'a> {
6    #[inline]
7    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
8        match self {
9            String(s) => {
10                if let Some(c @ ('{' | '}')) = s.chars().next() {
11                    f.write_char(c)?;
12                }
13                s.fmt(f)
14            }
15            NextArgument(a) => a.fmt(f),
16        }
17    }
18}
19
20impl<'a> Display for Argument<'a> {
21    #[inline]
22    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
23        f.write_char('{')?;
24        self.position.fmt(f)?;
25        self.format.fmt(f)?;
26        f.write_char('}')
27    }
28}
29
30impl<'a> Display for FormatSpec<'a> {
31    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
32        if self.is_empty() {
33            return Ok(());
34        }
35        f.write_char(':')?;
36        if let Some(fill) = self.fill {
37            f.write_char(fill)?;
38        }
39        self.align.fmt(f)?;
40        if self.flags & (1 << FlagSignPlus as u32) != 0 {
41            f.write_char('+')?;
42        } else if self.flags & (1 << FlagSignMinus as u32) != 0 {
43            f.write_char('-')?;
44        }
45        if self.flags & (1 << FlagAlternate as u32) != 0 {
46            f.write_char('#')?;
47        }
48        if self.flags & (1 << FlagSignAwareZeroPad as u32) != 0 {
49            f.write_char('0')?;
50        }
51        self.width.fmt(f)?;
52        if self.precision != CountImplied {
53            f.write_char('.')?;
54            self.precision.fmt(f)?;
55        }
56        if self.flags & (1 << FlagDebugLowerHex as u32) != 0 {
57            f.write_char('x')?;
58        } else if self.flags & (1 << FlagDebugUpperHex as u32) != 0 {
59            f.write_char('X')?;
60        }
61        self.ty.fmt(f)
62    }
63}
64
65impl<'a> Display for Position<'a> {
66    #[inline]
67    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
68        match self {
69            ArgumentImplicitlyIs(_) => Ok(()),
70            ArgumentIs(p) => p.fmt(f),
71            ArgumentNamed(s) => s.fmt(f),
72        }
73    }
74}
75
76impl Display for Alignment {
77    #[inline]
78    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
79        match self {
80            AlignLeft => f.write_char('<'),
81            AlignRight => f.write_char('>'),
82            AlignCenter => f.write_char('^'),
83            AlignUnknown => Ok(()),
84        }
85    }
86}
87
88impl<'a> Display for Count<'a> {
89    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
90        match self {
91            CountIs(p) => p.fmt(f),
92            CountIsName(s, _) => {
93                s.fmt(f)?;
94                f.write_char('$')
95            }
96            CountIsParam(p) => {
97                p.fmt(f)?;
98                f.write_char('$')
99            }
100            CountIsStar(_) => f.write_char('*'),
101            CountImplied => Ok(()),
102        }
103    }
104}
105
106impl Display for ParseError {
107    #[inline]
108    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
109        write!(f, "{}: {}", self.span.start, self.description)
110    }
111}
112
113impl std::error::Error for ParseError {}