2
2
use std:: convert:: Infallible ;
3
3
use std:: fmt:: { Debug , Display } ;
4
4
use std:: mem;
5
- use std:: ops:: Range ;
5
+ use std:: ops:: { Add , AddAssign , Range } ;
6
6
7
7
#[ cfg( feature = "darling" ) ]
8
8
use darling_core:: Error as DarlingError ;
@@ -58,6 +58,20 @@ impl From<SilentError> for Error {
58
58
}
59
59
}
60
60
61
+ impl < T : ToTokensError + ' static > Add < T > for Error {
62
+ type Output = Error ;
63
+
64
+ fn add ( self , rhs : T ) -> Self :: Output {
65
+ self . join ( rhs)
66
+ }
67
+ }
68
+
69
+ impl < T : ToTokensError + ' static > AddAssign < T > for Error {
70
+ fn add_assign ( & mut self , rhs : T ) {
71
+ self . push ( rhs) ;
72
+ }
73
+ }
74
+
61
75
impl Error {
62
76
/// Mimics [`From<impl ToTokensError> for Error`](From) implementation to
63
77
/// not conflict std's `From<T> for T`
@@ -66,6 +80,17 @@ impl Error {
66
80
}
67
81
68
82
/// Pushes an additional `Error`
83
+ ///
84
+ /// Alternatively errors can also be "added":
85
+ ///
86
+ /// ```
87
+ /// use manyhow::{Error, error_message};
88
+ /// let mut error = Error::from(error_message!("Hello Rust!"));
89
+ /// error += error_message!("Hello 🦀!");
90
+ /// # use manyhow::ToTokensError;
91
+ /// # proc_macro_utils::assert_tokens!(error.into_token_stream(),
92
+ /// # { ::core::compile_error!{"Hello Rust!"} ::core::compile_error!{"Hello 🦀!"} });
93
+ /// ```
69
94
pub fn push ( & mut self , error : impl ToTokensError + ' static ) {
70
95
self . 0 . push ( Box :: new ( error) ) ;
71
96
}
@@ -139,6 +164,14 @@ impl From<ErrorMessage> for Syn2Error {
139
164
}
140
165
}
141
166
167
+ impl < T : ToTokensError + ' static > Add < T > for ErrorMessage {
168
+ type Output = Error ;
169
+
170
+ fn add ( self , rhs : T ) -> Self :: Output {
171
+ self . join ( rhs)
172
+ }
173
+ }
174
+
142
175
impl ErrorMessage {
143
176
/// Creates a new error message at the specified span
144
177
///
@@ -222,6 +255,12 @@ impl Attachment for ErrorMessage {
222
255
#[ derive( Default , Debug ) ]
223
256
pub struct Emitter ( Vec < Box < dyn ToTokensError > > ) ;
224
257
258
+ impl < T : ToTokensError + ' static > AddAssign < T > for Emitter {
259
+ fn add_assign ( & mut self , rhs : T ) {
260
+ self . emit ( rhs) ;
261
+ }
262
+ }
263
+
225
264
impl Emitter {
226
265
/// Creates an `Emitter`, this can be used to collect errors than can later
227
266
/// be converted with [`Emitter::into_result()`].
@@ -236,7 +275,16 @@ impl Emitter {
236
275
}
237
276
}
238
277
239
- /// Emitts an error
278
+ /// Emits an error
279
+ ///
280
+ /// Alternatively errors can also be "added":
281
+ ///
282
+ /// ```
283
+ /// let mut emitter = manyhow::Emitter::new();
284
+ /// emitter += manyhow::error_message!("Hello World!");
285
+ /// # use manyhow::ToTokensError;
286
+ /// # proc_macro_utils::assert_tokens!(emitter.into_result().unwrap_err().into_token_stream(), { ::core::compile_error!{"Hello World!"} });
287
+ /// ```
240
288
pub fn emit ( & mut self , error : impl ToTokensError + ' static ) {
241
289
self . 0 . push ( Box :: new ( error) ) ;
242
290
}
@@ -307,6 +355,20 @@ pub trait JoinToTokensError {
307
355
///
308
356
/// error_message!("test").join(error_message!("another"));
309
357
/// ```
358
+ ///
359
+ /// Some errors like [`manyhow::Error`] and [`manyhow::ErrorMessage`] can
360
+ /// also be "added":
361
+ ///
362
+ /// ```
363
+ /// # use manyhow::ToTokensError;
364
+ /// use manyhow::{error_message, Error};
365
+ /// # proc_macro_utils::assert_tokens!((
366
+ /// error_message!("Hello Rust!") + error_message!("Hello 🦀!")
367
+ /// # ).into_token_stream(), { ::core::compile_error!{"Hello Rust!"} ::core::compile_error!{"Hello 🦀!"} });
368
+ /// # proc_macro_utils::assert_tokens!((
369
+ /// Error::from(error_message!("Hello Rust!")) + error_message!("Hello 🦀!")
370
+ /// # ).into_token_stream(), { ::core::compile_error!{"Hello Rust!"} ::core::compile_error!{"Hello 🦀!"} });
371
+ /// ```
310
372
fn join ( self , error : impl ToTokensError + ' static ) -> Error ;
311
373
}
312
374
0 commit comments