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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#![crate_type = "proc-macro"]
#![feature(proc_macro, proc_macro_lib)]
#![recursion_limit = "300"]

//! A Macros 1.1 implementation of https://crates.io/crates/error-chain
//!
//! The error-chain example
//!
//! ```ignore
//! mod other_error {
//!     error_chain! {}
//! }
//!
//! error_chain {
//!     types { Error, ErrorKind, ResultExt, Result; }
//!
//!     links {
//!         Another(other_error::Error, other_error::ErrorKind) #[cfg(unix)];
//!     }
//!
//!     foreign_links {
//!         Fmt(::std::fmt::Error);
//!         Io(::std::io::Error) #[cfg(unix)];
//!     }
//!
//!     errors {
//!     	InvalidToolchainName(t: String) {
//!     		description("invalid toolchain name")
//!     		display("invalid toolchain name: '{}'", t)
//!     	}
//!     }
//! }
//! ```
//!
//! becomes
//!
//! ```ignore
//! mod other_error {
//!     #[derive(Debug, error_chain)]
//!     pub enum ErrorKind {
//!         Msg(String), // A special variant that must always be present.
//!     }
//! }
//!
//! #[derive(Debug, error_chain)]
//! // This attribute is optional if using the default names "Error", "ResultExt" and "Result".
//! #[error_chain(error = "Error", result_ext = "ResultExt", result = "Result")]
//! pub enum ErrorKind {
//!     Msg(String), // A special variant that must always be present.
//!
//!     #[cfg(unix)]
//!     #[error_chain(link = "other_error::Error")]
//!     Another(other_error::ErrorKind),
//!
//!     #[error_chain(foreign)]
//!     Fmt(::std::fmt::Error),
//!
//!     #[cfg(unix)]
//!     #[error_chain(foreign)]
//!     Io(::std::io::Error),
//!
//!     #[error_chain(custom, description = "invalid_toolchain_name_description", display = "invalid_toolchain_name_display")]
//!     InvalidToolchainName(String),
//! }
//!
//! // A description function receives refs to all the variant constituents, and should return a &str
//! fn invalid_toolchain_name_description(_: &str) -> &str {
//!     "invalid toolchain name"
//! }
//!
//! // A display function receives a formatter and refs to all the variant constituents, and should return a ::std::fmt::Result
//! fn invalid_toolchain_name_display(f: &mut ::std::fmt::Formatter, t: &str) -> ::std::fmt::Result {
//!     write!(f, "invalid toolchain name: '{}'", t)
//! }
//! ```
//!
//! Notes:
//!
//! - This library requires the nightly compiler to be able to use the `proc_macro` rust feature.
//! - The macro output can be used with `#[deny(missing_docs)]` since it allows doc comments on the ErrorKind variants.
//! - The result wrapper can be disabled by setting `result = ""` in the `#[error_chain]` attribute on the ErrorKind.
//! - The backtrace functionality can be disabled by setting `backtrace = "false"` or `backtrace = false` in the `#[error_chain]` attribute on the ErrorKind.
//! - The ErrorKind must have a special `Msg(String)` member. Unlike error_chain which adds this member implicitly, this macro requires it explicitly.
//! - The description and display functions can be inlined like this:
//!
//!    ```ignore
//!     #[error_chain(custom)]
//!     #[error_chain(description = r#"(|_| "invalid toolchain name")"#)]
//!     #[error_chain(display = r#"(|f: &mut ::std::fmt::Formatter, t| write!(f, "invalid toolchain name: '{}'", t))"#)]
//!     InvalidToolchainName(String),
//!    ```

extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;

#[proc_macro_derive(error_chain, attributes(error_chain))]
pub fn derive_error_chain(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
	let source = input.to_string();
	let ast = syn::parse_macro_input(&source).unwrap();
	let error_kind_name = ast.ident;

	let mut error_name = syn::parse_ident("Error").unwrap();
	let mut result_ext_name = syn::parse_ident("ResultExt").unwrap();
	let mut result_name = Some(syn::parse_ident("Result").unwrap());
	let mut support_backtrace = true;

	for attr in ast.attrs {
		match &attr.value {
			&syn::MetaItem::List(ref ident, ref nested_meta_items) if ident == "error_chain" => {
				for nested_meta_item in nested_meta_items {
					match *nested_meta_item {
						syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref ident, syn::Lit::Str(ref value, _))) => {
							if ident == "error" {
								error_name = syn::parse_ident(value).map_err(|err| format!("couldn't parse error attribute as an identifier - {}", err)).unwrap()
							}
							else if ident == "result_ext" {
								result_ext_name = syn::parse_ident(value).map_err(|err| format!("couldn't parse result_ext attribute as an identifier - {}", err)).unwrap()
							}
							else if ident == "result" {
								result_name =
									if value == "" {
										None
									}
									else {
										Some(syn::parse_ident(value).map_err(|err| format!("couldn't parse result attribute as an identifier - {}", err)).unwrap())
									}
							}
							else if ident == "backtrace" && value == "false" {
								support_backtrace = false
							}
						},

						syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref ident, syn::Lit::Bool(false))) if ident == "backtrace" =>
							support_backtrace = false,

						_ => { },
					}
				}
			},

			_ => { },
		}
	}

	let error_chain_name = syn::parse_ident(&(error_name.to_string() + "_error_chain")).unwrap();

	struct Link {
		variant: syn::Variant,
		link_type: LinkType,
		custom_description: Option<syn::Expr>,
		custom_display: Option<syn::Expr>,
	}

	enum LinkType {
		Chainable(syn::Ty, syn::Ty),
		Foreign(syn::Ty),
		Custom,
	}

	let result = match ast.body {
		syn::Body::Enum(variants) => {
			let mut links = vec![];

			for variant in variants {
				if &variant.ident == "Msg" {
					continue;
				}

				let mut link_type = None;
				let mut custom_description = None;
				let mut custom_display = None;

				for attr in &variant.attrs {
					if let syn::MetaItem::List(ref ident, ref nested_meta_items) = attr.value {
						if ident == "error_chain" {
							for nested_meta_item in nested_meta_items {
								match *nested_meta_item {
									syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ref ident)) => {
										if ident == "foreign" {
											match &variant.data {
												&syn::VariantData::Tuple(ref fields) if fields.len() == 1 =>
													link_type = Some(LinkType::Foreign(fields[0].ty.clone())),

												_ => panic!("Foreign link {} must be a tuple of one element (the foreign error type).", variant.ident),
											}
										}
										else if ident == "custom" {
											link_type = Some(LinkType::Custom);
										}
									},

									syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref ident, syn::Lit::Str(ref value, _))) => {
										if ident == "link" {
											match &variant.data {
												&syn::VariantData::Tuple(ref fields) if fields.len() == 1 =>
													link_type = Some(LinkType::Chainable(
														syn::parse_type(value).unwrap_or_else(|err| {
															let variant_name = &variant.ident;
															panic!("Could not parse link attribute of member {} as a type - {}", variant_name, err)
														}),
														fields[0].ty.clone())),

												_ => panic!("Chainable link {} must be a tuple of one element (the chainable error kind).", variant.ident),
											}
										}
										else if ident == "description" {
											custom_description = Some(syn::parse_expr(value).unwrap());
										}
										else if ident == "display" {
											custom_display = Some(syn::parse_expr(value).unwrap());
										}
									},

									_ => { },
								}
							}
						}
					}
				}

				let link_type =
					link_type.unwrap_or_else(|| {
						let variant_name = &variant.ident;
						panic!(r#"Member {} does not have any of #[error_chain(link = "...")] or #[error_chain(foreign)] or #[error_chain(custom)]."#, variant_name)
					});

				links.push(Link {
					variant: variant,
					link_type: link_type,
					custom_description: custom_description,
					custom_display: custom_display,
				});
			}

			let error_kind_description_cases = links.iter().map(|link| match link.link_type {
				LinkType::Chainable(_, _) => {
					let variant_name = &link.variant.ident;
					match link.custom_description {
						Some(ref custom_description) => quote! {
							#error_kind_name::#variant_name(ref kind) => #custom_description(err),
						},

						None => quote! {
							#error_kind_name::#variant_name(ref kind) => kind.description(),
						},
					}
				},

				LinkType::Foreign(_) => {
					let variant_name = &link.variant.ident;
					match link.custom_description {
						Some(ref custom_description) => quote! {
							#error_kind_name::#variant_name(ref err) => #custom_description(err),
						},

						None => quote! {
							#error_kind_name::#variant_name(ref err) => ::std::error::Error::description(err),
						},
					}
				},

				LinkType::Custom => {
					let variant_name = &link.variant.ident;
					match link.custom_description {
						Some(ref custom_description) => {
							let pattern = fields_pattern(&link.variant);
							let args = args(&link.variant);
							quote! {
								#error_kind_name::#variant_name #pattern => #custom_description(#args),
							}
						},

						None => {
							let pattern = fields_pattern_ignore(&link.variant);
							quote! {
								#error_kind_name::#variant_name #pattern => stringify!(#variant_name),
							}
						},
					}
				},
			});

			let error_kind_display_cases = links.iter().map(|link| match link.link_type {
				LinkType::Chainable(_, _) => {
					let variant_name = &link.variant.ident;
					match link.custom_display {
						Some(ref custom_display) => quote! {
							#error_kind_name::#variant_name(ref kind) => #custom_display(f, kind),
						},

						None => quote! {
							#error_kind_name::#variant_name(ref kind) => write!(f, "{}", kind),
						},
					}
				},

				LinkType::Foreign(_) => {
					let variant_name = &link.variant.ident;
					match link.custom_display {
						Some(ref custom_display) => quote! {
							#error_kind_name::#variant_name(ref err) => #custom_display(f, err),
						},

						None => quote! {
							#error_kind_name::#variant_name(ref err) => write!(f, "{}", err),
						},
					}
				},

				LinkType::Custom => {
					let variant_name = &link.variant.ident;
					match link.custom_display {
						Some(ref custom_display) => {
							let pattern = fields_pattern(&link.variant);
							let args = args(&link.variant);
							quote! {
								#error_kind_name::#variant_name #pattern => #custom_display(f, #args),
							}
						},

						None => {
							let pattern = fields_pattern_ignore(&link.variant);
							quote! {
								#error_kind_name::#variant_name #pattern => write!(f, "{}", self.description()),
							}
						},
					}
				},
			});

			let error_kind_from_impls = links.iter().map(|link| match link.link_type {
				LinkType::Chainable(_, ref error_kind_ty) => {
					let variant_name = &link.variant.ident;
					Some(quote! {
						impl From<#error_kind_ty> for #error_kind_name {
							fn from(kind: #error_kind_ty) -> Self {
								#error_kind_name::#variant_name(kind)
							}
						}
					})
				},

				LinkType::Foreign(_) |
				LinkType::Custom => None,
			});

			let error_doc_comment = format!(r"The Error type.

This struct is made of three things:

- `{0}` which is used to determine the type of the error.
- a backtrace, generated when the error is created.
- an error chain, used for the implementation of `Error::cause()`.", error_kind_name);

			let error_cause_cases = links.iter().filter_map(|link| match link.link_type {
				LinkType::Foreign(_) => {
					let variant_name = &link.variant.ident;
					Some(quote! {
						#error_kind_name::#variant_name(ref err) => err.cause(),
					})
				},

				LinkType::Chainable(_, _) |
				LinkType::Custom => None,
			});

			let chained_error_extract_backtrace_cases = links.iter().filter_map(|link| match link.link_type {
				LinkType::Chainable(ref error_ty, _) => {
					Some(quote! {
						if let Some(err) = err.downcast_ref::<#error_ty>() {
							return err.1.backtrace.clone();
						}
					})
				},

				LinkType::Foreign(_) |
				LinkType::Custom => None,
			});

			let error_from_impls = links.iter().filter_map(|link| match link.link_type {
				LinkType::Chainable(ref error_ty, _) => {
					let variant_name = &link.variant.ident;
					Some(quote! {
						impl From<#error_ty> for #error_name {
							fn from(err: #error_ty) -> Self {
								#error_name(#error_kind_name::#variant_name(err.0), err.1)
							}
						}
					})
				},

				LinkType::Foreign(ref ty) => {
					let variant_name = &link.variant.ident;
					Some(quote! {
						impl From<#ty> for #error_name {
							fn from(err: #ty) -> Self {
								Self::from_kind(#error_kind_name::#variant_name(err))
							}
						}
					})
				},

				LinkType::Custom => None,
			});

			let extract_backtrace_fn = if support_backtrace {
				Some(quote! {
					fn extract_backtrace(err: &(::std::error::Error + Send + 'static)) -> Option<::std::sync::Arc<#error_chain_name::Backtrace>> {
						if let Some(err) = err.downcast_ref::<Self>() {
							return err.1.backtrace.clone();
						}

						#(#chained_error_extract_backtrace_cases)*

						None
					}
				})
			}
			else {
				None
			};

			let result_wrapper = result_name.map(|result_name| quote! {
				/// Convenient wrapper around `std::Result`.
				pub type #result_name<T> = ::std::result::Result<T, #error_name>;
			});

			quote! {
				extern crate error_chain as #error_chain_name;

				impl #error_kind_name {
					/// A string describing the error kind.
					pub fn description(&self) -> &str {
						match *self {
							#error_kind_name::Msg(ref s) => s,

							#(#error_kind_description_cases)*
						}
					}
				}

				impl ::std::fmt::Display for #error_kind_name {
					fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
						match *self {
							#error_kind_name::Msg(ref s) => write!(f, "{}", s),

							#(#error_kind_display_cases)*
						}
					}
				}

				#(#error_kind_from_impls)*

				impl <'a> From<&'a str> for #error_kind_name {
					fn from(s: &'a str) -> Self { #error_kind_name::Msg(s.to_string()) }
				}

				impl From<String> for #error_kind_name {
					fn from(s: String) -> Self { #error_kind_name::Msg(s) }
				}

				impl From<#error_name> for #error_kind_name {
					fn from(err: #error_name) -> Self { err.0 }
				}

				#[doc = #error_doc_comment]
				#[derive(Debug)]
				pub struct #error_name(
					/// The kind of the error.
					pub #error_kind_name,

					/// Contains the error chain and the backtrace.
					pub #error_chain_name::State,
				);

				#[allow(unused)]
				impl #error_name {
					/// Constructs an error from a kind, and generates a backtrace.
					pub fn from_kind(kind: #error_kind_name) -> #error_name {
						#error_name(kind, #error_chain_name::State::default())
					}

					/// Returns the kind of the error.
					pub fn kind(&self) -> &#error_kind_name { &self.0 }

					/// Iterates over the error chain.
					pub fn iter(&self) -> #error_chain_name::ErrorChainIter {
						#error_chain_name::ChainedError::iter(self)
					}

					/// Returns the backtrace associated with this error.
					pub fn backtrace(&self) -> Option<&#error_chain_name::Backtrace> {
						self.1.backtrace()
					}
				}

				impl ::std::error::Error for #error_name {
					fn description(&self) -> &str { self.0.description() }

					fn cause(&self) -> Option<&::std::error::Error> {
						match self.1.next_error {
							Some(ref c) => Some(&**c),
							None => match self.0 {
								#(#error_cause_cases)*

								_ => None,
							},
						}
					}
				}

				impl ::std::fmt::Display for #error_name {
					fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
						::std::fmt::Display::fmt(&self.0, f)
					}
				}

				#(#error_from_impls)*

				impl From<#error_kind_name> for #error_name {
					fn from(kind: #error_kind_name) -> Self { Self::from_kind(kind) }
				}

				impl <'a> From<&'a str> for #error_name {
					fn from(s: &'a str) -> Self { Self::from_kind(s.into()) }
				}

				impl From<String> for #error_name {
					fn from(s: String) -> Self { Self::from_kind(s.into()) }
				}

				impl ::std::ops::Deref for #error_name {
					type Target = #error_kind_name;

					fn deref(&self) -> &Self::Target { &self.0 }
				}

				impl #error_chain_name::ChainedError for #error_name {
					type ErrorKind = #error_kind_name;

					fn new(kind: Self::ErrorKind, state: #error_chain_name::State) -> Self {
						#error_name(kind, state)
					}

					fn from_kind(kind: Self::ErrorKind) -> Self {
						Self::from_kind(kind)
					}

					fn kind(&self) -> &Self::ErrorKind {
						self.kind()
					}

					fn iter(&self) -> #error_chain_name::ErrorChainIter {
						#error_chain_name::ErrorChainIter(Some(self))
					}

					fn backtrace(&self) -> Option<&#error_chain_name::Backtrace> {
						self.backtrace()
					}

					#extract_backtrace_fn
				}

				/// Additional methods for `Result`, for easy interaction with this crate.
				pub trait #result_ext_name<T, E> {
					/// If the `Result` is an `Err` then `chain_err` evaluates the closure,
					/// which returns *some type that can be converted to `ErrorKind`*,
					/// boxes the original error to store as the cause, then returns a new error
					/// containing the original error.
					fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, Error>
						where F: FnOnce() -> EK, EK: Into<ErrorKind>;
				}

				impl<T, E> #result_ext_name<T, E> for ::std::result::Result<T, E>
					where E: ::std::error::Error + Send + 'static {
					fn chain_err<F, EK>(self, callback: F) -> ::std::result::Result<T, Error>
						where F: FnOnce() -> EK, EK: Into<ErrorKind> {
						self.map_err(move |e| {
							let state = #error_chain_name::State::new::<Error>(Box::new(e));
							#error_chain_name::ChainedError::new(callback().into(), state)
						})
					}
				}

				#result_wrapper
			}
		},

		_ => panic!("#[derive(error_chain)] can only be used with enums."),
	};

	result.to_string().parse().unwrap()
}

fn fields_pattern(variant: &syn::Variant) -> quote::Tokens {
	match variant.data {
		syn::VariantData::Struct(ref fields) => {
			let fields = fields.iter().map(|f| {
				let field_name = &f.ident;
				quote!(ref #field_name)
			});
			quote!({ #(#fields),* })
		},

		syn::VariantData::Tuple(ref fields) => {
			let fields = fields.iter().enumerate().map(|(i, _)| {
				let field_name = syn::parse_ident(&format!("value{}", i)).unwrap();
				quote!(ref #field_name)
			});
			quote!((#(#fields),*))
		},

		syn::VariantData::Unit => quote!(),
	}
}

fn fields_pattern_ignore(variant: &syn::Variant) -> quote::Tokens {
	match variant.data {
		syn::VariantData::Struct(_) => quote!({ .. }),
		syn::VariantData::Tuple(_) => quote!((..)),
		syn::VariantData::Unit => quote!(),
	}
}

fn args(variant: &syn::Variant) -> quote::Tokens {
	match variant.data {
		syn::VariantData::Struct(ref fields) => {
			let fields = fields.iter().map(|f| {
				let field_name = &f.ident;
				quote!(#field_name)
			});
			quote!((#(#fields),*))
		},

		syn::VariantData::Tuple(ref fields) => {
			let fields = fields.iter().enumerate().map(|(i, _)| {
				let field_name = syn::parse_ident(&format!("value{}", i)).unwrap();
				quote!(#field_name)
			});
			quote!((#(#fields),*))
		},

		syn::VariantData::Unit => quote!(),
	}
}