1use proc_macro::TokenStream as CompilerTokenStream;
31use quote::{quote, quote_spanned};
32
33fn supported() -> Vec<(String, String)> {
35 let source = [
36 ("ipfs/go-ipfs", "v0.7.0"),
37 ("ipfs/go-ipfs", "v0.8.0"),
38 ("ipfs/go-ipfs", "v0.9.1"),
39 ("ipfs/go-ipfs", "v0.10.0"),
40 ("ipfs/go-ipfs", "v0.11.1"),
41 ("ipfs/go-ipfs", "v0.12.2"),
42 ("ipfs/go-ipfs", "v0.13.0"),
43 ("ipfs/kubo", "v0.14.0"),
44 ("ipfs/kubo", "v0.15.0"),
45 ("ipfs/kubo", "v0.16.0"),
46 ("ipfs/kubo", "v0.17.0"),
47 ];
48
49 source
50 .into_iter()
51 .map(|(i, t)| (i.into(), t.into()))
52 .collect()
53}
54
55fn current() -> (String, String) {
57 supported().into_iter().last().unwrap()
58}
59
60fn image_test_case(image_name: &str, image_tag: &str) -> proc_macro2::TokenStream {
61 quote! {
62 #[test_case::test_case(#image_name, #image_tag)]
63 }
64}
65
66fn unexpected_meta(meta: CompilerTokenStream) -> Option<CompilerTokenStream> {
67 let m2: proc_macro2::TokenStream = meta.into();
68
69 if let Some(m) = m2.into_iter().next() {
70 let result = quote_spanned! { m.span() =>
71 compile_error!("Macro does not expect any arguments.");
72 };
73
74 Some(result.into())
75 } else {
76 None
77 }
78}
79
80#[proc_macro_attribute]
81pub fn test_current_image(
82 meta: CompilerTokenStream,
83 input: CompilerTokenStream,
84) -> CompilerTokenStream {
85 if let Some(err) = unexpected_meta(meta) {
86 err
87 } else {
88 let (image_name, image_tag) = current();
89
90 let tokens = vec![image_test_case(&image_name, &image_tag), input.into()];
91
92 let result = quote! {
93 #(#tokens)*
94 };
95
96 result.into()
97 }
98}
99
100#[proc_macro_attribute]
101pub fn test_supported_images(
102 meta: CompilerTokenStream,
103 input: CompilerTokenStream,
104) -> CompilerTokenStream {
105 if let Some(err) = unexpected_meta(meta) {
106 err
107 } else {
108 let mut tokens: Vec<_> = supported()
109 .iter()
110 .map(|(image_name, image_tag)| {
111 quote! {
112 #[test_case::test_case(#image_name, #image_tag)]
113 }
114 })
115 .collect();
116
117 tokens.push(input.into());
118
119 let result = quote! {
120 #(#tokens)*
121 };
122
123 result.into()
124 }
125}