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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

#![recursion_limit="128"]

extern crate proc_macro;
extern crate proc_macro2;
extern crate wood;

use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as PM2TS};
use quote::quote;
use syn::{Ident, Data::{Struct, Enum, Union}, Fields::{Named, Unnamed, Unit}};


// was going to make an impl that only does part of the translation, to let the user prepend tags. But I decided I'd prefer to just write translations by hand. Opening this up to the point where it's helping to automate, but also the user has full control, seems daunting. I might hate magic.
// #[proc_macro_derive(GeneratedWoodable)]
// pub fn generatedwoodable_derive(input: TokenStream) -> TokenStream {
// 	generated_Woodable(input, quote!{GeneratedWoodable});
// }

#[proc_macro_derive(Woodable)]
pub fn woodable_derive(input: TokenStream) -> TokenStream {
	generated_woodable(input, quote!{Woodable})
}
fn generated_woodable(input: TokenStream, being_impld: PM2TS) -> TokenStream {
	let ast: syn::DeriveInput = syn::parse(input).unwrap();

	let name = &ast.ident;
	
	let ret: TokenStream = match ast.data {
		Struct(ref s)=> {
			let woodify_branch_contents:Vec<PM2TS> = match s.fields {
				Named(ref n)=> {
					n.named.iter().map(|m:&syn::Field|{
						let mid = &m.ident.as_ref().unwrap();
						quote!{
							wood::woods!(
								stringify!(#mid),
								self.#mid.woodify(),
							)
						}
					}).collect()
				},
				Unnamed(ref n)=> {
					n.unnamed.iter().enumerate().map(|(i, _m)|{
						let id = syn::Index::from(i);
						quote!{ self.#id.woodify() }
					}).collect()
				},
				Unit=> {
					vec!(quote!{})
				}
			};
			
			let tit = woodify_branch_contents.into_iter();
			
			(quote! {
				impl wood::#being_impld for #name {
					fn woodify(&self)-> wood::Wood {
						wood::woods!(
							stringify!(#name),
							#(#tit),*
						)
					}
				}
			}).into()
		},
		
		
		Enum(ref e)=> {
			let variant_cases:Vec<PM2TS> = e.variants.iter().map(|m:&syn::Variant|{
				let variant_name = &m.ident;
				
				//left is just the name, right is the 
				let has_fields = |bindings: PM2TS, woodifications:PM2TS|{
					quote!{
						#name::#variant_name #bindings => {
							wood::woods!(
								stringify!(#variant_name),
								#woodifications
							)
						}
					}
				};
				
				
				match m.fields {
					Named(ref fs)=>{
						let bindvec:Vec<PM2TS> = fs.named.iter().map(|m:&syn::Field|{
							let id = &m.ident;
							quote!{ ref #id }
						}).collect();
						let woodvec:Vec<PM2TS> = fs.named.iter().map(|m:&syn::Field|{
							let fid = m.ident.as_ref().unwrap();
							quote!{
								wood::woods!(
									stringify!(#fid),
									#fid.woodify(),
								)
							}
						}).collect();
						
						has_fields(
							quote!{ { #(#bindvec),* } },
							quote!{ #(#woodvec),* }
						)
					},
					Unnamed(ref fs)=>{
						let names:Vec<Ident> = (0..fs.unnamed.len()).map(|i|{
							Ident::new(format!("v{}", i).as_str(), Span::call_site())
						}).collect();
						let nam = names.iter().map(|id| quote!{ ref #id });
						let woodvec = names.iter().map(|s| quote!{ #s.woodify() });
						has_fields(
							quote!{ ( #(#nam),* ) },
							quote!{ #(#woodvec),* },
						)
					},
					Unit =>{
						quote! {
							#name::#variant_name => stringify!(#variant_name).into()
						}
					}
				}
			}).collect();
			
			(quote! {
				impl wood::#being_impld for #name {
					fn woodify(&self)-> wood::Wood {
					 	match *self {
							#(#variant_cases),*
						}
					}
				}
			}).into()
		},
		Union(_)=> {
			panic!("derive(Woodable) cannot support untagged unions. It would have no way of knowing which variant to read from")
		}
	};
	
	ret
}



// #[proc_macro_attribute]
// pub fn wood_derive(attr: TokenStream, item: TokenStream) -> TokenStream {
// 	(quote!{}).into()
// }


#[proc_macro_derive(Dewoodable, attributes(wood))]
pub fn dewoodable_derive(input: TokenStream) -> TokenStream {
	let ast: syn::DeriveInput = syn::parse(input).unwrap();

	let name = &ast.ident;
	
	let ret = TokenStream::from( match ast.data {
		Struct(ref s)=> {
			
			let with_body = |method_body|-> PM2TS {
				//it seeks over the contents of the wood branch in such way where if the items are in order it will find each one immediately
				quote! {
					impl wood::Dewoodable for #name {
						fn dewoodify(v:&wood::Wood)-> Result<Self, Box<wood::WoodError>> {
							#method_body
						}
					}
				}
			};
			
			match s.fields {
				Named(ref n)=> {
					
					if n.named.len() == 0 {
						with_body(quote! { Self{} })
					}else{
						
						let mut var_parsing:Vec<PM2TS> = Vec::new();
						for m in n.named.iter() {
							// if m.attrs.iter().any(|a| a.path.segments.zip(["wood", "dont"].iter()).all(|i, s| i.ident == s)) {continue;}
							let var_ident = &m.ident.as_ref().unwrap();
							let var_type = &m.ty;
							var_parsing.push(quote!{
								#var_ident: {type T = #var_type; T::dewoodify(scanning.find(stringify!(#var_ident))?)?}
							});
						}
						
						let number_of_fields = var_parsing.len();
						
						with_body(quote!{
							let mut scanning = wood::wooder::FieldScanning::new(v);
							if scanning.li.len() != #number_of_fields {
								return Err(Box::new(wood::WoodError::new(v, format!("{} expected the wood to have {} elements, but it has {}", stringify!(#name), #number_of_fields, scanning.li.len()))));
							}
							
							Ok(Self{
								#(#var_parsing),*
							})
						})
					}
				},
				
				Unnamed(ref n)=> {
					
					let number_of_fields = n.unnamed.len();
					
					let each_field = n.unnamed.iter().enumerate().map(|(i, m)|{
						let ty = &m.ty;
						quote!{#ty::dewoodify(&li[#i])?}
					});
					
					with_body(quote!{
						let li = v.tail().as_slice();
						if li.len() == #number_of_fields {
							Ok(Self(#(#each_field),*))
						}else{
							Err(Box::new(wood::WoodError::new(v, format!("{} expected the wood to have {} fields, but it has {}", stringify!(#name), #number_of_fields, li.len()))))
						}
					})
				},
				Unit=> {
					with_body(quote!{
						Ok(#name)
					})
				}
			}
		},
		
		
		Enum(ref e)=> {
			
			let variant_cases:Vec<PM2TS> = e.variants.iter().map(|m:&syn::Variant|{
				let variant_name = &m.ident;
				
				let for_fields:PM2TS = match m.fields {
					Named(ref n)=> {
						let each_feild:Vec<PM2TS> = n.named.iter().map(|f:&syn::Field|{
							let id = &f.ident;
							let ty = &f.ty;
							quote!{ #id: #ty::dewoodify(scanning.find(stringify!(#id))?)? }
						}).collect();
						
						let number_of_fields = each_feild.len();
						
						quote!{
							let mut scanning = wooder::FieldScanning::new(v);
							if scanning.li.len() != #number_of_fields {
								return Err(Box::new(wood::WoodError::new(v, format!("variant {} expected {} elements, found {}", stringify!(#variant_name), #number_of_fields, scanning.li.len()))));
							}
							Ok(#name::#variant_name {
								#(#each_feild),*
							})
						}
					},
					Unnamed(ref n)=> {
						
						let each_feild = n.unnamed.iter().enumerate().map(|(i,m)|{
							let ty = &m.ty;
							quote!{ #ty::dewoodify(&li[#i])? }
						});
						
						let number_of_fields = each_feild.len();
						
						quote!{
							let li = v.tail().as_slice();
							
							if li.len() != #number_of_fields {
								return Err(Box::new(wood::WoodError::new(v, format!("variant {} expected {} elements, found {}", stringify!(#variant_name), #number_of_fields, li.len()))));
							}
							
							Ok(#name::#variant_name(
								#(#each_feild),*
							))
						}
					},
					Unit=> {
						quote!{ Ok(#name::#variant_name) }
					}
				};
				
				quote!{
					stringify!(#variant_name)=> {
						#for_fields
					}
				}
			}).collect();
			
			quote!{
				impl Dewoodable for #name {
					fn dewoodify(v:&wood::Wood)-> Result<Self, Box<wood::WoodError>> {
						match v.initial_str() {
							#(#variant_cases,)*
							erc => Err(Box::new(wood::WoodError::new(v, format!("expected a {}, but no variant of {} is called {}", stringify!(#name), stringify!(#name), erc)))),
						}
					}
				}
			}
		},
		
		
		Union(_)=> {
			panic!("derive(Dewoodable) doesn't yet support unions")
		}
	});
	
	ret
}




// I think, to complete this API, we kinda need something like this:
// define_struct_biwooder!(ProfileBiwooder {
//     id "uuid": Base64Bi,
//     name: Ident, 
//     description: Ident, 
// })