| 1 | use syn::parse::Error; |
| 2 | use syn::spanned::Spanned; |
| 3 | |
| 4 | /// Return the value that fulfills the predicate if there is one in the slice. Panic if there is |
| 5 | /// more than one. |
| 6 | pub fn find_only<T, F>(iter: impl Iterator<Item = T>, pred: F) -> Result<Option<T>, Error> |
| 7 | where |
| 8 | T: Spanned, |
| 9 | F: Fn(&T) -> Result<bool, Error>, |
| 10 | { |
| 11 | let mut result: Option = None; |
| 12 | for item: T in iter { |
| 13 | if pred(&item)? { |
| 14 | if result.is_some() { |
| 15 | return Err(Error::new(item.span(), message:"Multiple defaults" )); |
| 16 | } |
| 17 | result = Some(item); |
| 18 | } |
| 19 | } |
| 20 | Ok(result) |
| 21 | } |
| 22 | |