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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! # Auto Convert Enums
//! This crate provides a proc macro that generates [From] impls for unnamed enum fields that have a type.
//!
//! Ever get tired of writing the same From impls to enable using `?` on some [Result] types? This crate is for you!
//!
//! This is useful for enums that are used as a wrapper for a collecting multiple errors into one big enum.
//!
//! ## Example
//! ```
//! #[macro_use] extern crate ace_it;
//!
//! #[derive(Debug)]
//! #[ace_it]
//! enum Error {
//!   Io(std::io::Error),
//!   ParseInt(std::num::ParseIntError),
//!   ParseFloat(std::num::ParseFloatError),
//! }
//! ```
//! After this, Error has three [From] impls:
//! * [From]<[std::io::Error]> for Error
//! * [From]<[std::num::ParseIntError]> for Error
//! * [From]<[std::num::ParseFloatError]> for Error
//!
//! Now you can use `?` on any of these types and get an Error back.
//! ```
//! # #[macro_use] extern crate ace_it;
//! # 
//! # #[derive(Debug)]
//! # #[ace_it]
//! # enum Error {
//! #   Io(std::io::Error),
//! #   ParseInt(std::num::ParseIntError),
//! #   ParseFloat(std::num::ParseFloatError),
//! # }
//! 
//! use std::io::Read;
//!
//! fn read_int<R: Read>(reader: &mut R) -> Result<i32, Error> {
//!     let mut buf = String::new();
//!     reader.read_to_string(&mut buf)?;
//!     Ok(buf.parse()?)
//! }

use std::collections::HashSet;

use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{spanned::Spanned, Fields, Variant};

/// Generates [From] impls for the given enum.
/// ## Usage
/// ### Applying the macro
/// This will generate the From impls for each type and turns it into an accompanying variant.
/// ```
/// # #[macro_use] extern crate ace_it;
/// #[ace_it]
/// enum Error {
///   Io(std::io::Error),
///   ParseInt(std::num::ParseIntError),
///   ParseFloat(std::num::ParseFloatError),
/// }
/// ```
/// ### Compile errors
/// This will error because there are two variants with the same type.
/// There is no way to know which one to use. 
/// ```compile_fail
/// # #[macro_use] extern crate ace_it;
/// #[ace_it]
/// enum SomeEnum {
///     A(i32),
///     B(i32) // Duplicate i32, shouldn't compile
/// }
/// ```
#[proc_macro_attribute]
pub fn ace_it(
    _: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let parsed = syn::parse(input);

    let parsed = match parsed {
        Ok(parsed) => parsed,
        Err(e) => return e.to_compile_error().into(),
    };

    ace_it_impl(parsed).into()
}

/// Generates From impls for the given enum.
fn process_variants<'a>(
    variants: impl Iterator<Item = &'a Variant>,
    enum_name: &Ident,
) -> Vec<TokenStream> {
    let mut from_impls = Vec::new();

    for variant in variants {
        let variant_name = &variant.ident;

        if let Fields::Unnamed(fields) = &variant.fields {
            let types = &fields.unnamed;
            let imp = quote! {
                impl From<#types> for #enum_name {
                    fn from(value: #types) -> Self {
                        Self::#variant_name(value)
                    }
                }
            };

            from_impls.push(imp);
        }
    }

    from_impls
}

fn find_duplicate_variant_type<'a>(variants: impl Iterator<Item = &'a Variant>) -> Option<Span> {
    let mut types_map = HashSet::new();
    for variant in variants {
        if let Fields::Unnamed(fields) = &variant.fields {
            let types = fields.unnamed.to_token_stream().to_string();

            if !types_map.insert(types) {
                return Some(variant.span());
            }
        }
    }
    None
}

fn ace_it_impl(parsed: syn::ItemEnum) -> TokenStream {
    let mut enum_def = parsed.to_token_stream();
    if let Some(var) = find_duplicate_variant_type(parsed.variants.iter()) {
        return syn::Error::new(
            var,
            "Duplicate variant type, can't auto-generate From impls",
        )
        .to_compile_error();
    }

    let for_impls = process_variants(parsed.variants.iter(), &parsed.ident);

    for impls in for_impls {
        impls.to_tokens(&mut enum_def);
    }

    enum_def
}

#[cfg(test)]
mod tests {
    use super::*;

    use quote::quote;
    use syn::parse2;

    #[test]
    fn ace_it() {
        let input = quote! {
            enum Test {
                A,
                B(u32),
                C { a: u32, b: u32 },
            }
        };
        let expected = quote! {
            enum Test {
                A,
                B(u32),
                C { a: u32, b: u32 },
            }

            impl From<u32> for Test {
                fn from(value: u32) -> Self {
                    Self::B(value)
                }
            }
        };
        let parsed: syn::ItemEnum = parse2(input).unwrap();
        let result = ace_it_impl(parsed);
        assert_eq!(result.to_string(), expected.to_string());
    }

    #[test]
    fn repeating_types_error() {
        let input = quote! {
            enum Test {
                A,
                B(u32),
                C(u32),
            }
        };
        let parsed: syn::ItemEnum = parse2(input).unwrap();
        let result = ace_it_impl(parsed);
        assert!(result.to_string().contains("Duplicate variant type"));
    }
}