refactor: make arg parsing code more readable

This commit is contained in:
JasterV 2025-11-06 16:15:31 +01:00
parent aef2e05dcb
commit b37885d27c

View file

@ -40,11 +40,16 @@ pub enum TestArg {
impl TestArg {
pub fn parse_arg_with_expected_context(arg: FnArg, expected_context_type: &syn::Type) -> Self {
// Check if the argument is the context argument
if let syn::FnArg::Typed(pat_type) = &arg
&& let syn::Pat::Ident(pat_ident) = &*pat_type.pat
{
let syn::FnArg::Typed(pat_type) = &arg else {
return Self::Any(arg);
};
let syn::Pat::Ident(pat_ident) = &*pat_type.pat else {
return Self::Any(arg);
};
let arg_type = &*pat_type.ty;
// Check for mutable/immutable reference
if let syn::Type::Reference(type_ref) = arg_type
&& types_equal(&type_ref.elem, expected_context_type)
@ -55,11 +60,16 @@ impl TestArg {
ContextArgMode::Reference
};
TestArg::Context(ContextArg {
return TestArg::Context(ContextArg {
name: pat_ident.ident.clone(),
mode,
})
} else if types_equal(arg_type, expected_context_type) {
});
}
if !types_equal(arg_type, expected_context_type) {
return TestArg::Any(arg);
}
// To determine mutability for an owned type, we check the identifier pattern.
let mode = if pat_ident.mutability.is_some() {
// This catches signatures like: `mut my_ctx: ContextType`
@ -73,12 +83,6 @@ impl TestArg {
name: pat_ident.ident.clone(),
mode,
})
} else {
TestArg::Any(arg)
}
} else {
TestArg::Any(arg)
}
}
}