dataport-macros 0.1.0

Macros for dataport
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright © 2026 Stephan Kunz
//! Macro implementations for port and port collection creation.

#![allow(unused, dead_code)]

use std::any::{Any, TypeId};

//use darling::FromMeta; // needed for Debug!
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::ToTokens;
use quote::quote;
use syn::LitChar;
use syn::{
	Expr, Ident, LitStr, Result, Token, Type, TypeGroup, TypeParam,
	parse::{Parse, ParseStream},
	parse_macro_input,
};

pub enum PortVariant {
	InBound,
	InOutBound,
	OutBound,
}

pub struct Params {
	pub port_name: TokenStream,
	pub port_type: Option<Type>,
	pub port_value: Option<Expr>,
	pub collection: Option<Ident>,
}

impl Parse for Params {
	fn parse(input: ParseStream) -> Result<Self> {
		if input.is_empty() {
			panic!("macro needs at least one tuple of two comma separated elements");
		}

		let old = input;
		let port_name = if let Ok(name) = input.parse::<LitStr>() {
			quote! {#name}
		} else {
			let value = old.parse::<Ident>()?;
			quote! {#value}
		};
		input.parse::<Token![,]>()?; // consume separator

		let mut port_type = None;
		let mut port_value = None;
		let mut collection = None;

		let old = input;
		if let Ok(ty) = input.parse::<Type>() {
			port_type = Some(ty);
			if !input.is_empty() {
				input.parse::<Token![,]>()?; // consume separator
				port_value = Some(old.parse::<Expr>()?);
			}
			if !input.is_empty() {
				input.parse::<Token![,]>()?; // consume separator
				collection = Some(old.parse::<Ident>()?);
			}
		} else {
			port_value = Some(old.parse::<Expr>()?)
		}

		Ok(Params {
			port_name,
			port_type,
			port_value,
			collection,
		})
	}
}

/// Should be same as in src, currently cannot be exported.
fn is_port_collection_pointer(name: &str) -> bool {
	name == "{=}" || (name.starts_with('{') && name.ends_with('}') && is_valid_port_name(&name[1..name.len() - 1]))
}

/// Should be same as in src, currently cannot be exported.
fn is_valid_port_name(name: &str) -> bool {
	if name.is_empty() {
		return false;
	}

	let mut iter = name.chars();
	// check first digit beeing alphabetic
	if let Some(c) = iter.next() {
		if !c.is_alphabetic() {
			return false;
		}
	} else {
		return false;
	}

	// check the rest for allowed chars
	for c in iter {
		if !c.is_alphanumeric() && c != '_' && c != ' ' {
			return false;
		}
	}

	true
}

pub fn quote_from_input(
	name: TokenStream,
	tp: Type,
	value: Expr,
	collection: Option<Ident>,
	variant: PortVariant,
	parseable: bool,
) -> TokenStream {
	// If 'port_type' is given, the 'port_value' may contain
	// a 'T', a 'str' or a 'String' type or a reference to one of these.
	// If 'port_type' is not the same as the type of 'port_value', we need to
	// - check, whether 'port_value' contains a port collection pointer,
	//   - if not: use a conversion from either str or String type resp references to them,
	//   - if yes: do the initialization needs from another port collection.
	let res = match value.clone() {
		Expr::Lit(expr_lit) => {
			// Something like "42", 42, "{pointer}",
			let lit = expr_lit.lit;
			match lit {
				// a str -> use .into()
				syn::Lit::Str(lit_str) => {
					// check for pointer resp. conversion necessary
					let lit_value = lit_str.value();
					if is_port_collection_pointer(&lit_str.value()) {
						// &str containing a collection pointer
						// check for collection
						if let Some(collection) = collection {
							// determine the name
							let other_name = if &lit_value == "{=}" {
								name.clone()
							} else {
								let other = &lit_value[1..lit_value.len() - 1];
								quote! {#other}
							};
							quote_from_collection(variant, name, tp, collection.to_token_stream(), other_name, parseable)
						} else {
							panic!("missing collection")
						}
					} else {
						// &str
						quote_from_str(variant, name, tp, value.to_token_stream(), parseable)
					}
				}
				// a char
				syn::Lit::Char(lit_char) => quote_from_char(variant, name, tp, lit_char, parseable),
				_ => quote_from_type_value(variant, name, tp, value.to_token_stream(), parseable),
			}
		}
		Expr::Reference(expr_reference) => {
			let reference = expr_reference.expr;
			match *reference {
				// &String::from(..) which may contain a collection pointer
				Expr::Call(expr_call) => match *expr_call.func {
					Expr::Path(expr_path) => {
						// check for collection pointer
						todo!("something like &String::from(...)");
					}
					_ => quote_from_type_value(variant, name, tp, value.to_token_stream(), parseable),
				},
				// &&str
				Expr::Path(expr_path) => {
					let derefed = quote! {*{#value}};
					quote_from_str(variant, name, tp, derefed, parseable)
				}
				// &'T', &char, &&str which may contain a collection pointer,
				Expr::Lit(expr_lit) => {
					let lit = expr_lit.lit;
					match lit {
						// &&str which may contain a pointer
						// check for pointer resp. conversion necessary
						syn::Lit::Str(lit_str) => {
							if is_port_collection_pointer(&lit_str.value()) {
								// check for collection
								if let Some(collection) = collection {
									// determine the name
									let lit_value = lit_str.value();
									let other_name = if &lit_value == "{=}" {
										name.clone()
									} else {
										let other = &lit_value[1..lit_value.len() - 1];
										quote! {#other}
									};
									quote_from_collection(
										variant,
										name,
										tp,
										collection.to_token_stream(),
										other_name,
										parseable,
									)
								} else {
									panic!("missing collection")
								}
							} else {
								quote_from_str(variant, name, tp, value.to_token_stream(), parseable)
							}
						}
						// &char
						syn::Lit::Char(lit_char) => quote_from_char(variant, name, tp, lit_char, parseable),
						// &'T', where T is not in above cases -> dereference it
						_ => {
							let derefed = quote! {*{#value}};
							quote_from_type_value(variant, name, tp, derefed, parseable)
						}
					}
				}
				_ => quote_from_type_value(variant, name, tp, value.to_token_stream(), parseable),
			}
		}
		//Expr::Call(expr_call) => {
		//	// @TODO: this should be implemented
		//	// Call to String::from(...) etc
		//	todo!("something like String::from(...)")
		//}
		//Expr::MethodCall(expr_method_call) => {
		//	// @TODO: this should be implemented
		//	// A method call like T.clone(), str.clone() or String.clone()
		//	todo!("something like <var>.clone()")
		//}
		//Expr::Path(expr_path) => {
		//	//@TODO: this should be implemented
		//	// An item which might be a str or String
		//	todo!("something like <var>")
		//}
		_ => quote_from_type_value(variant, name, tp, value.to_token_stream(), parseable),
	};
	res.into()
}

pub fn quote_from_type(variant: PortVariant, name: TokenStream, tp: Type, parseable: bool) -> TokenStream {
	if parseable {
		match variant {
			PortVariant::InBound => quote! {
				({#name}.into(), dataport::PortVariant::InBound(dataport::InBound::new_parseable::<#tp>()))
			},
			PortVariant::InOutBound => quote! {
				({#name}.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::new_parseable::<#tp>()))
			},
			PortVariant::OutBound => quote! {
				({#name}.into(), dataport::PortVariant::OutBound(dataport::OutBound::new_parseable::<#tp>()))
			},
		}
	} else {
		match variant {
			PortVariant::InBound => quote! {
				({#name}.into(), dataport::PortVariant::InBound(dataport::InBound::new::<#tp>()))
			},
			PortVariant::InOutBound => quote! {
				({#name}.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::new::<#tp>()))
			},
			PortVariant::OutBound => quote! {
				({#name}.into(), dataport::PortVariant::OutBound(dataport::OutBound::new::<#tp>()))
			},
		}
	}
}

pub fn quote_from_value(variant: PortVariant, name: TokenStream, value: TokenStream, parseable: bool) -> TokenStream {
	if parseable {
		match variant {
			PortVariant::InBound => quote! {
				({#name}.into(), dataport::PortVariant::InBound(dataport::InBound::with_value_parseable({#value})))
			},
			PortVariant::InOutBound => quote! {
				({#name}.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value_parseable({#value})))
			},
			PortVariant::OutBound => quote! {
				({#name}.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value_parseable({#value})))
			},
		}
	} else {
		match variant {
			PortVariant::InBound => quote! {
				({#name}.into(), dataport::PortVariant::InBound(dataport::InBound::with_value({#value})))
			},
			PortVariant::InOutBound => quote! {
				({#name}.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value({#value})))
			},
			PortVariant::OutBound => quote! {
				({#name}.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value({#value})))
			},
		}
	}
}

fn quote_from_type_value(
	variant: PortVariant,
	name: TokenStream,
	tp: Type,
	value: TokenStream,
	parseable: bool,
) -> TokenStream {
	if parseable {
		match variant {
			PortVariant::InBound => quote! {
				({#name}.into(), dataport::PortVariant::InBound(dataport::InBound::with_value_parseable::<#tp>({#value})))
			},
			PortVariant::InOutBound => quote! {
				({#name}.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value_parseable::<#tp>({#value})))
			},
			PortVariant::OutBound => quote! {
				({#name}.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value_parseable::<#tp>({#value})))
			},
		}
	} else {
		match variant {
			PortVariant::InBound => quote! {
				({#name}.into(), dataport::PortVariant::InBound(dataport::InBound::with_value::<#tp>({#value})))
			},
			PortVariant::InOutBound => quote! {
				({#name}.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value::<#tp>({#value})))
			},
			PortVariant::OutBound => quote! {
				({#name}.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value::<#tp>({#value})))
			},
		}
	}
}

fn quote_from_collection(
	variant: PortVariant,
	name: TokenStream,
	tp: Type,
	collection: TokenStream,
	other_name: TokenStream,
	parseable: bool,
) -> TokenStream {
	if parseable {
		match variant {
			PortVariant::InBound => quote! {
				(#name.into(), dataport::PortVariant::InBound(dataport::InBound::from_collection_parseable::<#tp>({#collection}, #other_name)?))
			},
			PortVariant::InOutBound => quote! {
				(#name.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::from_collection_parseable::<#tp>({#collection}, #other_name)?))
			},
			PortVariant::OutBound => quote! {
				(#name.into(), dataport::PortVariant::OutBound(dataport::OutBound::from_collection_parseable::<#tp>({#collection}, #other_name)?))
			},
		}
	} else {
		match variant {
			PortVariant::InBound => quote! {
				(#name.into(), dataport::PortVariant::InBound(dataport::InBound::from_collection::<#tp>({#collection}, #other_name)?))
			},
			PortVariant::InOutBound => quote! {
				(#name.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::from_collection::<#tp>({#collection}, #other_name)?))
			},
			PortVariant::OutBound => quote! {
				(#name.into(), dataport::PortVariant::OutBound(dataport::OutBound::from_collection::<#tp>({#collection}, #other_name)?))
			},
		}
	}
}

fn quote_from_str(variant: PortVariant, name: TokenStream, tp: Type, value: TokenStream, parseable: bool) -> TokenStream {
	if parseable {
		match variant {
			PortVariant::InBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str({#value})?;
					(#name.into(), dataport::PortVariant::InBound(dataport::InBound::with_value_parseable::<#tp>(my_value)))
				}
			},
			PortVariant::InOutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str({#value})?;
					(#name.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value_parseable::<#tp>(my_value)))
				}
			},
			PortVariant::OutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str({#value})?;
					(#name.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value_parseable::<#tp>(my_value)))
				}
			},
		}
	} else {
		match variant {
			PortVariant::InBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str({#value})?;
					(#name.into(), dataport::PortVariant::InBound(dataport::InBound::with_value::<#tp>(my_value)))
				}
			},
			PortVariant::InOutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str({#value})?;
					(#name.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value::<#tp>(my_value)))
				}
			},
			PortVariant::OutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str({#value})?;
					(#name.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value::<#tp>(my_value)))
				}
			},
		}
	}
}

fn quote_from_char(variant: PortVariant, name: TokenStream, tp: Type, character: LitChar, parseable: bool) -> TokenStream {
	if parseable {
		match variant {
			PortVariant::InBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str(#character.encode_utf8(&mut[0; 4]))?;
					(#name.into(), dataport::PortVariant::InBound(dataport::InBound::with_value_parseable::<#tp>(my_value)))
				}
			},
			PortVariant::InOutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str(#character.encode_utf8(&mut[0; 4]))?;
					(#name.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value_parseable::<#tp>(my_value)))
				}
			},
			PortVariant::OutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str(#character.encode_utf8(&mut[0; 4]))?;
					(#name.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value_parseable::<#tp>(my_value)))
				}
			},
		}
	} else {
		match variant {
			PortVariant::InBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str(#character.encode_utf8(&mut[0; 4]))?;
					(#name.into(), dataport::PortVariant::InBound(dataport::InBound::with_value::<#tp>(my_value)))
				}
			},
			PortVariant::InOutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str(#character.encode_utf8(&mut[0; 4]))?;
					(#name.into(), dataport::PortVariant::InOutBound(dataport::InOutBound::with_value::<#tp>(my_value)))
				}
			},
			PortVariant::OutBound => quote! {
				{
					let my_value: #tp = core::str::FromStr::from_str(#character.encode_utf8(&mut[0; 4]))?;
					(#name.into(), dataport::PortVariant::OutBound(dataport::OutBound::with_value::<#tp>(my_value)))
				}
			},
		}
	}
}