carton_macros/
lib.rs

1// Copyright 2023 Vivek Panyam
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use proc_macro::TokenStream;
16use quote::quote;
17
18// Nested repeating macros get complex with declarative macros
19// so we'll use a proc macro instead
20// https://github.com/rust-lang/rust/issues/35853
21#[proc_macro]
22pub fn for_each_carton_type(item: TokenStream) -> TokenStream {
23    let item = proc_macro2::TokenStream::from(item);
24    quote! {
25
26            // Declare the inner macro
27            macro_rules! inner {
28                ($( ( $CartonType:ident, $RustType:ty, $TypeStr:literal ) ), * ) => {
29                    #item
30                };
31            }
32
33            // Call it for each type
34            inner!(
35                (Float, f32, "float32"),
36                (Double, f64, "float64"),
37                (String, String, "string"),
38                (I8, i8, "int8"),
39                (I16, i16, "int16"),
40                (I32, i32, "int32"),
41                (I64, i64, "int64"),
42                (U8, u8, "uint8"),
43                (U16, u16, "uint16"),
44                (U32, u32, "uint32"),
45                (U64, u64, "uint64")
46            );
47    }
48    .into()
49}
50
51#[proc_macro]
52pub fn for_each_numeric_carton_type(item: TokenStream) -> TokenStream {
53    let item = proc_macro2::TokenStream::from(item);
54    quote! {
55
56            // Declare the inner macro
57            macro_rules! inner {
58                ($( ( $CartonType:ident, $RustType:ty, $TypeStr:literal ) ), * ) => {
59                    #item
60                };
61            }
62
63            // Call it for each type
64            inner!(
65                (Float, f32, "float32"),
66                (Double, f64, "float64"),
67                (I8, i8, "int8"),
68                (I16, i16, "int16"),
69                (I32, i32, "int32"),
70                (I64, i64, "int64"),
71                (U8, u8, "uint8"),
72                (U16, u16, "uint16"),
73                (U32, u32, "uint32"),
74                (U64, u64, "uint64")
75            );
76    }
77    .into()
78}