use syn::spanned::Spanned; use syn::parse::Error; /// 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(iter: impl Iterator, pred: F) -> Result, Error> where T: Spanned, F: Fn(&T) -> Result, { 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) } pub fn single_value(mut it: impl Iterator) -> Option { if let Some(result) = it.next() { if it.next().is_none() { return Some(result) } } None }