Skip to content

Commit

Permalink
Add concept of how asn proc_macro_attributes should look like #11
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Apr 22, 2020
1 parent 0db9fe9 commit 5239274
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
5 changes: 4 additions & 1 deletion asn1rs-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ proc-macro = true


[dependencies]
syn = "1.0.17"
syn = {version = "1.0.17", features = ["full"] }
quote = "1.0.3"
proc-macro2 = "1.0.10"


[dependencies.asn1rs-model]
path = "../asn1rs-model"
26 changes: 24 additions & 2 deletions asn1rs-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
extern crate proc_macro;
extern crate proc_macro2;

use asn1rs_model::gen::rust::RustCodeGenerator as RustGenerator;
use asn1rs_model::gen::Generator;
use asn1rs_model::model::Model;
use asn1rs_model::parser::Tokenizer;
use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::LitStr;
use quote::quote;
use syn::{parse_macro_input, AttributeArgs};
use syn::{Item, LitStr};

#[proc_macro]
pub fn asn_to_rust(item: TokenStream) -> TokenStream {
Expand All @@ -27,3 +29,23 @@ pub fn asn_to_rust(item: TokenStream) -> TokenStream {
.parse()
.unwrap()
}

#[proc_macro_attribute]
pub fn asn(attr: TokenStream, item: TokenStream) -> TokenStream {
let attributes = parse_macro_input!(attr as AttributeArgs);
let item = parse_macro_input!(item as Item);

let item = match item {
Item::Struct(mut strct) => {
for field in strct.fields.iter_mut() {
field
.attrs
.retain(|a| !a.path.segments.first().unwrap().ident.to_string().eq("asn"));
}
Item::Struct(strct)
}
item => item,
};

TokenStream::from(quote! {#item})
}
18 changes: 18 additions & 0 deletions tests/basic_proc_macro_attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use asn1rs_macros::asn;

#[asn(sequence)]
#[derive(Default)]
pub struct Potato {
#[asn(Integer)]
size: u64,
#[asn(Utf8String)]
string: String,
}

#[test]
fn test_compiles() {
let _ = Potato {
size: 123,
string: String::from("where is the content"),
};
}

0 comments on commit 5239274

Please sign in to comment.