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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! Traits for interoperation of `bool` and the [`Option`] and [`Result`] sum
//! types.
//!
//! Fool provides extension traits for `bool` and sum types as well as a Boolean
//! coercion trait for [`Option`] and [`Result`]. [`BoolExt`] enables fluent
//! conversion from `bool` into sum types. [`IntoBool`] enables compound Boolean
//! predicates using `bool`, [`Option`], and [`Result`] types with implicit
//! conversion.
//!
//! Versions in the `0.0.*` series are experimental and unstable.
//!
//! # Generalized Zipping
//!
//! In Rust 1.46, the [`Option::zip`] API was stabilized. Fool provides
//! [`OptionExt::zip_ext`] for prior versions of Rust as well as a generalized
//! `zip` function when the `zip` feature is enabled. The `zip` function accepts
//! a tuple of up to six [`Option`]s and collapses them into a single [`Option`]
//! if all of the inputs are `Some`.
//!
//! # Collisions with Proposed `core` and `std` APIs
//!
//! Some extension methods in [`BoolExt`] and [`OptionExt`] mirror unstable APIs
//! and APIs added to `core` and `std` in subsequent releases of Rust. Fool
//! avoids collisions with these APIs using a `_ext` postfix, but the bare names
//! of these APIs can be included with the `bare` feature.
//!
//! Use of bare extension methods can cause errors after upgrading to a Rust
//! toolchain that includes these APIs, because the invocation may become
//! ambiguous. For example, [`BoolExt::then_some_ext`] mirrors
//! `bool::then_some`, which has not yet become a stable API at the time of this
//! writing.
//!
//! # Examples
//!
//! Using [`then_ext`][`BoolExt::then_ext`] (or `then` with the `bare` feature
//! flag) to create an [`Option`] from a Boolean expression:
//!
//! ```rust
//! use fool::BoolExt;
//! use std::collections::HashSet;
//!
//! let mut set = HashSet::new();
//! set.insert(10u32);
//!
//! let message = set.contains(&10u32).then_ext(|| "Contains 10!".to_owned());
//! ```
//!
//! Using [`ok_or_else`][`BoolExt::ok_or_else`] and the try operator `?` to
//! return errors:
//!
//! ```rust
//! use fool::BoolExt;
//!
//! struct Door {
//!     is_open: bool,
//! }
//!
//! impl Door {
//!     pub fn close(&mut self) -> Result<(), ()> {
//!         self.is_open().ok_or_else(|| ())?;
//!         self.is_open = false;
//!         Ok(())
//!     }
//!
//!     pub fn is_open(&self) -> bool {
//!         self.is_open
//!     }
//! }
//! ```
//!
//! [`Option`]: core::option::Option
//! [`Result`]: core::result::Result
//! [`BoolExt`]: crate::BoolExt
//! [`BoolExt::then_ext`]: crate::BoolExt::then_ext
//! [`BoolExt::then_some_ext`]: crate::BoolExt::then_some_ext
//! [`OptionExt`]: crate::OptionExt
//! [`OptionExt::zip_ext`]: crate::OptionExt::zip_ext

#![no_std]

mod zip;

#[cfg(feature = "zip")]
use zip::Zip;

pub trait BoolExt: Sized {
    fn into_option(self) -> Option<()>;

    fn into_result(self) -> Result<(), ()>;

    fn and<T>(self, option: Option<T>) -> Option<T> {
        self.into_option().and(option)
    }

    fn and_then<T, F>(self, f: F) -> Option<T>
    where
        F: FnOnce() -> Option<T>,
    {
        self.into_option().and_then(|_| f())
    }

    #[cfg(feature = "bare")]
    fn then<T, F>(self, f: F) -> Option<T>
    where
        F: FnOnce() -> T,
    {
        self.then_ext(f)
    }

    fn then_ext<T, F>(self, f: F) -> Option<T>
    where
        F: FnOnce() -> T,
    {
        self.into_option().map(|_| f())
    }

    #[cfg(feature = "bare")]
    fn then_some<T>(self, value: T) -> Option<T> {
        self.then_some_ext(value)
    }

    fn then_some_ext<T>(self, value: T) -> Option<T> {
        self.then_ext(|| value)
    }

    fn ok_or<E>(self, error: E) -> Result<(), E> {
        self.into_result().map_err(|_| error)
    }

    fn ok_or_else<E, F>(self, f: F) -> Result<(), E>
    where
        F: FnOnce() -> E,
    {
        self.into_result().map_err(|_| f())
    }
}

impl BoolExt for bool {
    fn into_option(self) -> Option<()> {
        if self {
            Some(())
        }
        else {
            None
        }
    }

    fn into_result(self) -> Result<(), ()> {
        if self {
            Ok(())
        }
        else {
            Err(())
        }
    }
}

pub trait IntoBool {
    fn into_bool(self) -> bool;
}

impl IntoBool for bool {
    fn into_bool(self) -> bool {
        self
    }
}

impl<T> IntoBool for Option<T> {
    fn into_bool(self) -> bool {
        self.is_some()
    }
}

impl<'a, T> IntoBool for &'a Option<T> {
    fn into_bool(self) -> bool {
        self.is_some()
    }
}

impl<'a, T> IntoBool for &'a mut Option<T> {
    fn into_bool(self) -> bool {
        self.is_some()
    }
}

impl<T, E> IntoBool for Result<T, E> {
    fn into_bool(self) -> bool {
        self.is_ok()
    }
}

impl<'a, T, E> IntoBool for &'a Result<T, E> {
    fn into_bool(self) -> bool {
        self.is_ok()
    }
}

impl<'a, T, E> IntoBool for &'a mut Result<T, E> {
    fn into_bool(self) -> bool {
        self.is_ok()
    }
}

pub trait OptionExt<T>: Sized {
    #[cfg(feature = "bare")]
    fn zip<U>(self, other: Option<U>) -> Option<(T, U)> {
        self.zip_ext(other)
    }

    fn zip_ext<U>(self, other: Option<U>) -> Option<(T, U)>;
}

impl<T> OptionExt<T> for Option<T> {
    fn zip_ext<U>(self, other: Option<U>) -> Option<(T, U)> {
        // Avoid the complexity of the `Zip` trait and match on the `Option`s.
        match (self, other) {
            (Some(a), Some(b)) => Some((a, b)),
            _ => None,
        }
    }
}

#[cfg(feature = "zip")]
pub fn zip<U>(tuple: U) -> U::Output
where
    U: Zip,
{
    Zip::zip(tuple)
}

#[macro_export]
macro_rules! and {
    ($head:expr, $($tail:expr),*$(,)?) => ({
        use $crate::IntoBool;

        $head.into_bool() $(&& ($tail.into_bool()))*
    });
    (option => $head:expr, $($tail:expr),*$(,)?) => (
        $head$(.and($tail))*
    );
    (result => $head:expr, $($tail:expr),*$(,)?) => (
        $head$(.and($tail))*
    );
}

#[macro_export]
macro_rules! or {
    ($head:expr, $($tail:expr),*$(,)?) => ({
        use $crate::IntoBool;

        $head.into_bool() $(|| ($tail.into_bool()))*
    });
    (option => $head:expr, $($tail:expr),*$(,)?) => (
        $head$(.or($tail))*
    );
    (result => $head:expr, $($tail:expr),*$(,)?) => (
        $head$(.or($tail))*
    );
}

#[macro_export]
macro_rules! xor {
    ($head:expr, $($tail:expr),*$(,)?) => ({
        use $crate::IntoBool;

        $head.into_bool() $(^ ($tail.into_bool()))*
    });
    (option => $head:expr, $($tail:expr),*$(,)?) => (
        $head$(.xor($tail))*
    );
}

#[cfg(test)]
mod tests {
    use crate::{and, or, xor};

    fn some() -> Option<u32> {
        Some(0)
    }

    fn none() -> Option<u32> {
        None
    }

    fn ok() -> Result<u32, ()> {
        Ok(0)
    }

    fn err() -> Result<u32, ()> {
        Err(())
    }

    #[test]
    fn and() {
        assert!(and!(true, true, true));
        assert!(!and!(true, true, false));

        assert!(and!(some(), some(), some()));
        assert!(!and!(some(), some(), none()));

        assert!(and!(ok(), ok(), ok()));
        assert!(!and!(ok(), ok(), err()));
    }

    #[test]
    fn and_option() {
        assert!(and!(option => some(), some(), some()).is_some());
        assert!(and!(option => some(), some(), none()).is_none());
    }

    #[test]
    fn and_result() {
        assert!(and!(result => ok(), ok(), ok()).is_ok());
        assert!(and!(result => ok(), ok(), err()).is_err());
    }

    #[test]
    fn or() {
        assert!(or!(true, false, false));
        assert!(!or!(false, false, false));

        assert!(or!(some(), none(), none()));
        assert!(!or!(none(), none(), none()));

        assert!(or!(ok(), err(), err()));
        assert!(!or!(err(), err(), err()));
    }

    #[test]
    fn or_option() {
        assert!(or!(option => some(), some(), none()).is_some());
        assert!(or!(option => none(), none(), none()).is_none());
    }

    #[test]
    fn or_result() {
        assert!(or!(result => ok(), ok(), err()).is_ok());
        assert!(or!(result => err(), err(), err()).is_err());
    }

    #[test]
    fn xor() {
        assert!(xor!(true, false, false));
        assert!(!xor!(true, true, false));
        assert!(!xor!(false, false, false));

        assert!(xor!(some(), none(), none()));
        assert!(!xor!(some(), some(), none()));
        assert!(!xor!(none(), none(), none()));

        assert!(xor!(ok(), err(), err()));
        assert!(!xor!(ok(), ok(), err()));
        assert!(!xor!(err(), err(), err()));
    }

    #[test]
    fn xor_option() {
        assert!(xor!(option => some(), none(), none()).is_some());
        assert!(xor!(option => some(), some(), none()).is_none());
        assert!(xor!(option => none(), none(), none()).is_none());
    }

    #[cfg(feature = "zip")]
    #[test]
    fn zip_option() {
        use crate::OptionExt;

        let a = Some(0i32);
        let b = Some(1i32);
        let c = Some(2i32);
        let d: Option<i32> = None;

        assert_eq!(Some((0, 1, 2)), crate::zip((a, b, c)));
        assert_eq!(None, crate::zip((a, b, c, d)));
        assert_eq!(Some((0, 1)), OptionExt::zip(a, b));
    }
}