Library/Fn/Binary/Command/
Parallel.rs

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