Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update syn / quote / proc-macro2 #29

Merged
merged 1 commit into from
Jul 29, 2024
Merged
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
53 changes: 9 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions econf-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repository = "https://github.com/YushiOMOTE/econf"
readme = "README.md"

[dependencies]
syn = "0.15"
quote = "0.6"
proc-macro2 = "0.4"
syn = "2"
quote = "1"
proc-macro2 = "1"

[lib]
proc-macro = true
47 changes: 25 additions & 22 deletions econf-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use proc_macro::TokenStream;

use proc_macro2::{Ident, TokenStream as TokenStream2};
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, Lit, Meta, NestedMeta};
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields, LitStr};

#[proc_macro_derive(LoadEnv, attributes(econf))]
pub fn load_env(input: TokenStream) -> TokenStream {
Expand All @@ -26,31 +26,34 @@ pub fn load_env(input: TokenStream) -> TokenStream {
}

fn is_skip(f: &Field) -> bool {
f.attrs.iter().any(|a| {
a.path.is_ident("econf")
&& matches!(a.parse_meta().unwrap(), Meta::List(meta) if meta.nested.iter().any(|nm| {
matches!(nm, NestedMeta::Meta(Meta::Word(word)) if *word == "skip")
}))
f.attrs.iter().any(|attr| {
if attr.path().is_ident("econf") {
if let Ok(args) = attr.parse_args::<Ident>() {
return args == "skip";
}
}

false
})
}

fn find_renaming(f: &Field) -> Option<String> {
f.attrs
.iter()
.filter(|attr| attr.path.is_ident("econf"))
.filter_map(|attr| match attr.parse_meta().unwrap() {
Meta::List(meta) => Some(meta.nested),
_ => None,
})
.flat_map(|nested| nested.into_iter())
.filter_map(|nested| match nested {
NestedMeta::Meta(Meta::NameValue(value)) if value.ident == "rename" => Some(value),
_ => None,
})
.find_map(|value| match value.lit {
Lit::Str(token) => Some(token.value()),
_ => None,
})
let mut rename = None;
for attr in &f.attrs {
if attr.path().is_ident("econf") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("rename") {
let s: LitStr = meta.value()?.parse()?;
rename = Some(s.value());
}

Ok(())
})
.expect("failed to parse nested meta");
}
}

rename
}

fn content(name: &Ident, data: &Data) -> TokenStream2 {
Expand Down
2 changes: 1 addition & 1 deletion econf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
log = "0.4"
serde = "1.0"
serde_yaml = "0.8"
econf-derive = { version = "0.2.1", path = "../econf-derive" }
econf-derive = { path = "../econf-derive" }
humantime = "2.1"

[dev-dependencies]
Expand Down
Loading