2025-10-11 14:35:29 -07:00
|
|
|
use crate::context::SproutContext;
|
2025-10-24 16:32:48 -07:00
|
|
|
use crate::entries::{BootableEntry, EntryDeclaration};
|
2025-10-11 14:35:29 -07:00
|
|
|
use anyhow::Result;
|
2025-10-11 14:11:31 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2025-10-04 23:12:01 -07:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Matrix generator configuration.
|
|
|
|
|
/// The matrix generator produces multiple entries based
|
|
|
|
|
/// on input values multiplicatively.
|
2025-10-11 14:11:31 -07:00
|
|
|
#[derive(Serialize, Deserialize, Default, Clone)]
|
|
|
|
|
pub struct MatrixConfiguration {
|
2025-10-20 00:06:46 -07:00
|
|
|
/// The template entry to use for each generated entry.
|
2025-10-11 14:11:31 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub entry: EntryDeclaration,
|
2025-10-20 00:06:46 -07:00
|
|
|
/// The values to use as the input for the matrix.
|
2025-10-11 14:11:31 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub values: BTreeMap<String, Vec<String>>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Builds out multiple generations of `input` based on a matrix style.
|
|
|
|
|
/// For example, if input is: {"x": ["a", "b"], "y": ["c", "d"]}
|
|
|
|
|
/// It will produce:
|
|
|
|
|
/// x: a, y: c
|
|
|
|
|
/// x: a, y: d
|
|
|
|
|
/// x: b, y: c
|
|
|
|
|
/// x: b, y: d
|
2025-10-04 23:12:01 -07:00
|
|
|
fn build_matrix(input: &BTreeMap<String, Vec<String>>) -> Vec<BTreeMap<String, String>> {
|
2025-10-20 00:06:46 -07:00
|
|
|
// Convert the input into a vector of tuples.
|
2025-10-04 23:12:01 -07:00
|
|
|
let items: Vec<(String, Vec<String>)> = input.clone().into_iter().collect();
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// The result is a vector of maps.
|
2025-10-04 23:12:01 -07:00
|
|
|
let mut result: Vec<BTreeMap<String, String>> = vec![BTreeMap::new()];
|
|
|
|
|
|
|
|
|
|
for (key, values) in items {
|
|
|
|
|
let mut new_result = Vec::new();
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Produce all the combinations of the input values.
|
2025-10-04 23:12:01 -07:00
|
|
|
for combination in &result {
|
|
|
|
|
for value in &values {
|
|
|
|
|
let mut new_combination = combination.clone();
|
|
|
|
|
new_combination.insert(key.clone(), value.clone());
|
|
|
|
|
new_result.push(new_combination);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = new_result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.into_iter().filter(|item| !item.is_empty()).collect()
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Generates a set of entries using the specified `matrix` configuration in the `context`.
|
2025-10-04 23:12:01 -07:00
|
|
|
pub fn generate(
|
2025-10-11 14:35:29 -07:00
|
|
|
context: Rc<SproutContext>,
|
2025-10-04 23:12:01 -07:00
|
|
|
matrix: &MatrixConfiguration,
|
2025-10-24 16:32:48 -07:00
|
|
|
) -> Result<Vec<BootableEntry>> {
|
2025-10-20 00:06:46 -07:00
|
|
|
// Produce all the combinations of the input values.
|
2025-10-04 23:12:01 -07:00
|
|
|
let combinations = build_matrix(&matrix.values);
|
|
|
|
|
let mut entries = Vec::new();
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// For each combination, create a new context and entry.
|
2025-10-24 16:32:48 -07:00
|
|
|
for (index, combination) in combinations.into_iter().enumerate() {
|
2025-10-04 23:12:01 -07:00
|
|
|
let mut context = context.fork();
|
2025-10-20 00:06:46 -07:00
|
|
|
// Insert the combination into the context.
|
2025-10-04 23:12:01 -07:00
|
|
|
context.insert(&combination);
|
|
|
|
|
let context = context.freeze();
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Stamp the entry title and actions from the template.
|
2025-10-04 23:12:01 -07:00
|
|
|
let mut entry = matrix.entry.clone();
|
|
|
|
|
entry.actions = entry
|
|
|
|
|
.actions
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|action| context.stamp(action))
|
|
|
|
|
.collect();
|
2025-10-20 00:06:46 -07:00
|
|
|
// Push the entry into the list with the new context.
|
2025-10-24 16:32:48 -07:00
|
|
|
entries.push(BootableEntry::new(
|
|
|
|
|
index.to_string(),
|
|
|
|
|
entry.title.clone(),
|
|
|
|
|
context,
|
|
|
|
|
entry,
|
|
|
|
|
));
|
2025-10-04 23:12:01 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-11 14:35:29 -07:00
|
|
|
Ok(entries)
|
2025-10-04 23:12:01 -07:00
|
|
|
}
|