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);