Debian Codegraph @

src/error.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 use std::iter::{self, FromIterator};
4
5 use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
6
7 use crate::to_tokens::ToTokens;
8
9 macro_rules! format_err {
10 ($span:expr, $msg:expr $(,)*) => {
11 crate::error::Error::new(&$span, String::from($msg))
12 };
13 ($span:expr, $($tt:tt)*) => {
14 format_err!($span, format!($($tt)*))
15 };
16 }
17
18 macro_rules! bail {
19 ($($tt:tt)*) => {
20 return Err(format_err!($($tt)*))
21 };
22 }
23
24 pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
25
26 #[derive(Debug)]
27 pub(crate) struct Error {
28 start_span: Span,
29 end_span: Span,
30 msg: String,
31 }
32
33 impl Error {
34 pub(crate) fn new(tokens: &dyn ToTokens, msg: String) -> Self {
35 let mut iter = tokens.to_token_stream().into_iter();
36 // `Span` on stable Rust has a limitation that only points to the first
37 // token, not the whole tokens. We can work around this limitation by
38 // using the first/last span of the tokens like `syn::Error::new_spanned` does.
39 let start_span = iter.next().map_or_else(Span::call_site, |t| t.span());
40 let end_span = iter.last().map_or(start_span, |t| t.span());
41
42 Self { start_span, end_span, msg }
43 }
44
45 // Based on https://github.com/dtolnay/syn/blob/1.0.39/src/error.rs#L210-L237
46 pub(crate) fn into_compile_error(self) -> TokenStream {
47 // compile_error!($msg)
48 TokenStream::from_iter(vec![
49 TokenTree::Ident(Ident::new("compile_error", self.start_span)),
50 TokenTree::Punct({
51 let mut punct = Punct::new('!', Spacing::Alone);
52 punct.set_span(self.start_span);
53 punct
54 }),
55 TokenTree::Group({
56 let mut group = Group::new(Delimiter::Brace, {
57 iter::once(TokenTree::Literal({
58 let mut string = Literal::string(&self.msg);
59 string.set_span(self.end_span);
60 string
61 }))
62 .collect()
63 });
64 group.set_span(self.end_span);
65 group
66 }),
67 ])
68 }
69 }