Skip to main content

terrain_forge/compose/
pipeline.rs

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