Rest/Fn/Binary/Command/
Parallel.rs

1/// Asynchronously processes entries to generate summaries and outputs the
2/// results.
3///
4/// This function performs the following steps:
5/// 1. Filters and processes the provided entries based on the given pattern and
6///    separator.
7/// 2. Spawns asynchronous tasks to generate summaries for each entry.
8/// 3. Collects the results and outputs them.
9///
10/// # Argument
11///
12/// * `Option` - A struct containing the following fields:
13///   - `Entry`: A vector of vectors, where each inner vector contains the
14///     components of a file path.
15///   - `Separator`: A character used to join the components of the file path.
16///   - `Pattern`: A string pattern to match against the last element of each
17///     entry.
18///
19/// # Example
20///
21/// ```rust
22/// let options = Option {
23
24/// 	Entry:vec![vec!["path".to_string(), "to".to_string(),
25/// "file.git".to_string()]],
26
27/// 	Separator:'/',
28
29/// 	Pattern:".git".to_string(),
30
31/// };
32
33/// Fn(options).await;
34
35/// ```
36/// 
37/// # Errors
38///
39/// This function will log errors if it fails to generate summaries or send
40/// results.
41pub async fn Fn(Option { Entry, Separator, Pattern, .. }:Option) {
42	let (Allow, mut Mark) = tokio::sync::mpsc::unbounded_channel();
43
44	let Queue = futures::stream::FuturesUnordered::new();
45
46	let glob = globset::Glob::new(&Pattern).expect("Invalid pattern").compile_matcher();
47
48	for Entry in Entry
49		.into_par_iter()
50		.filter_map(|Entry| {
51			if glob.is_match(&Entry.join(&Separator.to_string())) {
52				Some(Entry[0..Entry.len() - 1].join(&Separator.to_string()))
53			} else {
54				None
55			}
56		})
57		.collect::<Vec<String>>()
58	{
59		let Allow = Allow.clone();
60
61		Queue.push(tokio::spawn(async move {
62			match crate::Fn::Build::Fn(&Entry).await {
63				Ok(Build) => {
64					if let Err(_Error) = Allow.send((Entry, format!("{:?}", Build))) {
65						eprintln!("Cannot Allow: {}", _Error);
66					}
67				},
68
69				Err(_Error) => {
70					eprintln!("Cannot Build for {}: {}", Entry, _Error)
71				},
72			}
73		}));
74	}
75
76	tokio::spawn(async move {
77		Queue.collect::<Vec<_>>().await;
78
79		drop(Allow);
80	});
81
82	let mut Output = Vec::new();
83
84	while let Some((Entry, Build)) = Mark.recv().await {
85		Output.push((Entry, Build));
86	}
87
88	crate::Fn::Build::Group::Fn(Output);
89}
90
91use futures::stream::StreamExt;
92use rayon::iter::{IntoParallelIterator, ParallelIterator};
93
94use crate::Struct::Binary::Command::Entry::Struct as Option;