Skip to content

Commit

Permalink
refactor(codegen): migrate to syn2 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor authored Mar 26, 2024
1 parent 31698b6 commit c406fc4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ edition = "2021"
proc-macro = true

[dependencies]
syn = { version = "1.0.75", features = ["full", "extra-traits"] }
quote = "1.0.9"
proc-macro2 = "1.0.28"
syn = { version = "2.0.55", features = ["full", "extra-traits"] }
quote = "1.0.35"
proc-macro2 = "1.0.79"
34 changes: 21 additions & 13 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use proc_macro::{self, TokenStream};
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse_macro_input, AttributeArgs, DeriveInput, Fields, FieldsNamed, GenericArgument, Ident,
ItemStruct, Lit, LitStr, NestedMeta, PathArguments, Type,
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
DeriveInput, Fields, FieldsNamed, GenericArgument, Ident, ItemStruct, LitStr, PathArguments,
Result, Token, Type,
};

fn extract_field(field_ident: Ident, ty: &Type) -> proc_macro2::TokenStream {
Expand Down Expand Up @@ -244,18 +247,23 @@ pub fn derive_decoder(input: TokenStream) -> TokenStream {
output.into()
}

struct Args {
types: Vec<LitStr>,
}

impl Parse for Args {
fn parse(input: ParseStream) -> Result<Self> {
let vars = Punctuated::<LitStr, Token![,]>::parse_terminated(input)?;
Ok(Args {
types: vars.into_iter().collect(),
})
}
}

#[proc_macro_attribute]
pub fn reader(metadata: TokenStream, input: TokenStream) -> TokenStream {
let types = parse_macro_input!(metadata as AttributeArgs)
.iter()
.map(|item| {
if let NestedMeta::Lit(Lit::Str(lit)) = item {
lit.clone()
} else {
unimplemented!("{:?}", item);
}
})
.collect::<Vec<LitStr>>();
let types = parse_macro_input!(metadata as Args).types;

let types_len = types.len();

let input = parse_macro_input!(input as ItemStruct);
Expand Down

0 comments on commit c406fc4

Please sign in to comment.