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
use ;
/// Represents generic data and maybe an error
///
/// - Full data when err is empty `OkMaybe(_, None)`.
/// - Maybe partial data when err is present: `OkMaybe(_, Some(_))`.
///
/// ## Why
///
/// When parsing, we want to accumulate as many errors as possible. That means
/// that even partial data may be useful. For example, this code has the same
/// attribute specified twice and also a syntax error that will prevent it from
/// parsing additional attributes:
///
/// ```text
/// #[macro_name(unknown attribute here, ignore)]
/// #[macro_name(rename = "First", rename = "Again")]
/// ```
///
/// In this situation `unknown attribute here` constitutes a syntax error that would
/// prevent parsing `ignore`. However, the second line can be fully parsed but
/// we want to error on the accidental duplicate `rename` attribute. By returning
/// partial data along side of errors we can report
///
/// ## Examples
///
/// If `E` implements [IntoIterator] and [Extend] (as [syn::Error] or does) then you can
/// accumulate errors using [OkMaybe::push_unwrap]. Use this when partial data
/// returned might hold additional errors that you wish to accumulate before returning.
///
/// For example:
///
/// ```rust
/// use proc_micro::{OkMaybe, MaybeError};
///
/// let mut errors = MaybeError::new();
///
/// # let span = proc_macro2::Span::call_site();
/// let _out: () =
/// OkMaybe((), Some(syn::Error::new(span, "message".to_string())))
/// .push_unwrap(&mut errors);
///
/// assert!(errors.has_err());
/// assert!(matches!(errors.maybe(), Some(_)));
/// ```
///
/// For situations where partial data is not usable, convert into a result:
///
/// ```rust
/// use proc_micro::OkMaybe;
///
/// # let span = proc_macro2::Span::call_site();
/// let result: Result<(), syn::Error> =
/// OkMaybe((), Some(syn::Error::new(span, "message".to_string())))
/// .to_result();
///
/// assert!(result.is_err(), "Expected an error, got {:?}", result);
///
/// let result: Result<(), syn::Error> =
/// OkMaybe((), None)
/// .to_result();
/// assert!(result.is_ok(), "Expected Ok, got {:?}", result);
/// ```
;
/// Accumulate zero or more [syn::Error]-s
///
/// It is a best practice in proc macros to iterate over a collection of
/// results to accumulate as many errors as possible. Then at the end checking
/// if the collection contains any errors and converting them into a single
/// [syn::Error] (which can hold 1 or more errors) using [syn::Error::combine].
/// This struct makes that accumulation pattern easier.
///
/// FYI a [syn::Error] that contains multiple errors can be split apart using
/// [syn::Error::into_iter]. <https://github.com/dtolnay/syn/pull/1855>
///
/// Accumulate and return errors in a function:
///
/// ```rust
/// use proc_micro::MaybeError;
///
/// # #[allow(dead_code)]
/// fn my_fun() -> Result<(), syn::Error> {
/// # #[allow(unused_mut)]
/// let mut errors = MaybeError::new();
/// // ...
///
/// if let Some(error) = errors.maybe() {
/// Err(error)
/// } else {
/// Ok(())
/// }
/// }
/// ```
///
/// Convert a non-empty "maybe" error into a [syn::Error]:
///
/// ```
/// use proc_micro::MaybeError;
///
/// let mut errors = MaybeError::new();
///
/// assert!(!errors.has_err());
/// match syn::parse_str::<syn::Ident>("ident cannot hold string with spaces") {
/// Ok(_ident) => todo!(),
/// Err(error) => errors.push_back(error)
/// }
///
/// assert!(errors.has_err());
/// let error: syn::Error = errors.maybe().unwrap();
///
/// assert_eq!(
/// vec!["unexpected token".to_string()],
/// error
/// .into_iter()
/// .map(|e| format!("{e}"))
/// .collect::<Vec<_>>()
/// )
/// ```
;