mirror of
https://github.com/sam0x17/pub-fields.git
synced 2025-04-04 13:27:37 +03:00
working
This commit is contained in:
commit
2054845649
4 changed files with 47 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/Cargo.lock
|
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
[package]
|
||||||
|
name = "pub-fields"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
authors = ["sam0x17"]
|
||||||
|
description = "Provides a proc macro attribute that defaults all struct fields to public unless private is prepended."
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syn = { version = "2", features = ["full"]}
|
||||||
|
quote = "1"
|
17
src/lib.rs
Normal file
17
src/lib.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::ToTokens;
|
||||||
|
use syn::{parse::Nothing, parse_macro_input, parse_quote, ItemStruct};
|
||||||
|
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn pub_fields(attr: TokenStream, tokens: TokenStream) -> TokenStream {
|
||||||
|
parse_macro_input!(attr as Nothing);
|
||||||
|
let mut item_struct = parse_macro_input!(tokens as ItemStruct);
|
||||||
|
for field in &mut item_struct.fields {
|
||||||
|
field.vis = match &field.vis {
|
||||||
|
syn::Visibility::Public(p) => syn::Visibility::Public(*p),
|
||||||
|
syn::Visibility::Restricted(res) => syn::Visibility::Restricted(res.clone()),
|
||||||
|
syn::Visibility::Inherited => parse_quote!(pub),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
item_struct.to_token_stream().into()
|
||||||
|
}
|
15
tests/tests.rs
Normal file
15
tests/tests.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
mod submod {
|
||||||
|
use pub_fields::pub_fields;
|
||||||
|
|
||||||
|
#[pub_fields]
|
||||||
|
pub struct MyStruct {
|
||||||
|
a: usize,
|
||||||
|
b: usize,
|
||||||
|
c: usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_it() {
|
||||||
|
let x = submod::MyStruct { a: 3, b: 4, c: 5 };
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue