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