num_format/
error_kind.rs

1use core::fmt;
2
3#[cfg(not(feature = "std"))]
4use arrayvec::ArrayString;
5
6#[cfg(not(feature = "std"))]
7use crate::strings::MAX_ERR_LEN;
8
9/// This crate's error kind.
10#[derive(Clone, Debug, Eq, PartialEq, Hash)]
11#[allow(missing_copy_implementations)]
12#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
13pub enum ErrorKind {
14    /// Input exceeds buffer capacity.
15    Capacity {
16        /// Length of the input in bytes.
17        len: usize,
18        /// Capacity of the buffer in bytes.
19        cap: usize,
20    },
21
22    #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
23    /// Locale name contains an interior nul byte, which is not allowed.
24    InteriorNulByte(String),
25
26    #[cfg(feature = "std")]
27    /// Other miscellaneous error.
28    Other(String),
29
30    #[cfg(not(feature = "std"))]
31    /// Other miscellaneous error.
32    Other(ArrayString<MAX_ERR_LEN>),
33
34    #[cfg(feature = "std")]
35    /// Failed to parse input into a valid locale.
36    ParseLocale(String),
37
38    #[cfg(not(feature = "std"))]
39    /// Failed to parse input into a valid locale.
40    ParseLocale(ArrayString<MAX_ERR_LEN>),
41
42    #[cfg(feature = "std")]
43    /// Failed to parse input into a number.
44    ParseNumber(String),
45
46    #[cfg(not(feature = "std"))]
47    /// Failed to parse input into a number.
48    ParseNumber(ArrayString<MAX_ERR_LEN>),
49
50    #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
51    /// Call to C standard library or Windows API unexpectedly returned invalid data.
52    SystemInvalidReturn {
53        /// The name of the C standard library or Windows API function called.
54        function_name: String,
55        /// Details about the invalid data returned.
56        message: String,
57    },
58
59    #[cfg(all(feature = "with-system-locale", unix))]
60    /// Attempted to use a system locale that relies on an encoding that is not currently supported
61    /// by num-format.
62    SystemUnsupportedEncoding(String),
63
64    #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
65    /// The operating system returned grouping data that is currently unsuppported by num-format.
66    SystemUnsupportedGrouping(Vec<u8>),
67}
68
69impl fmt::Display for ErrorKind {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        use self::ErrorKind::*;
72        match self {
73            Capacity { len, cap } => write!(
74                f,
75                "Attempted to write input of length {} bytes into a buffer with \
76                 capacity {} bytes.",
77                len, cap
78            ),
79
80            #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
81            InteriorNulByte(ref locale_name) => write!(
82                f,
83                "Locale name {} contains an interior nul byte, which is not allowed.",
84                locale_name
85            ),
86
87            Other(ref message) => write!(f, "{}", message),
88
89            ParseLocale(ref input) => write!(f, "Failed to parse {} into a valid locale.", input),
90
91            ParseNumber(ref input) => write!(f, "Failed to parse {} into a number.", input),
92
93            #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
94            SystemInvalidReturn { message, .. } => write!(f, "{}", message),
95
96            #[cfg(all(feature = "with-system-locale", unix))]
97            SystemUnsupportedEncoding(ref encoding_name) => write!(
98                f,
99                "Attempted to use a system locale that relies on an encoding that is not \
100                 currently supported by num-format. The unsupported encoding is {}.",
101                encoding_name
102            ),
103
104            #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
105            SystemUnsupportedGrouping(ref bytes) => write!(
106                f,
107                "The operating system returned grouping data of {:?}, which is not currently \
108                 suppported by num-format.",
109                bytes
110            ),
111        }
112    }
113}