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