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
|
/// Represents the result of parsing or looking up a value.
///
/// This enum is generic over the type being parsed. It models four possible outcomes:
/// - [`Unparsed`](PickerResult::Unparsed): The value has not yet been parsed (default).
/// - [`Parsed`](PickerResult::Parsed): The value was successfully parsed into `Type`.
/// - [`NotFound`](PickerResult::NotFound): The requested value could not be found.
/// - [`ParseError`](PickerResult::ParseError): The input could not be parsed due to a format error.
#[derive(Default)]
pub enum PickerResult<Type> {
/// The value has not yet been parsed (default).
#[default]
Unparsed,
/// The value was successfully parsed into `Type`.
Parsed(Type),
/// The requested value could not be found.
NotFound,
/// The input could not be parsed due to a format error.
ParseError,
}
impl<Type, E> From<Result<Type, E>> for PickerResult<Type> {
/// Converts a `Result<Type, E>` into a `PickerResult<Type>`.
///
/// - `Ok(value)` maps to [`Parsed(value)`](PickerResult::Parsed).
/// - `Err(_)` maps to [`ParseError`](PickerResult::ParseError).
fn from(result: Result<Type, E>) -> Self {
match result {
Ok(value) => PickerResult::Parsed(value),
Err(_) => PickerResult::ParseError,
}
}
}
impl<Type> From<Option<Type>> for PickerResult<Type> {
/// Converts an `Option<Type>` into a `PickerResult<Type>`.
///
/// - `Some(value)` maps to [`Parsed(value)`](PickerResult::Parsed).
/// - `None` maps to [`NotFound`](PickerResult::NotFound).
fn from(option: Option<Type>) -> Self {
match option {
Some(value) => PickerResult::Parsed(value),
None => PickerResult::NotFound,
}
}
}
impl<Type> PickerResult<Type> {
/// Returns `true` if the result is [`Parsed`](PickerResult::Parsed).
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert!(result.is_parsed());
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// assert!(!result.is_parsed());
/// ```
pub fn is_parsed(&self) -> bool {
matches!(self, PickerResult::Parsed(_))
}
/// Returns `true` if the result is [`Parsed`](PickerResult::Parsed) or [`NotFound`](PickerResult::NotFound).
/// i.e., the value exists (was either found or not yet parsed, but not a parse error).
/// Typically indicates the value was "found" in some sense.
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert!(result.is_found());
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// assert!(result.is_found());
///
/// let result: PickerResult<i32> = PickerResult::ParseError;
/// assert!(!result.is_found());
/// ```
pub fn is_found(&self) -> bool {
matches!(self, PickerResult::Parsed(_) | PickerResult::NotFound)
}
/// Returns `true` if the result is [`ParseError`](PickerResult::ParseError).
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::ParseError;
/// assert!(result.is_err());
///
/// let result: PickerResult<i32> = PickerResult::Parsed(10);
/// assert!(!result.is_err());
/// ```
pub fn is_err(&self) -> bool {
matches!(self, PickerResult::ParseError)
}
/// Returns `Some(&Type)` if [`Parsed`](PickerResult::Parsed), otherwise `None`.
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert_eq!(result.parsed(), Some(&42));
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// assert_eq!(result.parsed(), None);
/// ```
pub fn parsed(&self) -> Option<&Type> {
if let PickerResult::Parsed(value) = self {
Some(value)
} else {
None
}
}
/// Returns the contained [`Parsed`](PickerResult::Parsed) value or panics with a given message.
///
/// # Panics
/// Panics if the value is not [`Parsed`](PickerResult::Parsed), with a message including the provided `msg`.
///
/// # Examples
///
/// ```should_panic
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// result.expect("expected a parsed value");
/// ```
pub fn expect(self, msg: &str) -> Type {
match self {
PickerResult::Parsed(value) => value,
_ => panic!("{}", msg),
}
}
/// Returns the contained [`Parsed`](PickerResult::Parsed) value or panics.
///
/// # Panics
/// Panics if the value is not [`Parsed`](PickerResult::Parsed).
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert_eq!(result.unwrap(), 42);
/// ```
///
/// ```should_panic
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// result.unwrap();
/// ```
pub fn unwrap(self) -> Type {
match self {
PickerResult::Parsed(value) => value,
PickerResult::Unparsed => {
panic!("called `PickerResult::unwrap()` on an `Unparsed` value")
}
PickerResult::NotFound => {
panic!("called `PickerResult::unwrap()` on a `NotFound` value")
}
PickerResult::ParseError => {
panic!("called `PickerResult::unwrap()` on a `ParseError` value")
}
}
}
/// Returns the contained [`Parsed`](PickerResult::Parsed) value or a provided `default`.
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert_eq!(result.unwrap_or(0), 42);
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// assert_eq!(result.unwrap_or(0), 0);
/// ```
pub fn unwrap_or(self, default: Type) -> Type {
match self {
PickerResult::Parsed(value) => value,
_ => default,
}
}
/// Returns the contained [`Parsed`](PickerResult::Parsed) value or computes it from a closure.
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert_eq!(result.unwrap_or_else(|| 0), 42);
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// assert_eq!(result.unwrap_or_else(|| 0), 0);
/// ```
pub fn unwrap_or_else<F: FnOnce() -> Type>(self, f: F) -> Type {
match self {
PickerResult::Parsed(value) => value,
_ => f(),
}
}
/// Returns the contained [`Parsed`](PickerResult::Parsed) value or the default value of `Type`.
///
/// # Examples
///
/// ```
/// use mingling_picker::PickerResult;
///
/// let result: PickerResult<i32> = PickerResult::Parsed(42);
/// assert_eq!(result.unwrap_or_default(), 42);
///
/// let result: PickerResult<i32> = PickerResult::NotFound;
/// assert_eq!(result.unwrap_or_default(), 0);
/// ```
pub fn unwrap_or_default(self) -> Type
where
Type: Default,
{
match self {
PickerResult::Parsed(value) => value,
_ => Type::default(),
}
}
}
|