✨ Añade AutoDefault para derivar Default avanzado
This commit is contained in:
parent
bceb43e6d0
commit
3bb2355b4f
11 changed files with 303 additions and 8 deletions
21
helpers/pagetop-macros/src/smart_default/util.rs
Normal file
21
helpers/pagetop-macros/src/smart_default/util.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use syn::parse::Error;
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Return the value that fulfills the predicate if there is one in the slice. Panic if there is
|
||||
/// more than one.
|
||||
pub fn find_only<T, F>(iter: impl Iterator<Item = T>, pred: F) -> Result<Option<T>, Error>
|
||||
where
|
||||
T: Spanned,
|
||||
F: Fn(&T) -> Result<bool, Error>,
|
||||
{
|
||||
let mut result = None;
|
||||
for item in iter {
|
||||
if pred(&item)? {
|
||||
if result.is_some() {
|
||||
return Err(Error::new(item.span(), "Multiple defaults"));
|
||||
}
|
||||
result = Some(item);
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue