Skip to main content

rtformat/
error.rs

1use core::fmt;
2
3/// Error returned by [`Format::try_format`](crate::Format::try_format).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[non_exhaustive]
6pub enum FormatError {
7    /// A placeholder index is beyond the number of supplied arguments.
8    InsufficientParameters,
9    /// The template is syntactically invalid (unbalanced braces, named
10    /// arguments, an illegal spec, ...).
11    InvalidFormatString,
12    /// The argument does not support the requested format type (e.g. `{:x}`
13    /// on a string, or the unsupported `{:p}`).
14    UnsupportedFormatType,
15    /// The argument referenced by `{:1$}` / `{:.1$}` as a width/precision is
16    /// not an unsigned integer.
17    ExpectedUsize,
18    /// The output sink rejected a write (`fmt::Write::write_str` returned
19    /// `Err`). Writing into a `String` never fails.
20    WriteFailed,
21}
22
23impl fmt::Display for FormatError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            FormatError::InsufficientParameters => write!(f, "Insufficient parameters"),
27            FormatError::InvalidFormatString => write!(f, "Invalid format string"),
28            FormatError::UnsupportedFormatType => {
29                write!(f, "Argument does not support the requested format type")
30            }
31            FormatError::ExpectedUsize => {
32                write!(f, "Width/precision argument must be an unsigned integer")
33            }
34            FormatError::WriteFailed => write!(f, "Failed to write to the output sink"),
35        }
36    }
37}
38
39impl core::error::Error for FormatError {}