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#[derive(Clone, Debug, Eq, PartialEq, Hash)]
11#[allow(missing_copy_implementations)]
12#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
13pub enum ErrorKind {
14 Capacity {
16 len: usize,
18 cap: usize,
20 },
21
22 #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
23 InteriorNulByte(String),
25
26 #[cfg(feature = "std")]
27 Other(String),
29
30 #[cfg(not(feature = "std"))]
31 Other(ArrayString<MAX_ERR_LEN>),
33
34 #[cfg(feature = "std")]
35 ParseLocale(String),
37
38 #[cfg(not(feature = "std"))]
39 ParseLocale(ArrayString<MAX_ERR_LEN>),
41
42 #[cfg(feature = "std")]
43 ParseNumber(String),
45
46 #[cfg(not(feature = "std"))]
47 ParseNumber(ArrayString<MAX_ERR_LEN>),
49
50 #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
51 SystemInvalidReturn {
53 function_name: String,
55 message: String,
57 },
58
59 #[cfg(all(feature = "with-system-locale", unix))]
60 SystemUnsupportedEncoding(String),
63
64 #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
65 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}