Improve the error message for state type inference failure in FromRequest(Parts) derive macro (#1432)

* Add a dedicated error message for state type inference issues

* Generate valid code even if state type can't be inferred

* Also error on state type inference for debug_handler
This commit is contained in:
Jonas Platte
2022-10-09 20:25:05 +02:00
committed by GitHub
parent ee0b71a4ac
commit 7cbacd1433
4 changed files with 134 additions and 107 deletions
+4 -12
View File
@@ -43,8 +43,6 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(test, allow(clippy::float_cmp))]
use std::collections::HashSet;
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse::Parse, Type};
@@ -615,11 +613,11 @@ where
}
}
fn infer_state_type<'a, I>(types: I) -> Option<Type>
fn infer_state_types<'a, I>(types: I) -> impl Iterator<Item = Type> + 'a
where
I: Iterator<Item = &'a Type>,
I: Iterator<Item = &'a Type> + 'a,
{
let state_inputs = types
types
.filter_map(|ty| {
if let Type::Path(path) = ty {
Some(&path.path)
@@ -650,13 +648,7 @@ where
None
}
})
.collect::<HashSet<_>>();
if state_inputs.len() == 1 {
state_inputs.iter().next().map(|&ty| ty.clone())
} else {
None
}
.cloned()
}
#[cfg(test)]