devela/code/result/own/
value.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// devela::code::result::own::value
//
//!
//

use crate::{iif, Debug, Own};

/// # Additional methods for when the `value` field is a `Result`.
impl<S, V, E> Own<S, Result<V, E>> {
    /* map (4) */

    /// Maps a `Result<V>` to a `Result<W>` by applying the `op` function
    /// to a contained [`Ok`] value, leaving an [`Err`] value untouched.
    pub fn v_map_ok<W, F: FnOnce(V) -> W>(self, op: F) -> Own<S, Result<W, E>> {
        Own::new(self.s, self.v.map(op))
    }

    /// Maps a `Result<V, E>` to a `Result<V, F>` by applying the `op` function
    /// to a contained [`Err`] value, leaving an [`Ok`] value untouched.
    pub fn v_map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Own<S, Result<V, F>> {
        Own::new(self.s, self.v.map_err(op))
    }

    /// Returns `res` if the result is [`Ok`], otherwise returns the [`Err`] value of `self`.
    pub fn v_and<W>(self, res: Result<W, E>) -> Own<S, Result<W, E>> {
        Own::new(self.s, self.v.and(res))
    }

    /// Calls `op` if the result is [`Ok`], otherwise returns the [`Err`] value of `self`.
    pub fn v_and_then<W, F: FnOnce(V) -> Result<W, E>>(self, op: F) -> Own<S, Result<W, E>> {
        Own::new(self.s, self.v.and_then(op))
    }

    /* assert (4) */

    /// Asserts the `value` is [`Ok`] and returns `self`, otherwise panics.
    /// # Panics
    /// Panics if the `value` is `Err`.
    pub const fn v_assert_ok(self) -> Self {
        iif![let Ok(_) = self.v; self; panic![]]
    }

    /// Asserts the `value` is [`Ok`] and returns `self`, otherwise panics with `message`.
    /// # Panics
    /// Panics if the `value` is `Err`.
    pub const fn v_assert_ok_or(self, message: &'static str) -> Self {
        iif![let Ok(_) = self.v; self; panic!["{}", message]]
    }

    /// Asserts the `value` is [`Err`] and returns `self`, otherwise panics.
    /// # Panics
    /// Panics if the `value` is `Ok`.
    pub const fn v_assert_err(self) -> Self {
        iif![let Err(_) = self.v; self; panic![]]
    }
    /// Asserts the `value` is [`Err`] and returns `self`, otherwise panics with `message`.
    ///
    /// # Panics
    /// Panics if the `value` is `Ok`.
    pub const fn v_assert_err_or(self, message: &'static str) -> Self {
        iif![let Err(_) = self.v; self; panic!["{}", message]]
    }

    /* unwrap (3) */

    /// Unwraps the contained `Ok(value)` or panics.
    /// # Panics
    /// Panics if the value is `Err`.
    pub fn v_unwrap(self) -> Own<S, V> {
        iif![let Ok(v) = self.v; Own::new(self.s, v); panic![]]
    }

    /// Unwraps the contained `Ok(value)` or provides a `default`.
    pub fn v_unwrap_or(self, default: V) -> Own<S, V> {
        Own::new(self.s, self.v.unwrap_or(default))
    }

    /// Unwraps the contained `Ok(value)` or panics with a `message`.
    /// # Panics
    /// Panics if the value is `Err`.
    #[rustfmt::skip]
    pub fn v_expect(self, message: &str) -> Own<S, V> where E: Debug {
        Own::new(self.s, self.v.expect(message))
    }
}

/// # *const* methods for when everything is `Copy` and the `value` is a `Result`.
impl<S: Copy, V: Copy, E: Copy> Own<S, Result<V, E>> {
    /* unwrap (3) */

    /// Unwraps the contained `Ok(value)` or panics.
    ///
    /// # Panics
    /// Panics if the value is `Err`.
    pub const fn v_const_unwrap(self) -> Own<S, V> {
        iif![let Ok(v) = self.v; Own::new(self.s, v); panic![]]
    }

    /// Unwraps the contained `Ok(value)` or provides a `default`.
    pub const fn v_const_unwrap_or(self, default: V) -> Own<S, V> {
        iif![let Ok(v) = self.v; Own::new(self.s, v); Own::new(self.s, default)]
    }

    /// Unwraps the contained `Ok(value)` or panics with the given `message`.
    ///
    /// # Panics
    /// Panics if the value is `Err`.
    pub const fn v_const_expect_const(self, message: &'static str) -> Own<S, V> {
        iif![let Ok(v) = self.v; Own::new(self.s, v); panic!["{}", message]]
    }
}

/// # Additional methods for when the `value` field is an `Option`.
impl<S, V> Own<S, Option<V>> {
    /* map */

    /// Maps an `Option<V>` to an `Option<W>` by applying the `op` function
    /// to a contained value (if `Some`), or returns `None` (if `None`).
    pub fn v_map_some<W, F: FnOnce(V) -> W>(self, op: F) -> Own<S, Option<W>> {
        Own::new(self.s, self.v.map(op))
    }

    /// Returns [`None`] if the value is `None`,
    /// otherwise returns `optb`.
    pub fn v_and<W>(self, optb: Option<W>) -> Own<S, Option<W>> {
        Own::new(self.s, self.v.and(optb))
    }

    /// Returns [`None`] if the value is `None`,
    /// otherwise calls `op` with the wrapped value and returns the result.
    pub fn v_and_then<W, F: FnOnce(V) -> Option<W>>(self, op: F) -> Own<S, Option<W>> {
        Own::new(self.s, self.v.and_then(op))
    }

    /* assert (4) */

    /// Asserts the value is [`Some`] and returns `self`, otherwise panics.
    /// # Panics
    /// Panics if the value is `None`.
    pub const fn v_assert_some(self) -> Self {
        iif![let Some(_) = self.v; self; panic![]]
    }

    /// Asserts the value is [`Some`] and returns `self`, otherwise panics with `message`.
    /// # Panics
    /// Panics if the value is `None`.
    pub const fn v_assert_some_or(self, message: &'static str) -> Self {
        iif![let Some(_) = self.v; self; panic!["{}", message]]
    }

    /// Asserts the value is [`None`] and returns `self`, otherwise panics.
    /// # Panics
    /// Panics if the value is `Some`.
    pub const fn v_assert_none(self) -> Self {
        iif![let None = self.v; self; panic![]]
    }

    /// Asserts the value is [`None`] and returns `self`, otherwise panics with `message`.
    ///
    /// # Panics
    /// Panics if the value is `Some`.
    pub const fn v_assert_none_or(self, message: &'static str) -> Self {
        iif![let None = self.v; self; panic!["{}", message]]
    }

    /* unwrap (3) */

    /// Unwraps the contained `Some(value)` or panics.
    /// # Panics
    /// Panics if the value is `None`.
    pub fn v_unwrap(self) -> Own<S, V> {
        Own::new(self.s, self.v.unwrap())
    }

    /// Unwraps the contained `Some(value)` or provides a `default`.
    pub fn v_unwrap_or(self, default: V) -> Own<S, V> {
        Own::new(self.s, self.v.unwrap_or(default))
    }

    /// Unwraps the contained `Some(value)` or panics with the given `message`.
    /// # Panics
    /// Panics if the value is `None`.
    pub fn v_expect(self, message: &str) -> Own<S, V> {
        Own::new(self.s, self.v.expect(message))
    }
}

/// # *const* methods for when everything is `Copy` and the `value` is an `Option`.
impl<S: Copy, V: Copy> Own<S, Option<V>> {
    /* unwrap (3) */

    /// Unwraps the contained `Some(value)` or panics.
    ///
    /// # Panics
    /// Panics if the value is `None`.
    pub const fn v_const_unwrap(self) -> Own<S, V> {
        iif![let Some(v) = self.v; Own::new(self.s, v); panic![]]
    }

    /// Unwraps the contained `Some(value)` or provides a `default`.
    pub const fn v_const_unwrap_or(self, default: V) -> Own<S, V> {
        iif![let Some(v) = self.v; Own::new(self.s, v); Own::new(self.s, default)]
    }

    /// Unwraps the contained `Some(value)` or panics with the given `message`.
    ///
    /// # Panics
    /// Panics if the value is `None`.
    pub const fn v_const_expect(self, message: &'static str) -> Own<S, V> {
        iif![let Some(v) = self.v; Own::new(self.s, v); panic!["{}", message]]
    }
}