1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use futures::{stream::FuturesUnordered, StreamExt};
use nunny::Vec as NonEmpty;

/// Helper function to collect errors from async validations.
pub async fn collect_errs<E>(
    mut handles: FuturesUnordered<tokio::task::JoinHandle<Result<(), E>>>,
) -> Result<(), NonEmpty<E>> {
    let mut errors = Vec::new();

    while let Some(result) = handles.next().await {
        if let Ok(Err(e)) = result {
            errors.push(e);
        }
    }

    match errors.try_into() {
        Ok(it) => Err(it),
        Err(_) => Ok(()),
    }
}