Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proc_macro2_helper"
version = "0.2.8"
version = "0.2.9"
authors = ["Jasper Visser <jasperav@hotmail.com>"]
edition = "2018"
description = "Various utility code to extract data that can be used with proc macro2"
Expand Down
4 changes: 2 additions & 2 deletions example/some_proc_crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ pub fn for_struct(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let fields = proc_macro2_helper::named_struct_fields_from_data(input.data);
let attrs = proc_macro2_helper::filter_attributes_from_fields(&fields, "someattr");

// Only 1 field should have the option type
// Only 2 fields should have the option type
let options = fields
.iter()
.filter(|field| proc_macro2_helper::has_first_type_option(field))
.count();

assert_eq!(1, options);
assert_eq!(2, options);

// Only 2 attributes have an attribute 'someattr'
assert_eq!(2, attrs.len());
Expand Down
3 changes: 2 additions & 1 deletion example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ struct SomeStruct {
field1: &'static str,
#[someattr]
field2: String,
nullable: Option<i32>
nullable: Option<i32>,
qualified_option: std::option::Option<i32>,
}

#[allow(dead_code)]
Expand Down
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,21 @@ pub fn has_first_type_option(field: &Field) -> bool {

pub fn has_first_type(field: &Field, ty: &str) -> bool {
match &field.ty {
Type::Path(path) => path.path.segments[0].ident == ty,
Type::Path(path) => {
let ident = &path.path.segments[0].ident;

if ident == ty {
true
} else if ident == "std" {
if path.path.segments.len() < 3 {
return false;
}

path.path.segments[2].ident == ty
} else {
false
}
}
_ => false,
}
}
Expand Down