solana_signature/
error.rs1#[cfg(feature = "alloc")]
5use alloc::boxed::Box;
6use core::fmt::{self, Debug, Display};
7
8#[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 #[cfg(feature = "alloc")]
39 pub fn from_source(
40 source: impl Into<Box<dyn core::error::Error + Send + Sync + 'static>>,
41 ) -> Self {
42 Self {
43 source: Some(source.into()),
44 }
45 }
46}
47
48impl Debug for Error {
49 #[cfg(not(feature = "alloc"))]
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 f.write_str("signature::Error {}")
52 }
53
54 #[cfg(feature = "alloc")]
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 f.write_str("signature::Error { source: ")?;
57
58 if let Some(source) = &self.source {
59 write!(f, "Some({source})")?;
60 } else {
61 f.write_str("None")?;
62 }
63
64 f.write_str(" }")
65 }
66}
67
68impl Display for Error {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 f.write_str("signature error")
71 }
72}
73
74#[cfg(feature = "alloc")]
75impl From<Box<dyn core::error::Error + Send + Sync + 'static>> for Error {
76 fn from(source: Box<dyn core::error::Error + Send + Sync + 'static>) -> Error {
77 Self::from_source(source)
78 }
79}
80
81impl core::error::Error for Error {
82 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
83 #[cfg(feature = "alloc")]
84 {
85 self.source
86 .as_ref()
87 .map(|source| source.as_ref() as &(dyn core::error::Error + 'static))
88 }
89 #[cfg(not(feature = "alloc"))]
90 {
91 None
92 }
93 }
94}