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
//! # The outcome crate //! Type `Outcome` represents a success or failure: Every `Outcome` is either `Success` or `Failure` //! //! ``` //! fn do_something() -> Outcome { //! // ... //! } //! //! // The return value is an outcome //! let result = do_something(); //! //! // Pattern Match //! match result { //! Success => println!("Well done!"), //! Failure => println!("Oh well :("), //! } //! ``` //! //! # Examples //! Using `and_then` on an `Outcome`: //! //! ``` //! // Returns `Failure` //! let result = Outcome::from_bool(false); //! //! match result.and_then(|| Success) { //! Success => println!("Success! :)"), //! Failure => println!("Failure :("), //! } //! ``` //! //! Using `or_none` on an `Outcome` to transform it into an `Option`: //! //! ``` //! let result = Success; //! //! // Encapsulates arg within an option //! match result.or_none("hello!") { //! Some(s) => println!("{}", s), //! None => println!("Nothing here!"), //! } //! ``` use Outcome::{Success, Failure}; #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] pub enum Outcome { /// Successful Success, /// Not successful Failure } impl Outcome { /// Returns `Success` if `good` is `true`, otherwise return `Failure` /// /// # Examples /// /// ``` /// let result = Outcome::from_bool(true); /// /// assert_eq!(result, Success); /// ``` pub fn from_bool(good: bool) -> Outcome { match good { true => Success, false => Failure, } } /// Returns `true` if the outcome is a success /// /// # Examples /// /// ``` /// let result = Success; /// /// assert!(result.is_success()); /// ``` pub fn is_success(&self) -> bool { *self == Success } /// Returns `true` if the outcome is a failure /// /// # Examples /// /// ``` /// let result = Failure; /// /// assert!(result.is_failure()); /// ``` pub fn is_failure(&self) -> bool { !self.is_success() } /// Transforms the `Outcome` into an `Option<T>`, mapping `Success` to `Some(good)` and `Failure` to `None` /// /// # Examples /// /// ``` /// let result = Success; /// /// assert_eq!(result.or_none(42), Option(42)); /// ``` pub fn or_none<T>(self, ok: T) -> Option<T> { match self { Success => Some(ok), Failure => None, } } /// Transforms the `Outcome` into a `Result<T, E>`, mapping `Success` to `Ok(good)` and `Failure` to `Err(err)` /// /// # Examples /// /// ``` /// let result = Failure; /// /// match result.or_err("good", "bad") { /// Ok(success) => println!("{}", success), /// Err(err) => println!("{}", err), /// } /// ``` pub fn or_err<T, E>(self, good: T, err: E) -> Result<T, E> { match self { Success => Ok(good), Failure => Err(err), } } /// Returns `good` if the outcome is `Success`, otherwise panics /// /// # Examples /// /// ``` /// let result = Success; /// /// assert_eq!(result.or_panic(42), 42); /// ``` pub fn or_panic<T>(self, good: T) -> T { match self { Success => good, Failure => panic!("Called `Outcome::or_panic(...)` on a `Failure` value"), } } /// Returns `Failure` if the outcome is `Failure`, otherwise returns `outb` /// /// # Examples /// /// ``` /// let result = Success; /// /// assert_eq!(result.and(Failure), Failure); /// ``` pub fn and(self, outb: Outcome) -> Outcome { match self { Success => outb, Failure => Failure, } } /// Returns `Success` if the outcome is `Success`, otherwise returns `outb` /// /// # Examples /// /// ``` /// let result = Failure; /// /// assert_eq!(result.or(Success), Success); /// ``` pub fn or(self, outb: Outcome) -> Outcome { match self { Success => Success, Failure => outb, } } /// Returns `Failure` if the outcome is `Failure`, otherwise calls `f` and returns result /// /// # Examples /// /// ``` /// let result = Success; /// /// assert_eq!(result.and_then(|| Failure), Failure); /// ``` pub fn and_then<F: FnOnce() -> Outcome>(self, f: F) -> Outcome { match self { Success => f(), Failure => Failure, } } /// Returns `Success` if the outcome is `Success`, otherwise calls `f` and returns result /// /// # Examples /// /// ``` /// let result = Failure; /// /// assert_eq!(result.or_then(|| Success), Success); /// ``` pub fn or_then<F: FnOnce() -> Outcome>(self, f: F) -> Outcome { match self { Success => Success, Failure => f(), } } }