core_crypto/error/
recursive.rs1#![allow(missing_docs)]
2
3#[derive(Debug)]
8pub enum RecursiveError {
9 Root {
10 context: &'static str,
11 source: Box<crate::Error>,
12 },
13 TransactionContext {
14 context: &'static str,
15 source: Box<crate::transaction_context::Error>,
16 },
17 E2e {
18 context: &'static str,
19 source: Box<crate::e2e_identity::Error>,
20 },
21 Mls {
22 context: &'static str,
23 source: Box<crate::mls::Error>,
24 },
25 MlsClient {
26 context: &'static str,
27 source: Box<crate::mls::session::Error>,
28 },
29 MlsConversation {
30 context: &'static str,
31 source: Box<crate::mls::conversation::Error>,
32 },
33 MlsCredential {
34 context: &'static str,
35 source: Box<crate::mls::credential::Error>,
36 },
37 #[cfg(test)]
38 Test(Box<crate::test_utils::TestError>),
39}
40
41impl RecursiveError {
42 pub fn root<E: Into<crate::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
43 move |into_source| Self::Root {
44 context,
45 source: Box::new(into_source.into()),
46 }
47 }
48
49 pub fn transaction<E: Into<crate::transaction_context::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
50 move |into_source| Self::TransactionContext {
51 context,
52 source: Box::new(into_source.into()),
53 }
54 }
55
56 pub fn e2e_identity<E: Into<crate::e2e_identity::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
57 move |into_source| Self::E2e {
58 context,
59 source: Box::new(into_source.into()),
60 }
61 }
62
63 pub fn mls<E: Into<crate::mls::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
64 move |into_source| Self::Mls {
65 context,
66 source: Box::new(into_source.into()),
67 }
68 }
69
70 pub fn mls_client<E: Into<crate::mls::session::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
71 move |into_source| Self::MlsClient {
72 context,
73 source: Box::new(into_source.into()),
74 }
75 }
76
77 pub fn mls_conversation<E: Into<crate::mls::conversation::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
78 move |into_source| Self::MlsConversation {
79 context,
80 source: Box::new(into_source.into()),
81 }
82 }
83
84 pub fn mls_credential<E: Into<crate::mls::credential::Error>>(context: &'static str) -> impl FnOnce(E) -> Self {
85 move |into_source| Self::MlsCredential {
86 context,
87 source: Box::new(into_source.into()),
88 }
89 }
90}
91
92impl std::fmt::Display for RecursiveError {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 #[cfg(test)]
95 use std::ops::Deref;
96
97 let context = match self {
98 RecursiveError::Root { context, .. } => context,
99 RecursiveError::E2e { context, .. } => context,
100 RecursiveError::Mls { context, .. } => context,
101 RecursiveError::MlsClient { context, .. } => context,
102 RecursiveError::MlsConversation { context, .. } => context,
103 RecursiveError::MlsCredential { context, .. } => context,
104 RecursiveError::TransactionContext { context, .. } => context,
105 #[cfg(test)]
106 RecursiveError::Test(e) => return e.deref().fmt(f),
107 };
108 write!(f, "{context}")
109 }
110}
111
112impl std::error::Error for RecursiveError {
113 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
114 match self {
115 RecursiveError::Root { source, .. } => Some(source.as_ref()),
116 RecursiveError::E2e { source, .. } => Some(source.as_ref()),
117 RecursiveError::Mls { source, .. } => Some(source.as_ref()),
118 RecursiveError::MlsClient { source, .. } => Some(source.as_ref()),
119 RecursiveError::MlsConversation { source, .. } => Some(source.as_ref()),
120 RecursiveError::MlsCredential { source, .. } => Some(source.as_ref()),
121 RecursiveError::TransactionContext { source, .. } => Some(source.as_ref()),
122 #[cfg(test)]
123 RecursiveError::Test(source) => Some(source.as_ref()),
124 }
125 }
126}
127
128pub trait ToRecursiveError {
132 fn construct_recursive(self, context: &'static str) -> RecursiveError;
134}
135
136macro_rules! impl_to_recursive_error_for {
137 ($($for:path => $variant:ident),+ $(,)?) => {
138 $(
139 impl ToRecursiveError for $for {
140 fn construct_recursive(self, context: &'static str) -> RecursiveError {
141 RecursiveError::$variant {
142 context,
143 source: Box::new(self),
144 }
145 }
146 }
147 )+
148 };
149}
150
151impl_to_recursive_error_for!(
152 crate::Error => Root,
153 crate::e2e_identity::Error => E2e,
154 crate::mls::Error => Mls,
155 crate::mls::session::Error => MlsClient,
156 crate::mls::conversation::Error => MlsConversation,
157 crate::mls::credential::Error => MlsCredential,
158 crate::transaction_context::Error => TransactionContext,
159);