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
// Copyright 2016 Kyle Mayes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use syntax::ast::*;
use syntax::codemap::{Span, Spanned};
use syntax::tokenstream::{TokenTree};

use super::{PluginResult};

//================================================
// Macros
//================================================

// to_error! _____________________________________

/// Defines a `ToError` implementation for the supplied type.
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()))
            }
        }
    );
}

//================================================
// Traits
//================================================

// PluginResultExt _______________________________

/// Extends `PluginResult<T>`.
pub trait PluginResultExt<T> {
    /// Returns this `PluginResult<T>` with a different span if it is an `Err`.
    fn map_err_span(self, span: Span) -> PluginResult<T>;

    /// Returns this `PluginResult<T>` with a different message if it is an `Err`.
    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()))
    }
}

// ToError _______________________________________

/// A type that can be extended into a `PluginResult<T>`.
pub trait ToError<T, S> where S: Into<String> {
    /// Returns an `Err` value with the span of this value and the supplied message.
    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);