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 {
11        Self { steps: Vec::new() }
12    }
13
14    pub fn then<A: Algorithm<C> + 'static>(mut self, algorithm: A) -> Self {
15        self.steps.push(Box::new(algorithm));
16        self
17    }
18
19    /// Alias for `then` - adds an algorithm to the pipeline
20    #[allow(clippy::should_implement_trait)]
21    pub fn add<A: Algorithm<C> + 'static>(self, algorithm: A) -> Self {
22        self.then(algorithm)
23    }
24
25    pub fn execute(&self, grid: &mut Grid<C>, seed: u64) {
26        for (i, step) in self.steps.iter().enumerate() {
27            step.generate(grid, seed.wrapping_add(i as u64 * 1000));
28        }
29    }
30}
31
32impl<C: Cell> Default for Pipeline<C> {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38impl<C: Cell> Algorithm<C> for Pipeline<C> {
39    fn generate(&self, grid: &mut Grid<C>, seed: u64) {
40        self.execute(grid, seed);
41    }
42
43    fn name(&self) -> &'static str {
44        "Pipeline"
45    }
46}