1use core::fmt::{self, Display};
6
7#[cfg(feature = "std")]
8#[derive(Debug)]
9pub struct AnyError(Box<dyn std::error::Error + Send + Sync>);
11
12#[cfg(not(feature = "std"))]
13#[derive(Debug)]
14pub struct AnyError;
15
16#[cfg(feature = "std")]
17impl Display for AnyError {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 self.0.fmt(f)
20 }
21}
22
23#[cfg(feature = "std")]
24impl std::error::Error for AnyError {
25 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
26 self.0.source()
27 }
28}
29
30#[cfg(feature = "std")]
31impl AnyError {
32 pub fn inner_dyn_error(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
33 &*self.0
34 }
35}
36
37#[cfg(not(feature = "std"))]
38impl Display for AnyError {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 core::fmt::Debug::fmt(self, f)
41 }
42}
43
44pub trait IntoAnyError: core::fmt::Debug + Sized {
46 #[cfg(feature = "std")]
47 fn into_any_error(self) -> AnyError {
48 self.into_dyn_error()
49 .map_or_else(|this| AnyError(format!("{this:?}").into()), AnyError)
50 }
51
52 #[cfg(not(feature = "std"))]
53 fn into_any_error(self) -> AnyError {
54 AnyError
55 }
56
57 #[cfg(feature = "std")]
58 fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> {
59 Err(self)
60 }
61}
62
63impl IntoAnyError for mls_rs_codec::Error {
64 #[cfg(feature = "std")]
65 fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> {
66 Ok(self.into())
67 }
68}
69
70impl IntoAnyError for core::convert::Infallible {}
71
72#[cfg(test)]
73mod tests {
74 use super::{AnyError, IntoAnyError};
75 use mls_rs_codec::Error as CodecError;
76
77 #[test]
78 fn inner_dyn_error_returns_wrapped_error_with_data() {
79 let error_with_data = CodecError::Custom(42);
80 let any_error: AnyError = error_with_data.into_any_error();
81
82 let downcast_err = any_error
83 .inner_dyn_error()
84 .downcast_ref::<CodecError>()
85 .expect("Expected the dyn error to be of type CodecError");
86
87 assert!(matches!(downcast_err, CodecError::Custom(42)));
88 }
89}