devela/code/result/own/
state.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
213
214
215
216
217
218
219
220
221
// devela::code::result::own::state
//
//! Methods .
//
//

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

/* Result<S, E> */

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

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

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

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

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

    /* assert (4) */

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

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

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

    /* unwrap (3) */

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

    /// Unwraps the contained `Ok(state)` or provides a `default`.
    ///
    /// # Panics
    /// Panics if the state is `Err`.
    pub fn s_unwrap_or(self, default: S) -> Own<S, V> {
        Own::new(self.s.unwrap_or(default), self.v)
    }

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

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

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

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

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

/* Option<S> */

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

    /// Maps an `Option<S>` to an `Option<T>` by applying the `op` function
    /// to a contained state (if `Some`), or returns `None` (if `None`).
    #[rustfmt::skip]
    pub fn s_map_some<T, F: FnOnce(S) -> T>(self, op: F) -> Own<Option<T>, V> {
        Own::new(self.s.map(op), self.v)
    }

    /// Returns [`None`] if the state is `None`, otherwise returns `optb`.
    pub fn s_and<T>(self, res: Option<T>) -> Own<Option<T>, V> {
        Own::new(self.s.and(res), self.v)
    }

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

    /* assert (4) */

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

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

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

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

    /* unwrap (3) */

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

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

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

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

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

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

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