devela/code/result/own/state.rs
1// devela::code::result::own::state
2//
3//! Methods .
4//
5//
6
7use crate::{iif, Debug, Own};
8
9/* Result<S, E> */
10
11/// # Additional methods for when the `state` is a `Result`.
12impl<S, E, V> Own<Result<S, E>, V> {
13 /* map (4) */
14
15 /// Maps a `Result<S>` to a `Result<T>` by applying the `op` function
16 /// to a contained [`Ok`] value, leaving an [`Err`] value untouched.
17 #[rustfmt::skip]
18 pub fn s_map_ok<T, F: FnOnce(S) -> T>(self, op: F) -> Own<Result<T, E>, V> {
19 Own::new(self.s.map(op), self.v)
20 }
21
22 /// Maps a `Result<S, E>` to a `Result<S, F>` by applying the `op` function
23 /// to a contained [`Err`] value, leaving an [`Ok`] value untouched.
24 #[rustfmt::skip]
25 pub fn s_map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Own<Result<S, F>, V> {
26 Own::new(self.s.map_err(op), self.v)
27 }
28
29 /// Returns `res` if the state is [`Ok`], otherwise returns the [`Err`] value of `self`.
30 pub fn s_and<T>(self, res: Result<T, E>) -> Own<Result<T, E>, V> {
31 Own::new(self.s.and(res), self.v)
32 }
33
34 /// Calls `op` if the state is [`Ok`], otherwise returns the [`Err`] value of `self`.
35 #[rustfmt::skip]
36 pub fn s_and_then<T, F: FnOnce(S) -> Result<T, E>>(self, op: F) -> Own<Result<T, E>, V> {
37 Own::new(self.s.and_then(op), self.v)
38 }
39
40 /* assert (4) */
41
42 /// Asserts the `state` is [`Ok`] and returns `self`, otherwise panics.
43 /// # Panics
44 /// Panics if the state is `Err`.
45 pub const fn s_assert_ok(self) -> Self {
46 iif![let Ok(_) = self.s; self; panic![]]
47 }
48
49 /// Asserts the `state` is [`Ok`] and returns `self`, otherwise panics with `message`.
50 /// # Panics
51 /// Panics if the `state` is `Err`.
52 pub const fn s_assert_ok_or(self, message: &'static str) -> Self {
53 iif![let Ok(_) = self.s; self; panic!["{}", message]]
54 }
55
56 /// Asserts the `state` is [`Err`] and returns `self`, otherwise panics.
57 /// # Panics
58 /// Panics if the `state` is `Ok`.
59 pub const fn s_assert_err(self) -> Self {
60 iif![let Err(_) = self.s; self; panic![]]
61 }
62 /// Asserts the `state` is [`Err`] and returns `self`, otherwise panics with `message`.
63 /// # Panics
64 /// Panics if the `state` is `Ok`.
65 pub const fn s_assert_err_or(self, message: &'static str) -> Self {
66 iif![let Err(_) = self.s; self; panic!["{}", message]]
67 }
68
69 /* unwrap (3) */
70
71 /// Unwraps the contained `Ok(state)` or panics.
72 ///
73 /// # Panics
74 /// Panics if the state is `Err`.
75 pub fn s_unwrap(self) -> Own<S, V> {
76 iif![let Ok(s) = self.s; Own::new(s, self.v); panic![]]
77 }
78
79 /// Unwraps the contained `Ok(state)` or provides a `default`.
80 ///
81 /// # Panics
82 /// Panics if the state is `Err`.
83 pub fn s_unwrap_or(self, default: S) -> Own<S, V> {
84 Own::new(self.s.unwrap_or(default), self.v)
85 }
86
87 /// Unwraps the contained `Ok(state)` or panics with a `message`.
88 ///
89 /// # Panics
90 /// Panics if the state is `Err`.
91 #[rustfmt::skip]
92 pub fn s_expect(self, message: &str) -> Own<S, V> where E: Debug {
93 Own::new(self.s.expect(message), self.v)
94 }
95}
96
97/// # *const* methods for when everything is `Copy` and the `state` is a `Result`.
98impl<S: Copy, E: Copy, V: Copy> Own<Result<S, E>, V> {
99 /* unwrap (3) */
100
101 /// Unwraps the contained `Ok(state)` or panics.
102 /// # Panics
103 /// Panics if the state is `Err`.
104 pub const fn s_const_unwrap(self) -> Own<S, V> {
105 iif![let Ok(s) = self.s; Own::new(s, self.v); panic![]]
106 }
107
108 /// Unwraps the contained `Ok(state)` or provides a `default`.
109 pub const fn s_const_unwrap_or(self, default: S) -> Own<S, V> {
110 iif![let Ok(s) = self.s; Own::new(s, self.v); Own::new(default, self.v)]
111 }
112
113 /// Unwraps the contained `Ok(state)` or panics with the given `message`.
114 /// # Panics
115 /// Panics if the state is `Err`.
116 pub const fn s_const_expect(self, message: &'static str) -> Own<S, V> {
117 iif![let Ok(s) = self.s; Own::new(s, self.v); panic!["{}", message]]
118 }
119}
120
121/* Option<S> */
122
123/// # Additional methods for when the `value` field is an `Option`.
124impl<S, V> Own<Option<S>, V> {
125 /* map (4) */
126
127 /// Maps an `Option<S>` to an `Option<T>` by applying the `op` function
128 /// to a contained state (if `Some`), or returns `None` (if `None`).
129 #[rustfmt::skip]
130 pub fn s_map_some<T, F: FnOnce(S) -> T>(self, op: F) -> Own<Option<T>, V> {
131 Own::new(self.s.map(op), self.v)
132 }
133
134 /// Returns [`None`] if the state is `None`, otherwise returns `optb`.
135 pub fn s_and<T>(self, res: Option<T>) -> Own<Option<T>, V> {
136 Own::new(self.s.and(res), self.v)
137 }
138
139 /// Returns [`None`] if the state is `None`,
140 /// otherwise calls `op` with the wrapped state and returns the result.
141 #[rustfmt::skip]
142 pub fn s_and_then<T, F: FnOnce(S) -> Option<T>>(self, op: F) -> Own<Option<T>, V> {
143 Own::new(self.s.and_then(op), self.v)
144 }
145
146 /* assert (4) */
147
148 /// Asserts the state is [`Some`] and returns `self`, otherwise panics.
149 /// # Panics
150 /// Panics if the state is `None`.
151 pub const fn s_assert_some(self) -> Self {
152 iif![let Some(_) = self.s; self; panic![]]
153 }
154
155 /// Asserts the state is [`Some`] and returns `self`, otherwise panics with `message`.
156 /// # Panics
157 /// Panics if the state is `None`.
158 pub const fn s_assert_some_or(self, message: &'static str) -> Self {
159 iif![let Some(_) = self.s; self; panic!["{}", message]]
160 }
161
162 /// Asserts the state is [`None`] and returns `self`, otherwise panics.
163 /// # Panics
164 /// Panics if the state is `Some`.
165 pub const fn s_assert_none(self) -> Self {
166 iif![let None = self.s; self; panic![]]
167 }
168
169 /// Asserts the state is [`None`] and returns `self`, otherwise panics with `message`.
170 ///
171 /// # Panics
172 /// Panics if the state is `Some`.
173 pub const fn s_assert_none_or(self, message: &'static str) -> Self {
174 iif![let None = self.s; self; panic!["{}", message]]
175 }
176
177 /* unwrap (3) */
178
179 /// Unwraps the contained `Some(state)` or panics.
180 /// # Panics
181 /// Panics if the state is `None`.
182 pub fn s_unwrap(self) -> Own<S, V> {
183 Own::new(self.s.unwrap(), self.v)
184 }
185
186 /// Unwraps the contained `Some(state)` or provides a `default`.
187 pub fn s_unwrap_or(self, default: S) -> Own<S, V> {
188 Own::new(self.s.unwrap_or(default), self.v)
189 }
190
191 /// Unwraps the contained `Some(state)` or panics with the given `message`.
192 /// # Panics
193 /// Panics if the state is `None`.
194 pub fn s_expect(self, message: &str) -> Own<S, V> {
195 Own::new(self.s.expect(message), self.v)
196 }
197}
198
199/// # *const* methods for when everything is `Copy` and the `value` is an `Option`.
200impl<S: Copy, V: Copy> Own<Option<S>, V> {
201 /* unwrap (3) */
202
203 /// Unwraps the contained `Some(state)` or panics.
204 /// # Panics
205 /// Panics if the state is `None`.
206 pub const fn s_const_unwrap(self) -> Own<S, V> {
207 iif![let Some(s) = self.s; Own::new(s, self.v); panic![]]
208 }
209
210 /// Unwraps the contained `Some(state)` or provides a `default`.
211 pub const fn s_const_unwrap_or(self, default: S) -> Own<S, V> {
212 iif![let Some(s) = self.s; Own::new(s, self.v); Own::new(default, self.v)]
213 }
214
215 /// Unwraps the contained `Some(state)` or panics with the given `message`.
216 /// # Panics
217 /// Panics if the state is `None`.
218 pub const fn s_const_expect(self, message: &'static str) -> Own<S, V> {
219 iif![let Some(s) = self.s; Own::new(s, self.v); panic!["{}", message]]
220 }
221}