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