Path: blob/main/crates/bevy_reflect/derive/src/result_sifter.rs
6599 views
/// Helper struct used to process an iterator of `Result<Vec<T>, syn::Error>`,1/// combining errors into one along the way.2pub(crate) struct ResultSifter<T> {3items: Vec<T>,4errors: Option<syn::Error>,5}67impl<T> Default for ResultSifter<T> {8fn default() -> Self {9Self {10items: Vec::new(),11errors: None,12}13}14}1516impl<T> ResultSifter<T> {17/// Sift the given result, combining errors if necessary.18pub fn sift(&mut self, result: Result<T, syn::Error>) {19match result {20Ok(data) => self.items.push(data),21Err(err) => {22if let Some(ref mut errors) = self.errors {23errors.combine(err);24} else {25self.errors = Some(err);26}27}28}29}3031/// Associated method that provides a convenient implementation for [`Iterator::fold`].32pub fn fold(mut sifter: Self, result: Result<T, syn::Error>) -> Self {33sifter.sift(result);34sifter35}3637/// Complete the sifting process and return the final result.38pub fn finish(self) -> Result<Vec<T>, syn::Error> {39if let Some(errors) = self.errors {40Err(errors)41} else {42Ok(self.items)43}44}45}464748