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
use crate::spanned::Spans;

use proc_macro2::{
    Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream as TokenStream2,
    TokenTree as TokenTree2,
};

use std::fmt::Display;

#[derive(Debug, Clone)]
pub struct Error {
    messages: Vec<CompileError>,
}

#[derive(Debug, Clone)]
enum CompileError {
    Basic {
        start_span: Span,
        end_span: Span,
        msg: String,
    },
    #[cfg(feature = "derive")]
    Syn(TokenStream2),
}

impl Error {
    pub fn new<T: Display>(span: Span, msg: T) -> Self {
        Error {
            messages: vec![CompileError::Basic {
                start_span: span,
                end_span: span,
                msg: msg.to_string(),
            }],
        }
    }

    pub fn spanned<T: Display>(spans: Spans, msg: T) -> Self {
        Error {
            messages: vec![CompileError::Basic {
                start_span: spans.start,
                end_span: spans.end,
                msg: msg.to_string(),
            }],
        }
    }

    pub fn to_compile_error(&self) -> TokenStream2 {
        macro_rules!  tokenstream{
            ($($tt:expr),* $(,)*) => ({
                let list: Vec<TokenTree2> = vec![
                    $($tt.into(),)*
                ];
                list.into_iter().collect::<TokenStream2>()
            })
        }

        self.messages
            .iter()
            .map(|em| match em {
                CompileError::Basic {
                    start_span,
                    end_span,
                    msg,
                } => {
                    let ts = tokenstream![
                        Ident::new("compile_error", *start_span),
                        {
                            let mut this = Punct::new('!', Spacing::Alone);
                            this.set_span(*start_span);
                            this
                        },
                        {
                            let mut group = Group::new(
                                Delimiter::Parenthesis,
                                tokenstream![{
                                    let mut lit = Literal::string(msg);
                                    lit.set_span(*end_span);
                                    TokenTree2::Literal(lit)
                                }],
                            );
                            group.set_span(*end_span);
                            group
                        },
                    ];

                    // Still have no idea why the compile_error has to be wrapped in parentheses
                    // so that the spans point at the stuff between start_span and end_span.
                    let mut this = Group::new(Delimiter::Parenthesis, ts);
                    this.set_span(*end_span);
                    tokenstream![this]
                }
                #[cfg(feature = "derive")]
                CompileError::Syn(x) => x.clone(),
            })
            .collect()
    }

    pub fn combine(&mut self, another: Error) {
        self.messages.extend(another.messages)
    }
}

#[cfg(feature = "derive")]
impl From<syn::Error> for Error {
    fn from(err: syn::Error) -> Self {
        Self {
            messages: vec![CompileError::Syn(err.to_compile_error())],
        }
    }
}