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 <[email protected]>
  • Loading branch information
mulkieran committed Sep 4, 2024
1 parent de10442 commit 7564ed0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 69 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
87 changes: 38 additions & 49 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 @@ -193,59 +193,48 @@ fn wrap_method(f: &mut ImplItemMethod) {
/// Get the pool available action state level at which a pool operation ceases to be
/// accepted.
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"))
{
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\")]");
}
if let Some((index, return_value)) = attrs
.iter()
.enumerate()
.filter(|(_, a)| a.path().is_ident("pool_mutating_action"))
.map(|(i, a)| {
if let Meta::List(MetaList { ref tokens, .. }) = a.meta {
if let TokenTree::Literal(litstr) = tokens.clone().into_iter().next().expect("stratisd code always supplies string literal argument to pool_mutating_actions") {
(i, parse_str::<Ident>(litstr.to_string().trim_matches('"')).expect("stratisd code uses only valid identifier strings"))
} else {
panic!("pool_mutating_action attribute must be in form #[pool_mutating_action(\"REJECTION LEVEL\")]")
}
} else {
panic!("pool_mutating_action attribute must be in form #[pool_mutating_action(\"REJECTION LEVEL\")]")
}
})
.last() {
attrs.remove(index);
Some(return_value)
} else {
None
}
}
if let Some(i) = index {
attrs.remove(i);
}
return_value
}

/// 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;
if let Some(index) = attrs
.iter()
.enumerate()
.filter_map(|(i, a)| {
if a.path().is_ident(attribute) {
Some(i)
} else {
None
}
}
}
if let Some(i) = index {
attrs.remove(i);
})
.last()
{
attrs.remove(index);
true
} else {
false
}
return_value
}

/// Determine whether a method should be marked as needing to handle failed rollback
Expand All @@ -265,7 +254,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 7564ed0

Please sign in to comment.