terrain_forge/compose/
pipeline.rs

1//! Pipeline for sequential algorithm execution
2
3use crate::{Algorithm, Cell, Grid};
4
5pub struct Pipeline<C: Cell> {
6    steps: Vec<Box<dyn Algorithm<C>>>,
7}
8
9impl<C: Cell> Pipeline<C> {
10    pub fn new() -> Self { Self { steps: Vec::new() } }
11
12    pub fn then<A: Algorithm<C> + 'static>(mut self, algorithm: A) -> Self {
13        self.steps.push(Box::new(algorithm));
14        self
15    }
16
17    /// Alias for `then` - adds an algorithm to the pipeline
18    #[allow(clippy::should_implement_trait)]
19    pub fn add<A: Algorithm<C> + 'static>(self, algorithm: A) -> Self {
20        self.then(algorithm)
21    }
22
23    pub fn execute(&self, grid: &mut Grid<C>, seed: u64) {
24        for (i, step) in self.steps.iter().enumerate() {
25            step.generate(grid, seed.wrapping_add(i as u64 * 1000));
26        }
27    }
28}
29
30impl<C: Cell> Default for Pipeline<C> {
31    fn default() -> Self { Self::new() }
32}
33
34impl<C: Cell> Algorithm<C> for Pipeline<C> {
35    fn generate(&self, grid: &mut Grid<C>, seed: u64) {
36        self.execute(grid, seed);
37    }
38
39    fn name(&self) -> &'static str { "Pipeline" }
40}