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
use syntax::ast::*;
use syntax::codemap::{Span, Spanned};
use syntax::tokenstream::{TokenTree};
use super::{PluginResult};
macro_rules! to_error {
($ty:ty) => (
impl<T, S: Into<String>> ToError<T, S> for $ty {
fn to_error(&self, message: S) -> PluginResult<T> {
Err((self.span, message.into()))
}
}
);
}
pub trait PluginResultExt<T> {
fn map_err_span(self, span: Span) -> PluginResult<T>;
fn map_err_message<S: Into<String>>(self, message: S) -> PluginResult<T>;
}
impl<T> PluginResultExt<T> for PluginResult<T> {
fn map_err_span(self, span: Span) -> PluginResult<T> {
self.map_err(|(_, m)| (span, m))
}
fn map_err_message<S: Into<String>>(self, message: S) -> PluginResult<T> {
self.map_err(|(s, _)| (s, message.into()))
}
}
pub trait ToError<T, S> where S: Into<String> {
fn to_error(&self, message: S) -> PluginResult<T>;
}
impl<T, S: Into<String>> ToError<T, S> for Span {
fn to_error(&self, message: S) -> PluginResult<T> {
Err((*self, message.into()))
}
}
impl<T, S: Into<String>> ToError<T, S> for TokenTree {
fn to_error(&self, message: S) -> PluginResult<T> {
Err((self.get_span(), message.into()))
}
}
impl<T, U, S: Into<String>> ToError<T, S> for Spanned<U> {
fn to_error(&self, message: S) -> PluginResult<T> {
Err((self.span, message.into()))
}
}
to_error!(Block);
to_error!(Expr);
to_error!(Item);
to_error!(Pat);
to_error!(Path);
to_error!(Stmt);
to_error!(Ty);