Skip to content

Commit

Permalink
Merge pull request #102 from noritada/feat/codegen-from-wgrib2-table
Browse files Browse the repository at this point in the history
Submessage filtering improvement part 4: make minor improvements to the proc macro for code generation

This PR makes minor improvements to the proc macro.
Now the proc macro copies attributes from the source enum.
  • Loading branch information
noritada committed Sep 17, 2024
2 parents d1b8f33 + d29e485 commit 289b59b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
12 changes: 5 additions & 7 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, DeriveInput, Lit, Result, Token,
parse_macro_input, ItemEnum, Lit, Result, Token,
};

#[proc_macro_attribute]
Expand All @@ -20,19 +20,21 @@ pub fn parameter_codes(args: TokenStream, input: TokenStream) -> TokenStream {
.into();
};

let input = parse_macro_input!(input as DeriveInput);
if !is_empty_enum(&input) {
let input = parse_macro_input!(input as ItemEnum);
if !input.variants.is_empty() {
return syn::Error::new(input.ident.span(), "not an empty enum")
.into_compile_error()
.into();
}
let attrs = input.attrs;
let vis = input.vis;
let ident = input.ident;

quote! {
use std::cell::LazyCell;
use std::collections::HashMap;

#(#attrs)*
#vis enum #ident {
#entries
}
Expand All @@ -49,10 +51,6 @@ pub fn parameter_codes(args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

fn is_empty_enum(input: &DeriveInput) -> bool {
matches!(&input.data, syn::Data::Enum(enum_) if enum_.variants.is_empty())
}

#[derive(Debug)]
struct ParameterCodesArgs {
path: (String, proc_macro2::Span),
Expand Down
4 changes: 4 additions & 0 deletions codegen/tests/01-normal-case.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use grib_codegen::parameter_codes;

#[parameter_codes(path = "tests/data/table")]
#[derive(Debug, PartialEq)]
#[repr(u32)]
pub enum FooCodes {}

#[allow(dead_code)]
Expand All @@ -20,4 +22,6 @@ fn main() {
assert_eq!(FooCodes::HGT as u32, 0x_00_03_05);
assert_eq!(FooCodes::remap(&0), None);
assert_eq!(FooCodes::remap(&0x_00_03_c2), Some(FooCodes::U_GWD as u32));
assert_eq!(format!("{:?}", FooCodes::TMP), "TMP");
assert_eq!(FooCodes::TMP, FooCodes::TMP);
}
6 changes: 3 additions & 3 deletions codegen/tests/02-not-empty-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error: not an empty enum
4 | pub enum FooCodes {
| ^^^^^^^^

error: not an empty enum
--> tests/02-not-empty-enum.rs:10:12
error: expected `enum`
--> tests/02-not-empty-enum.rs:10:5
|
10 | pub struct BarCodes;
| ^^^^^^^^
| ^^^^^^

0 comments on commit 289b59b

Please sign in to comment.