mirror of
https://github.com/edera-dev/sprout.git
synced 2025-12-20 02:40:18 +00:00
initial actions, entries, generators mechanism!
This commit is contained in:
50
src/generators/matrix.rs
Normal file
50
src/generators/matrix.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use crate::config::{EntryDeclaration, MatrixConfiguration};
|
||||
use crate::context::Context;
|
||||
use std::collections::BTreeMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn build_matrix(input: &BTreeMap<String, Vec<String>>) -> Vec<BTreeMap<String, String>> {
|
||||
let items: Vec<(String, Vec<String>)> = input.clone().into_iter().collect();
|
||||
let mut result: Vec<BTreeMap<String, String>> = vec![BTreeMap::new()];
|
||||
|
||||
for (key, values) in items {
|
||||
let mut new_result = Vec::new();
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn generate(
|
||||
context: Rc<Context>,
|
||||
matrix: &MatrixConfiguration,
|
||||
) -> Vec<(Rc<Context>, EntryDeclaration)> {
|
||||
let combinations = build_matrix(&matrix.values);
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for combination in combinations {
|
||||
let mut context = context.fork();
|
||||
context.insert(&combination);
|
||||
let context = context.freeze();
|
||||
|
||||
let mut entry = matrix.entry.clone();
|
||||
entry.title = context.stamp(&entry.title);
|
||||
entry.actions = entry
|
||||
.actions
|
||||
.into_iter()
|
||||
.map(|action| context.stamp(action))
|
||||
.collect();
|
||||
entries.push((context, entry));
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
Reference in New Issue
Block a user