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
//! Traits for interoperation of Boolean and sum types.
//!
//! Fool provides extension traits for `bool`, `Option`, and `Result` types.
//! These traits enable fluent conversions and expressions.
//!
//! # Examples
//!
//! Using `and_if` to produce an `Option` if a predicate succeeds:
//!
//! ```rust
//! use fool::prelude::*;
//!
//! pub fn get<T>() -> Option<T> {
//!     // ...
//!     # None
//! }
//!
//! if let Some(value) = get::<u32>().and_if(|value| *value > 10) {
//!     // ...
//! }
//! ```
//!
//! Using `some_with` to produce an `Option` based on a Boolean expression:
//!
//! ```rust
//! use fool::prelude::*;
//! use std::collections::HashSet;
//!
//! let mut set = HashSet::new();
//! set.insert(10u32);
//!
//! let message = set.contains(&10u32).some_with(|| "Contains 10!".to_owned());
//! ```

#![no_std]

#[allow(unused_imports)]
#[cfg(feature = "std")]
#[macro_use]
extern crate std;

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

    fn some<T>(self, value: T) -> Option<T>;

    fn some_with<T, F>(self, f: F) -> Option<T>
    where
        F: Fn() -> T;

    fn ok_or<T, E>(self, value: T, error: E) -> Result<T, E>;

    fn ok_or_else<F, G, T, E>(self, f: F, g: G) -> Result<T, E>
    where
        F: Fn() -> T,
        G: Fn() -> E;
}

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

    fn some<T>(self, value: T) -> Option<T> {
        if self {
            Some(value)
        }
        else {
            None
        }
    }

    fn some_with<T, F>(self, f: F) -> Option<T>
    where
        F: Fn() -> T,
    {
        if self {
            Some(f())
        }
        else {
            None
        }
    }

    fn ok_or<T, E>(self, value: T, error: E) -> Result<T, E> {
        if self {
            Ok(value)
        }
        else {
            Err(error)
        }
    }

    fn ok_or_else<F, G, T, E>(self, f: F, g: G) -> Result<T, E>
    where
        F: Fn() -> T,
        G: Fn() -> E,
    {
        if self {
            Ok(f())
        }
        else {
            Err(g())
        }
    }
}

pub trait OptionExt<T> {
    fn and_if<F>(self, f: F) -> Self
    where
        F: Fn(&T) -> bool;
}

impl<T> OptionExt<T> for Option<T> {
    fn and_if<F>(mut self, f: F) -> Self
    where
        F: Fn(&T) -> bool,
    {
        match self.take() {
            Some(value) => {
                if f(&value) {
                    Some(value)
                }
                else {
                    None
                }
            }
            _ => None,
        }
    }
}

pub mod prelude {
    pub use super::*;
}