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
111
112
113
114
115
116
117
118
119
//! Proc-macro attribute for the `sleuth` crate.

#![warn(
    missing_docs,
    rustdoc::all,
    clippy::missing_docs_in_private_items,
    clippy::all,
    clippy::restriction,
    clippy::pedantic,
    clippy::nursery,
    clippy::cargo
)]
#![allow(
    clippy::blanket_clippy_restriction_lints,
    clippy::implicit_return,
    clippy::integer_arithmetic,
    clippy::mod_module_files,
    clippy::pattern_type_mismatch,
    clippy::pub_use,
    clippy::question_mark_used,
    clippy::string_add,
    clippy::wildcard_enum_match_arm,
    clippy::wildcard_imports
)]
#![deny(warnings)]

/// Fake span for delimiting tokens.
macro_rules! bs_delim_span {
    ($d:ident) => {
        proc_macro2::Group::new(proc_macro2::Delimiter::$d, proc_macro2::TokenStream::new())
            .delim_span()
    };
}

/// Tokens that have a vector of one span instead of one span for some reason, with a fake span for convenience.
macro_rules! token {
    ($t:ident) => {
        syn::token::$t {
            spans: [proc_macro2::Span::call_site()],
        }
    };
}

/// Tokens that have one span instead of a vector of one span for some reason, with a fake span for convenience.
macro_rules! single_token {
    ($t:ident) => {
        syn::token::$t {
            span: proc_macro2::Span::call_site(),
        }
    };
}

/// Tokens that delimit a group, with a fake span for convenience.
macro_rules! dual_token {
    ($t:ident) => {
        syn::token::$t {
            spans: [
                proc_macro2::Span::call_site(),
                proc_macro2::Span::call_site(),
            ],
        }
    };
}

/// Tokens that delimit a group, with a fake span for convenience.
macro_rules! delim_token {
    (Paren) => {
        syn::token::Paren {
            span: bs_delim_span!(Parenthesis),
        }
    };
    ($d:ident) => {
        syn::token::$d {
            span: bs_delim_span!($d),
        }
    };
}

mod mutate;
mod x;

/// The name of this crate. Just in case.
const CRATE_NAME: &str = "sleuth";

/// Test that this is the shortest possible implementation to fulfill a set of properties.
#[proc_macro_attribute]
pub fn sleuth(
    attr: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    match mutate::implementation(attr.into(), input.into()) {
        Ok(ts) => ts,
        Err(e) => e.to_compile_error(),
    }
    .into()
}

/// Make a trivial punctuated list containing only the argument provided.
#[inline]
fn make_punc<T, P>(v: T) -> syn::punctuated::Punctuated<T, P> {
    let mut punc = syn::punctuated::Punctuated::new();
    punc.push_value(v);
    punc
}

/// Make a trivial identifier from a string.
#[inline]
fn ident(s: &str) -> syn::Ident {
    syn::Ident::new(s, proc_macro2::Span::call_site())
}

/// Make a path segment with only one argument (not really a path, but a singleton, sure).
#[inline]
const fn pathseg(fn_name: syn::Ident) -> syn::PathSegment {
    syn::PathSegment {
        ident: fn_name,
        arguments: syn::PathArguments::None,
    }
}