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
//! # Errors during interactive picker usage
//! This module contains the custom error type [`PickError`] returned by the
//! [`Picker::pick`](crate::Picker::pick) method, and siblings `Picker::pick_*`. The error type is
//! comprehensive and the individual picker method used may or may not result in the corresponding
//! errors.
//!
//! See the [`PickError`] documentation for more detail.
//!
//! ## Example
//! Convert a [`PickError::UserInterrupted`] silently into no choice, propagating any other error as an IO
//! error. Use with `picker.pick().or_else(suppress_abort)`.
//! ```
//! # use nucleo_picker::error::PickError;
//! # use std::io;
//! fn suppress_abort<D: Default>(err: PickError) -> Result<D, io::Error> {
//! match err {
//! PickError::UserInterrupted => Ok(D::default()),
//! e => Err(e.into()),
//! }
//! }
//! ```
use ;
/// An error which may be returned while running the picker interactively.
///
/// This is marked non-exhaustive since more variants may be added in the future. It is recommended
/// to handle the errors that are relevant to your application and propagate any remaining errors
/// as an [`io::Error`].
///
/// ## Type parameter for `Aborted` variant
/// The [`PickError::Aborted`] variant can be used by the application to propagate errors to the
/// picker; the application-defined error type is the type parameter `A`. By default, `A = !`
/// which means this type of abort will *never occur* and can be ignored during pattern matching.
///
/// This library will never generate an abort error directly. In order to pass errors downstream to
/// the picker, the application can define an abort error type using the
/// [`EventSource::AbortErr`](crate::EventSource::AbortErr) associated type. This associated type
/// is the same as the type parameter here when used in
/// [`Picker::pick_with_io`](crate::Picker::pick_with_io).
///
/// ## Relationship to `io::Error`
/// This error type with the default type parameter is (in spirit) an [`io::Error`], but with
/// more precise variants not present in the default [`io::Error`]. For convenience and
/// (partial) backwards compatibility, there is a `From<PickError> for io::Error` implementation;
/// this propagates the underlying IO error and converts any other error message to an
/// [`io::Error`] using [`io::Error::other`].
///
/// There is also a `From<PickError<io::Error>> for io::Error` to handle the common use-case that
/// the only error type which may occur during standard operation of your application is an IO
/// error; in this case, the conversion maps both the `Aborted(io::Error)` and `IO(io::Error)`
/// versions directly to an `io::Error`.
///
/// Any other abort error type `A` requires manual handling. The [`PickError::factor`] method
/// can be used to unwind non-aborted variants into an `io::Error` and extract the
/// error present in the `Aborted` variant.
// ideally we would like to replace these two conversions with a blanket implementation
// `impl<A: Into<io::Error>> From<PickError<A>> for io::Error`; however, currently there is no
// implementation of `From<!> for T` for a variety of reasons; so we are stuck with doing this for
// maximal compabitility since in the vast majority of cases, `A = !`.