actor_system_error/
lib.rs1#![no_std]
25
26#[macro_export]
28macro_rules! actor_system_error {
29 (
30 $(#[$($meta:meta)*])?
31 $vis:vis type $name:ident = ActorSystemError<$actor_err:ident, $system_err:ident>;
32 ) => {
33 $(#[$($meta)*])?
34 $vis type $name = $crate::ActorSystemError<$actor_err, $system_err>;
35
36 impl From<$actor_err> for $crate::ActorSystemError<$actor_err, $system_err> {
37 fn from(err: $actor_err) -> Self {
38 Self::Actor(err)
39 }
40 }
41
42 impl From<$system_err> for $crate::ActorSystemError<$actor_err, $system_err> {
43 fn from(err: $system_err) -> Self {
44 Self::System(err)
45 }
46 }
47 };
48}
49
50#[derive(Debug, Eq, PartialEq, derive_more::Display)]
52pub enum ActorSystemError<A, S> {
53 Actor(A),
54 System(S),
55}
56
57impl<A, S> ActorSystemError<A, S> {
58 pub fn map_actor<F, U>(self, f: F) -> ActorSystemError<U, S>
60 where
61 F: FnOnce(A) -> U,
62 {
63 match self {
64 Self::Actor(a) => ActorSystemError::Actor(f(a)),
65 Self::System(s) => ActorSystemError::System(s),
66 }
67 }
68
69 pub fn map_system<F, U>(self, f: F) -> ActorSystemError<A, U>
71 where
72 F: FnOnce(S) -> U,
73 {
74 match self {
75 Self::Actor(a) => ActorSystemError::Actor(a),
76 Self::System(s) => ActorSystemError::System(f(s)),
77 }
78 }
79
80 pub fn map_into<UA, US>(self) -> ActorSystemError<UA, US>
82 where
83 UA: From<A>,
84 US: From<S>,
85 {
86 match self {
87 Self::Actor(a) => ActorSystemError::Actor(UA::from(a)),
88 Self::System(s) => ActorSystemError::System(US::from(s)),
89 }
90 }
91}
92
93pub trait ResultExt<T, A, S> {
95 fn map_actor_err<F, U>(self, f: F) -> Result<T, ActorSystemError<U, S>>
97 where
98 F: FnOnce(A) -> U;
99
100 fn map_system_err<F, U>(self, f: F) -> Result<T, ActorSystemError<A, U>>
102 where
103 F: FnOnce(S) -> U;
104
105 fn map_err_into<UA, US>(self) -> Result<T, ActorSystemError<UA, US>>
107 where
108 UA: From<A>,
109 US: From<S>;
110}
111
112impl<T, A, S> ResultExt<T, A, S> for Result<T, ActorSystemError<A, S>> {
113 fn map_actor_err<F, U>(self, f: F) -> Result<T, ActorSystemError<U, S>>
114 where
115 F: FnOnce(A) -> U,
116 {
117 self.map_err(|err| err.map_actor(f))
118 }
119
120 fn map_system_err<F, U>(self, f: F) -> Result<T, ActorSystemError<A, U>>
121 where
122 F: FnOnce(S) -> U,
123 {
124 self.map_err(|err| err.map_system(f))
125 }
126
127 fn map_err_into<UA, US>(self) -> Result<T, ActorSystemError<UA, US>>
128 where
129 UA: From<A>,
130 US: From<S>,
131 {
132 self.map_err(ActorSystemError::map_into)
133 }
134}