1extern crate proc_macro;
2use proc_macro::TokenStream;
3
4#[proc_macro]
5pub fn fn_simple_as(ty: TokenStream) -> TokenStream {
6 let typename = ty.to_string();
7 format!("pub fn as_{type}s(&self) -> Vector2D<{type}> {{
8 Vector2D {{
9 x: self.x as {type},
10 y: self.y as {type},
11 }}
12 }}",
13 type = typename)
14 .parse()
15 .unwrap()
16}
17
18#[proc_macro]
19pub fn fn_lower_bounded_as(toks: TokenStream) -> TokenStream {
20 const USAGE_MSG: &'static str =
21 "Invalid usage, expected: fn_lower_bounded_as!(source_type, destination_type, lower_bound)";
22
23 let mut iter = toks.into_iter();
24
25 let src_ty = iter.next().expect(USAGE_MSG);
26 iter.next().expect(USAGE_MSG); let dst_ty = iter.next().expect(USAGE_MSG);
29 iter.next().expect(USAGE_MSG); let lower_bound = iter.next().expect(USAGE_MSG);
32 if let Some(_) = iter.next() {
33 panic!(USAGE_MSG);
34 }
35
36 format!(
37 "pub fn as_{dst_type}s(&self) -> Vector2D<{dst_type}> {{
38 Vector2D {{
39 x: {src_type}::max({lower_bound}, self.x) as {dst_type},
40 y: {src_type}::max({lower_bound}, self.y) as {dst_type},
41 }}
42 }}",
43 src_type = src_ty.to_string(),
44 dst_type = dst_ty.to_string(),
45 lower_bound = lower_bound.to_string()
46 )
47 .parse()
48 .unwrap()
49}