Skip to main content

Library/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/// 	Entry:vec![vec!["path".to_string(), "to".to_string(), "file.git".to_string()]],
24/// 	Separator:'/',
25/// 	Pattern:".git".to_string(),
26/// };
27/// Fn(options).await;
28/// ```
29///
30/// # Errors
31///
32/// This function will log errors if it fails to generate summaries or send
33/// results.
34pub async fn Fn(Option { Entry, Separator, Pattern, .. }:Option) {
35	let (Allow, mut Mark) = tokio::sync::mpsc::unbounded_channel();
36
37	let Queue = futures::stream::FuturesUnordered::new();
38
39	let glob = globset::Glob::new(&Pattern).expect("Invalid pattern").compile_matcher();
40
41	for Entry in Entry
42		.into_par_iter()
43		.filter_map(|Entry| {
44			if glob.is_match(&Entry.join(&Separator.to_string())) {
45				Some(Entry[0..Entry.len() - 1].join(&Separator.to_string()))
46			} else {
47				None
48			}
49		})
50		.collect::<Vec<String>>()
51	{
52		let Allow = Allow.clone();
53
54		Queue.push(tokio::spawn(async move {
55			match crate::Fn::Build::Fn(&Entry).await {
56				Ok(Build) => {
57					if let Err(_Error) = Allow.send((Entry, format!("{:?}", Build))) {
58						eprintln!("Cannot Allow: {}", _Error);
59					}
60				},
61
62				Err(_Error) => {
63					eprintln!("Cannot Build for {}: {}", Entry, _Error)
64				},
65			}
66		}));
67	}
68
69	tokio::spawn(async move {
70		Queue.collect::<Vec<_>>().await;
71
72		drop(Allow);
73	});
74
75	let mut Output = Vec::new();
76
77	while let Some((Entry, Build)) = Mark.recv().await {
78		Output.push((Entry, Build));
79	}
80
81	crate::Fn::Build::Group::Fn(Output);
82}
83
84use futures::stream::StreamExt;
85use rayon::iter::{IntoParallelIterator, ParallelIterator};
86
87use crate::Struct::Binary::Command::Entry::Struct as Option;