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 #[must_use]
34 pub fn new() -> Self {
35 Self::default()
36 }
37
38 #[cfg(feature = "alloc")]
45 pub fn from_source(
46 source: impl Into<Box<dyn core::error::Error + Send + Sync + 'static>>,
47 ) -> Self {
48 Self {
49 source: Some(source.into()),
50 }
51 }
52}
53
54impl Debug for Error {
55 #[cfg(not(feature = "alloc"))]
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 f.write_str("signature::Error {}")
58 }
59
60 #[cfg(feature = "alloc")]
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 f.write_str("signature::Error { source: ")?;
63
64 if let Some(source) = &self.source {
65 write!(f, "Some({source})")?;
66 } else {
67 f.write_str("None")?;
68 }
69
70 f.write_str(" }")
71 }
72}
73
74impl Display for Error {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 f.write_str("signature error")
77 }
78}
79
80#[cfg(feature = "alloc")]
81impl From<Box<dyn core::error::Error + Send + Sync + 'static>> for Error {
82 fn from(source: Box<dyn core::error::Error + Send + Sync + 'static>) -> Error {
83 Self::from_source(source)
84 }
85}
86
87impl core::error::Error for Error {
88 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
89 #[cfg(not(feature = "alloc"))]
90 {
91 None
92 }
93 #[cfg(feature = "alloc")]
94 #[allow(trivial_casts)]
95 {
96 self.source
97 .as_ref()
98 .map(|source| source.as_ref() as &(dyn core::error::Error + 'static))
99 }
100 }
101}