1#[doc(hidden)]
5extern crate proc_macro;
6
7#[doc(hidden)]
8extern crate alloc;
9
10use proc_macro2::TokenStream;
11use quote::quote;
12use syn::{DeriveInput, Expr, Lit};
13
14fn derive_scripting_enum(input: &DeriveInput) -> TokenStream {
16 let ident = &input.ident;
18
19 let mut discriminant = -1_i8;
21 let variants: Vec<(String, i8)> = match &input.data {
22 syn::Data::Enum(data) => data
23 .variants
24 .iter()
25 .map(|v| {
26 if let Some((_eq, expr)) = &v.discriminant {
27 match expr {
28 Expr::Lit(expr_lit) => match &expr_lit.lit {
29 Lit::Int(lit_int) => {
30 discriminant = lit_int
31 .base10_parse::<i8>()
32 .expect("value must be i8");
33 }
34 _ => panic!("value must be i8"),
35 },
36 _ => panic!("value must be i8"),
37 }
38 } else {
39 discriminant += 1;
40 }
41 (v.ident.to_string(), discriminant)
42 })
43 .collect(),
44 syn::Data::Struct(_struct) => panic!("structs not supported by ScriptEnum"),
45 syn::Data::Union(_union) => panic!("unions not supported by ScriptEnum"),
46 };
47 let variant_keys: Vec<String> = variants.iter().map(|v| v.0.clone()).collect();
48 let variant_discriminants: Vec<i8> = variants.iter().map(|v| v.1).collect();
49
50 let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
52
53 let derived: TokenStream = "#[automatically_derived]"
54 .parse()
55 .expect("derive(ScriptEnum) - derived");
56 let diagnostic: TokenStream = "#[diagnostic::do_not_recommend]"
57 .parse()
58 .expect("derive(ScriptEnum) - diagnostic");
59
60 quote! {
61 #derived
62 #diagnostic
63 impl #impl_generics tinyscript::ScriptEnum for #ident #type_generics #where_clause {
64 fn key_value_tuples<'a>() -> tinyscript::__private::Vec<(&'a str, i8)> {
65 tinyscript::__private::vec![#((#variant_keys, #variant_discriminants)),*]
66 }
67 }
68 }
69}
70
71#[proc_macro_derive(ScriptEnum, attributes(tscript))]
114pub fn derive_script_enum(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
115 let input: DeriveInput = syn::parse(input).expect("could not parse input");
117
118 derive_scripting_enum(&input).into()
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124 use syn::parse_quote;
125
126 fn expand(input: &DeriveInput) -> String {
127 derive_scripting_enum(input).to_string()
128 }
129
130 #[test]
131 fn variants_are_numbered_from_zero_or_explicit_discriminants() {
132 let input: DeriveInput = parse_quote! {
133 enum Color { Red, Green = 5, Blue }
134 };
135 let expanded = expand(&input);
136 assert!(expanded.contains("impl tinyscript :: ScriptEnum for Color"));
137 assert!(expanded.contains(r#"("Red" , 0i8) , ("Green" , 5i8) , ("Blue" , 6i8)"#));
138 }
139
140 #[test]
141 fn generics_are_kept() {
142 let input: DeriveInput = parse_quote! {
143 enum Wrapper<T: Clone> where T: Copy { A }
144 };
145 assert!(expand(&input).contains("impl < T : Clone > tinyscript :: ScriptEnum for Wrapper < T > where T : Copy"));
146 }
147
148 #[test]
149 #[should_panic(expected = "structs not supported by ScriptEnum")]
150 fn structs_are_rejected() {
151 let input: DeriveInput = parse_quote! { struct S; };
152 expand(&input);
153 }
154
155 #[test]
156 #[should_panic(expected = "unions not supported by ScriptEnum")]
157 fn unions_are_rejected() {
158 let input: DeriveInput = parse_quote! { union U { a: u8 } };
159 expand(&input);
160 }
161
162 #[test]
163 #[should_panic(expected = "value must be i8")]
164 fn discriminant_out_of_i8_range_is_rejected() {
165 let input: DeriveInput = parse_quote! { enum E { A = 200 } };
166 expand(&input);
167 }
168
169 #[test]
170 #[should_panic(expected = "value must be i8")]
171 fn non_integer_discriminant_is_rejected() {
172 let input: DeriveInput = parse_quote! { enum E { A = 'a' } };
173 expand(&input);
174 }
175
176 #[test]
177 #[should_panic(expected = "value must be i8")]
178 fn computed_discriminant_is_rejected() {
179 let input: DeriveInput = parse_quote! { enum E { A = 1 + 1 } };
180 expand(&input);
181 }
182}