2025-10-11 14:35:29 -07:00
|
|
|
use crate::context::SproutContext;
|
2025-10-24 16:32:48 -07:00
|
|
|
use crate::entries::BootableEntry;
|
2025-10-13 01:54:03 -07:00
|
|
|
use crate::generators::bls::BlsConfiguration;
|
2025-10-11 14:11:31 -07:00
|
|
|
use crate::generators::matrix::MatrixConfiguration;
|
2025-10-11 14:35:29 -07:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use anyhow::bail;
|
2025-10-11 14:11:31 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2025-10-04 23:12:01 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
2025-10-13 01:54:03 -07:00
|
|
|
pub mod bls;
|
2025-10-04 23:12:01 -07:00
|
|
|
pub mod matrix;
|
|
|
|
|
|
2025-10-19 21:44:05 -07:00
|
|
|
/// Declares a generator configuration.
|
|
|
|
|
/// Generators allow generating entries at runtime based on a set of data.
|
2025-10-27 03:37:09 -04:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
2025-10-11 14:11:31 -07:00
|
|
|
pub struct GeneratorDeclaration {
|
2025-10-19 21:44:05 -07:00
|
|
|
/// Matrix generator configuration.
|
|
|
|
|
/// Matrix allows you to specify multiple value-key values as arrays.
|
|
|
|
|
/// This allows multiplying the number of entries by any number of possible
|
|
|
|
|
/// configuration options. For example,
|
|
|
|
|
/// data.x = ["a", "b"]
|
|
|
|
|
/// data.y = ["c", "d"]
|
|
|
|
|
/// would generate an entry for each of these combinations:
|
|
|
|
|
/// x = a, y = c
|
|
|
|
|
/// x = a, y = d
|
|
|
|
|
/// x = b, y = c
|
|
|
|
|
/// x = b, y = d
|
2025-10-11 14:11:31 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub matrix: Option<MatrixConfiguration>,
|
2025-10-19 21:44:05 -07:00
|
|
|
/// BLS generator configuration.
|
|
|
|
|
/// BLS allows you to pass a filesystem path that contains a set of BLS entries.
|
|
|
|
|
/// It will generate a sprout entry for every supported BLS entry.
|
2025-10-13 01:54:03 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub bls: Option<BlsConfiguration>,
|
2025-10-11 14:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 21:44:05 -07:00
|
|
|
/// Runs the generator specified by the `generator` option.
|
|
|
|
|
/// It uses the specified `context` as the parent context for
|
|
|
|
|
/// the generated entries, injecting more values if needed.
|
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
|
|
|
generator: &GeneratorDeclaration,
|
2025-10-24 16:32:48 -07:00
|
|
|
) -> Result<Vec<BootableEntry>> {
|
2025-10-04 23:12:01 -07:00
|
|
|
if let Some(matrix) = &generator.matrix {
|
|
|
|
|
matrix::generate(context, matrix)
|
2025-10-13 01:54:03 -07:00
|
|
|
} else if let Some(bls) = &generator.bls {
|
|
|
|
|
bls::generate(context, bls)
|
2025-10-04 23:12:01 -07:00
|
|
|
} else {
|
2025-10-13 00:55:11 -07:00
|
|
|
bail!("unknown generator configuration");
|
2025-10-04 23:12:01 -07:00
|
|
|
}
|
|
|
|
|
}
|