1use core::fmt::{self, Debug, Display};
4
5#[cfg(feature = "alloc")]
6use alloc::boxed::Box;
7
8pub type Result<T> = core::result::Result<T, Error>;
12
13#[derive(Default)]
24#[non_exhaustive]
25pub struct Error {
26 #[cfg(feature = "alloc")]
28 source: Option<Box<dyn core::error::Error + Send + Sync + 'static>>,
29}
30
31impl Error {
32 pub fn new() -> Self {
34 Self::default()
35 }
36
37 #[cfg(feature = "alloc")]
44 pub fn from_source(
45 source: impl Into<Box<dyn core::error::Error + Send + Sync + 'static>>,
46 ) -> Self {
47 Self {
48 source: Some(source.into()),
49 }
50 }
51}
52
53impl Debug for Error {
54 #[cfg(not(feature = "alloc"))]
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 f.write_str("signature::Error {}")
57 }
58
59 #[cfg(feature = "alloc")]
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 f.write_str("signature::Error { source: ")?;
62
63 if let Some(source) = &self.source {
64 write!(f, "Some({source})")?;
65 } else {
66 f.write_str("None")?;
67 }
68
69 f.write_str(" }")
70 }
71}
72
73impl Display for Error {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 f.write_str("signature error")
76 }
77}
78
79#[cfg(feature = "alloc")]
80impl From<Box<dyn core::error::Error + Send + Sync + 'static>> for Error {
81 fn from(source: Box<dyn core::error::Error + Send + Sync + 'static>) -> Error {
82 Self::from_source(source)
83 }
84}
85
86impl core::error::Error for Error {
87 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
88 #[cfg(not(feature = "alloc"))]
89 {
90 None
91 }
92 #[cfg(feature = "alloc")]
93 {
94 self.source
95 .as_ref()
96 .map(|source| source.as_ref() as &(dyn core::error::Error + 'static))
97 }
98 }
99}