diff --git a/labirust/src/executor.rs b/labirust/src/executor.rs index 99b1794..b9e3320 100644 --- a/labirust/src/executor.rs +++ b/labirust/src/executor.rs @@ -92,21 +92,15 @@ impl<'m> Context<'m> { mod builder; /// A structure holding a [`Maze`] and iteratively solving it with a provided [`Algorithm`]. -pub struct Executor -where - Algo: Algorithm, -{ +pub struct Executor { delay: Duration, maze: Maze, - algorithm: Algo, + algorithm: Box, } -impl Executor -where - A: Algorithm, -{ +impl Executor { /// Constructor. - fn new(maze: Maze, algorithm: A, delay: Duration) -> Self { + fn new(maze: Maze, algorithm: Box, delay: Duration) -> Self { Self { maze, algorithm, @@ -114,22 +108,27 @@ where } } - pub fn build<'f, F, MS>(algorithm: A, builder: F) -> Self + pub fn build<'f, A, F, MS>(algorithm: A, builder: F) -> Self where + A: Algorithm + 'static, MS: BuildableMazeState, F: Fn(ExecutorBuilder) -> ExecutorBuilder, { let operation = builder; let builder = (operation)(new_builder()); let (maze, delay) = builder.build(); + let algorithm = Box::new(algorithm); Self::new(maze, algorithm, delay) } - pub fn build_dyn(algorithm: A, builder: F) -> Self + pub fn build_dyn(algorithm: Box, builder: F) -> Self where F: Fn(DynExecutorBuilder) -> DynExecutorBuilder, { - todo!() + let operation = builder; + let builder = (operation)(DynExecutorBuilder::new()); + let (maze, delay) = builder.build(); + Self::new(maze, algorithm, delay) } /// Submit the maze to the [`Algorithm`] and iteratively progress through the maze driven by said algorithm. diff --git a/labirust/src/executor/builder.rs b/labirust/src/executor/builder.rs index d08e862..994b737 100644 --- a/labirust/src/executor/builder.rs +++ b/labirust/src/executor/builder.rs @@ -11,7 +11,7 @@ pub mod maze_state { /// Describe the state of the [`Maze`] parameter in the builder of an [`crate::Executor`]. Not ment to be implemented. pub trait MazeState {} pub trait BuildableMazeState: MazeState { - fn get(self) -> Maze; + fn get(&self) -> Maze; } pub struct Unprovided; @@ -30,8 +30,8 @@ pub mod maze_state { impl MazeState for Provided {} impl BuildableMazeState for Provided { - fn get(self) -> Maze { - self.maze + fn get(&self) -> Maze { + self.maze.clone() } } @@ -51,7 +51,7 @@ pub mod maze_state { impl MazeState for Generated {} impl BuildableMazeState for Generated { - fn get(self) -> Maze { + fn get(&self) -> Maze { self.generator.generate() } } @@ -120,4 +120,34 @@ impl ExecutorBuilder { } } -pub struct DynExecutorBuilder {} +pub struct DynExecutorBuilder { + maze: Option>, + delay: Duration, +} + +impl DynExecutorBuilder { + pub(crate) fn new() -> Self { + Self { + maze: None, + delay: Duration::from_millis(100), + } + } + + pub fn maze(self, maze: Maze) -> Self { + todo!() + } + + pub fn generated(self, maze: Box) -> Self { + todo!() + } + + pub fn delay_ms(self, delay: u64) -> Self { + todo!() + } + + pub(crate) fn build(self) -> (Maze, Duration) { + let maze = self.maze.expect("no buildable maze provided").get(); + let delay = self.delay; + (maze, delay) + } +}