Skip to content

Commit

Permalink
Increase syn dependency lower bound to 2.0.0
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <amulhern@redhat.com>
  • Loading branch information
mulkieran committed Sep 4, 2024
1 parent de10442 commit 2718ccb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 59 deletions.
27 changes: 8 additions & 19 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion stratisd_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc-macro2 = "1.0.27"
quote = "1.0.9"

[dependencies.syn]
version = "1.0.73"
version = "2.0.0"
features = ["full", "extra-traits"]

[lints.rust]
Expand Down
73 changes: 34 additions & 39 deletions stratisd_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use proc_macro2::{Group, Span, TokenStream as TokenStream2, TokenTree};
use quote::quote;
use syn::{
parse, parse_str, punctuated::Punctuated, token::Comma, Attribute, Block, FnArg, Ident,
ImplItem, ImplItemMethod, Item, Lit, Meta, MetaList, NestedMeta, Pat, PatIdent, PatType, Path,
PathSegment, Receiver, ReturnType, Stmt, Token, Type, TypePath,
ImplItem, ImplItemFn, Item, Meta, MetaList, Pat, PatIdent, PatType, Path, PathSegment,
Receiver, ReturnType, Stmt, Token, Type, TypePath,
};

/// Add guard for mutating actions when the pool is in maintenance mode.
///
/// This method adds a statement that returns an error if the pool is set
/// to limit available actions.
fn add_method_guards(method: &mut ImplItemMethod, level: Ident) {
fn add_method_guards(method: &mut ImplItemFn, level: Ident) {
let stmt = if let ReturnType::Type(_, ty) = &method.sig.output {
if let Type::Path(TypePath {
path: Path { segments, .. },
Expand Down Expand Up @@ -136,7 +136,7 @@ fn process_token_stream(token: TokenTree) -> TokenTree {

/// Take the body of the method and wrap it in an inner method, replacing the
/// body with a check for an error that limits available actions.
fn wrap_method(f: &mut ImplItemMethod) {
fn wrap_method(f: &mut ImplItemFn) {
let wrapped_ident = Ident::new(
format!("{}_wrapped", f.sig.ident.clone()).as_str(),
Span::call_site(),
Expand Down Expand Up @@ -195,28 +195,22 @@ fn wrap_method(f: &mut ImplItemMethod) {
fn get_attr_level(attrs: &mut Vec<Attribute>) -> Option<Ident> {
let mut return_value = None;
let mut index = None;
for (i, attr) in attrs.iter().enumerate() {
if let Meta::List(MetaList {
ref path,
ref nested,
..
}) = attr
.parse_meta()
.unwrap_or_else(|_| panic!("Attribute {attr:?} cannot be parsed"))
for (i, attr) in attrs
.iter()
.enumerate()
.filter(|(_, a)| a.path().is_ident("pool_mutating_action"))
{
if let Meta::List(MetaList { ref tokens, .. }) = attr.meta
{
if path
== &parse_str("pool_mutating_action").expect("pool_mutating_action is valid path")
{
for nested_meta in nested.iter() {
if let NestedMeta::Lit(Lit::Str(litstr)) = nested_meta {
index = Some(i);
return_value =
Some(parse_str::<Ident>(&litstr.value()).unwrap_or_else(|_| {
panic!("{} is not a valid identifier", litstr.value())
}));
} else {
panic!("pool_mutating_action attribute must be in form #[pool_mutating_action(\"REJECTION LEVEL\")]");
}
for nested_meta in tokens.clone().into_iter() {
if let TokenTree::Literal(litstr) = nested_meta {
index = Some(i);
return_value =
Some(parse_str::<Ident>(&litstr.to_string()).unwrap_or_else(|_| {
panic!("{} is not a valid identifier", litstr.to_string())
}));
} else {
panic!("pool_mutating_action attribute must be in form #[pool_mutating_action(\"REJECTION LEVEL\")]");
}
}
}
Expand All @@ -229,23 +223,24 @@ fn get_attr_level(attrs: &mut Vec<Attribute>) -> Option<Ident> {

/// Determine whether a method has the given attribute.
fn has_attribute(attrs: &mut Vec<Attribute>, attribute: &str) -> bool {
let mut return_value = false;
let mut index = None;
for (i, attr) in attrs.iter().enumerate() {
if let Meta::Path(path) = attr
.parse_meta()
.unwrap_or_else(|_| panic!("Attribute {attr:?} cannot be parsed"))
{
if path == parse_str(attribute).expect("pool_mutating_action is valid path") {
index = Some(i);
return_value = true;
let index = attrs
.iter()
.enumerate()
.filter_map(|(i, a)| {
if a.path().is_ident(attribute) {
Some(i)
} else {
None
}
}
}
})
.last();

if let Some(i) = index {
attrs.remove(i);
true
} else {
false
}
return_value
}

/// Determine whether a method should be marked as needing to handle failed rollback
Expand All @@ -265,7 +260,7 @@ fn process_item(mut item: Item) -> Item {
};

for impl_item in i.items.iter_mut() {
if let ImplItem::Method(ref mut f) = impl_item {
if let ImplItem::Fn(ref mut f) = impl_item {
if let Some(level) = get_attr_level(&mut f.attrs) {
add_method_guards(f, level);
}
Expand Down

0 comments on commit 2718ccb

Please sign in to comment.