1use core::fmt;
23#[cfg(not(feature = "std"))]
4use arrayvec::ArrayString;
56#[cfg(not(feature = "std"))]
7use crate::strings::MAX_ERR_LEN;
89/// 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.
15Capacity {
16/// Length of the input in bytes.
17len: usize,
18/// Capacity of the buffer in bytes.
19cap: usize,
20 },
2122#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
23/// Locale name contains an interior nul byte, which is not allowed.
24InteriorNulByte(String),
2526#[cfg(feature = "std")]
27/// Other miscellaneous error.
28Other(String),
2930#[cfg(not(feature = "std"))]
31/// Other miscellaneous error.
32Other(ArrayString<MAX_ERR_LEN>),
3334#[cfg(feature = "std")]
35/// Failed to parse input into a valid locale.
36ParseLocale(String),
3738#[cfg(not(feature = "std"))]
39/// Failed to parse input into a valid locale.
40ParseLocale(ArrayString<MAX_ERR_LEN>),
4142#[cfg(feature = "std")]
43/// Failed to parse input into a number.
44ParseNumber(String),
4546#[cfg(not(feature = "std"))]
47/// Failed to parse input into a number.
48ParseNumber(ArrayString<MAX_ERR_LEN>),
4950#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
51/// Call to C standard library or Windows API unexpectedly returned invalid data.
52SystemInvalidReturn {
53/// The name of the C standard library or Windows API function called.
54function_name: String,
55/// Details about the invalid data returned.
56message: String,
57 },
5859#[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.
62SystemUnsupportedEncoding(String),
6364#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
65/// The operating system returned grouping data that is currently unsuppported by num-format.
66SystemUnsupportedGrouping(Vec<u8>),
67}
6869impl fmt::Display for ErrorKind {
70fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71use self::ErrorKind::*;
72match 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 ),
7980#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
81InteriorNulByte(ref locale_name) => write!(
82 f,
83"Locale name {} contains an interior nul byte, which is not allowed.",
84 locale_name
85 ),
8687 Other(ref message) => write!(f, "{}", message),
8889 ParseLocale(ref input) => write!(f, "Failed to parse {} into a valid locale.", input),
9091 ParseNumber(ref input) => write!(f, "Failed to parse {} into a number.", input),
9293#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
94SystemInvalidReturn { message, .. } => write!(f, "{}", message),
9596#[cfg(all(feature = "with-system-locale", unix))]
97SystemUnsupportedEncoding(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 ),
103104#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
105SystemUnsupportedGrouping(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}