1use core::fmt;
2
3use crate::error_kind::ErrorKind;
4#[cfg(not(feature = "std"))]
5use crate::strings::ErrString;
6
7#[derive(Clone, Debug, Eq, PartialEq, Hash)]
8#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
9pub struct Error {
11 kind: ErrorKind,
12}
13
14impl Error {
15 pub fn new<S>(message: S) -> Error
20 where
21 S: AsRef<str>,
22 {
23 #[cfg(feature = "std")]
24 return Error {
25 kind: ErrorKind::Other(message.as_ref().into()),
26 };
27
28 #[cfg(not(feature = "std"))]
29 return Error {
30 kind: ErrorKind::Other(ErrString::truncated(message).into()),
31 };
32 }
33
34 pub fn kind(&self) -> &ErrorKind {
38 &self.kind
39 }
40}
41
42impl Error {
43 pub(crate) fn capacity(len: usize, cap: usize) -> Error {
44 Error {
45 kind: ErrorKind::Capacity { len, cap },
46 }
47 }
48
49 #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
50 pub(crate) fn interior_nul_byte<S>(locale_name: S) -> Error
51 where
52 S: Into<String>,
53 {
54 Error {
55 kind: ErrorKind::InteriorNulByte(locale_name.into()),
56 }
57 }
58
59 pub(crate) fn parse_locale<S>(input: S) -> Error
60 where
61 S: AsRef<str>,
62 {
63 #[cfg(feature = "std")]
64 return Error {
65 kind: ErrorKind::ParseLocale(input.as_ref().into()),
66 };
67
68 #[cfg(not(feature = "std"))]
69 return Error {
70 kind: ErrorKind::ParseLocale(ErrString::truncated(input.as_ref()).into()),
71 };
72 }
73
74 pub(crate) fn parse_number<S>(input: S) -> Error
75 where
76 S: AsRef<str>,
77 {
78 #[cfg(feature = "std")]
79 return Error {
80 kind: ErrorKind::ParseNumber(input.as_ref().into()),
81 };
82
83 #[cfg(not(feature = "std"))]
84 return Error {
85 kind: ErrorKind::ParseNumber(ErrString::truncated(input.as_ref()).into()),
86 };
87 }
88
89 #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
90 pub(crate) fn system_invalid_return<S, T>(function_name: S, message: T) -> Error
91 where
92 S: Into<String>,
93 T: Into<String>,
94 {
95 Error {
96 kind: ErrorKind::SystemInvalidReturn {
97 function_name: function_name.into(),
98 message: message.into(),
99 },
100 }
101 }
102
103 #[cfg(all(feature = "with-system-locale", unix))]
104 pub(crate) fn system_unsupported_encoding<S>(encoding_name: S) -> Error
105 where
106 S: Into<String>,
107 {
108 Error {
109 kind: ErrorKind::SystemUnsupportedEncoding(encoding_name.into()),
110 }
111 }
112
113 #[cfg(all(feature = "with-system-locale", any(unix, windows)))]
114 pub(crate) fn system_unsupported_grouping<B>(bytes: B) -> Error
115 where
116 B: Into<Vec<u8>>,
117 {
118 Error {
119 kind: ErrorKind::SystemUnsupportedGrouping(bytes.into()),
120 }
121 }
122}
123
124impl fmt::Display for Error {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 write!(f, "{}", self.kind)
127 }
128}
129
130impl From<ErrorKind> for Error {
131 fn from(kind: ErrorKind) -> Error {
132 Error { kind }
133 }
134}
135
136#[cfg(feature = "std")]
137mod standard {
138 use super::*;
139
140 impl std::error::Error for Error {
141 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
142 None
143 }
144 }
145}