serde_encrypt_core/
error.rs1mod error_kind;
4
5pub use self::error_kind::ErrorKind;
6
7use alloc::string::{String, ToString};
8use core::fmt::Display;
9
10#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
12pub struct Error {
13 kind: ErrorKind,
15
16 reason: String,
18}
19
20impl Display for Error {
21 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22 write!(f, "{}: {}", self.kind, self.reason)
23 }
24}
25
26#[cfg(feature = "std")]
27impl std::error::Error for Error {}
28
29impl Error {
30 pub fn kind(&self) -> &ErrorKind {
32 &self.kind
33 }
34
35 fn new(kind: ErrorKind, reason: &str) -> Self {
36 Self {
37 kind,
38 reason: reason.to_string(),
39 }
40 }
41
42 #[doc(hidden)]
43 pub fn serialization_error(reason: &str) -> Self {
44 Self::new(ErrorKind::SerializationError, reason)
45 }
46
47 #[doc(hidden)]
48 pub fn deserialization_error(reason: &str) -> Self {
49 Self::new(ErrorKind::DeserializationError, reason)
50 }
51
52 #[doc(hidden)]
53 pub fn encryption_error(reason: &str) -> Self {
54 Self::new(ErrorKind::EncryptionError, reason)
55 }
56
57 #[doc(hidden)]
58 pub fn decryption_error(reason: &str) -> Self {
59 Self::new(ErrorKind::DecryptionError, reason)
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66 use alloc::format;
67
68 #[test]
69 fn test_display() {
70 let e = Error::serialization_error("x");
71 let _s = format!("{}", e);
72 }
73}