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
//! # Overview
//!
//! ```
//! # use not_found_error::{NotFoundError, Require};
//! // Convert Option<i32> to Result<i32, NotFoundError<i32>>
//!
//! assert_eq!(Some(10).require(), Ok(10));
//!
//! assert_eq!(None.require(), Err(NotFoundError::<i32>::new()));
//! ```
//!
//! This crate provides a generic `NotFoundError<T>` type and associated
//! utilities for handling "not found" scenarios in a type-safe and ergonomic manner.
//!
//! You can convert `Option<T>` to `Result<T, NotFoundError<T>` using [`require`](require) function or [`Require`](Require) extension trait.
//!
//! You can convert `Option<T>` to `Result<T, NotFoundError<AnotherType>` using [`not_found`](not_found) function or [`OkOrNotFound`](OkOrNotFound) extension trait.
//!
//! # Features
//!
//! * [x] Generic `NotFoundError<T>` type
//! * [x] Conversion functions and traits to transform `Option<T>` into `Result<T, NotFoundError<T>>`
//! * [x] Conversion functions and traits to transform `Option<T>` into `Result<T, NotFoundError<AnotherType>>`
//!
//! # Examples
//!
//! ```
//! use not_found_error::{NotFoundError, Require, locate, require};
//!
//! // Using the `require` function
//! let item = require([1, 2, 3].into_iter().next());
//! assert_eq!(item, Ok(1));
//!
//! // Using the `require` function
//! let item = require([].into_iter().next());
//! assert_eq!(item, Err(NotFoundError::<i32>::new()));
//!
//! // Using the `require` extension method
//! let item = [1, 2, 3].into_iter().next().require();
//! assert_eq!(item, Ok(1));
//!
//! // Using the `require` extension method
//! let item = [].into_iter().next().require();
//! assert_eq!(item, Err(NotFoundError::<i32>::new()));
//!
//! // Try to find a number greater than 10 (which doesn't exist in our list)
//! let numbers = &[1, 2, 3];
//! let result = locate(numbers, |&&n| n == 0);
//! assert_eq!(result, Err(NotFoundError::new()));
//! ```
use type_name;
use Error;
use ;
use PhantomData;
/// Represents an error indicating that a value was not found.
///
/// This struct is generic over the type `T` that was not found.
///
/// # Examples
///
/// ```
/// use not_found_error::NotFoundError;
///
/// let error: NotFoundError<i32> = NotFoundError::new();
/// assert_eq!(error.to_string(), "i32 not found");
/// ```
;
/// A type alias for `Result<T, NotFoundError<T>>`
pub type Result<T> = Result;
/// Converts `Option<T>` to `Result<T, NotFoundError<T>>`
///
/// # Examples
///
/// ```
/// # use not_found_error::require;
/// # let items = [0, 1, 2];
/// let item = require(items.first());
/// ```
///
/// # See also
///
/// - [`Require`]: Trait for converting `Option<T>` to `Result<T, NotFoundError<T>>`
/// - [`OkOrNotFound`]: Trait for converting `Option<T>` to `Result<T, NotFoundError<AnotherType>>`
/// A shorter version of `NotFoundError::new()`.
///
/// Useful in places where you need to convert `Option<T>` into `Result<T, NotFoundError<AnotherType>>` (notice that `T != AnotherType`).
///
/// # Examples
///
/// ```
/// # use std::path::Path;
/// # use not_found_error::{not_found, NotFoundError};
/// # pub struct WorkspaceRoot;
/// pub fn get_root(path: &Path) -> Result<&Path, NotFoundError<WorkspaceRoot>> {
/// find_root(path).ok_or(not_found())
/// }
/// # pub fn find_root(path: &Path) -> Option<&Path> { todo!() }
/// ```
///
/// # See also
///
/// - [`require`]: Function to convert `Option<T>` to `Result<T, NotFoundError<T>>`
/// - [`OkOrNotFound`]: Trait for converting `Option<T>` to `Result<T, NotFoundError<AnotherType>>`
/// An extension trait for `Option<T>` to convert it to `Result<T, NotFoundError<T>>`
///
/// # Examples
///
/// ```
/// # use not_found_error::Require;
/// # let items = [0, 1, 2];
/// let item = items.first().require();
/// ```
///
/// # See also
///
/// - [`require`]: Function to convert `Option<T>` to `Result<T, NotFoundError<T>>`
/// - [`OkOrNotFound`]: Trait for converting `Option<T>` to `Result<T, NotFoundError<AnotherType>>`
/// An extension trait for `Option<T>` to convert it to `Result<T, NotFoundError<AnotherType>>`
///
/// Useful in places where you need `NotFoundError<AnotherType>` instead of `NotFoundError<T>`.
///
/// # Examples
///
/// ```
/// # use std::path::Path;
/// # use not_found_error::{NotFoundError, OkOrNotFound};
/// # pub struct WorkspaceRoot;
/// pub fn get_root(path: &Path) -> Result<&Path, NotFoundError<WorkspaceRoot>> {
/// find_root(path).ok_or_not_found()
/// }
/// # pub fn find_root(path: &Path) -> Option<&Path> { todo!() }
/// ```
///
/// # See also
///
/// - [`Require`]: Trait for converting `Option<T>` to `Result<T, NotFoundError<T>>`
/// - [`require`]: Function to convert `Option<T>` to `Result<T, NotFoundError<T>>`
/// Searches an iterator for an element that satisfies a given predicate and returns a reference to it.
///
/// This function is different from [`Iterator::find`] because it returns `Result<&T, NotFoundError<&T>>` (not `Option<&T>`).
///
/// # Examples
///
/// ```
/// # use not_found_error::{locate, NotFoundError};
///
/// let numbers = &[1, 2, 3, 4, 5];
///
/// // Find the first even number
/// let result = locate(numbers, |&&n| n % 2 == 0);
/// assert_eq!(result, Ok(&2));
///
/// // Try to find a number greater than 10 (which doesn't exist in our list)
/// let result = locate(numbers, |&&n| n > 10);
/// assert_eq!(result, Err(NotFoundError::new()));
/// ```