konnektoren_core/commands/
command.rs

1//! This module defines the core command structure and traits for the game.
2
3use super::CommandType;
4use super::challenge_command::ChallengeCommand;
5use super::error::Result;
6use super::game_command::GameCommand;
7use crate::game::GameState;
8
9/// A trait that defines the basic behavior for all commands in the game.
10pub trait CommandTrait {
11    /// Executes the command on the given game state.
12    ///
13    /// # Arguments
14    ///
15    /// * `state` - A mutable reference to the current game state.
16    ///
17    /// # Returns
18    ///
19    /// A `Result` indicating success or containing an error if the command execution failed.
20    fn execute(&self, state: &mut GameState) -> Result<()>;
21
22    /// Gets the type of the command.
23    fn get_type(&self) -> CommandType;
24}
25
26/// An enum representing all possible commands in the game.
27///
28/// This enum serves as a unified interface for both game-level and challenge-level commands.
29#[allow(clippy::large_enum_variant)]
30#[derive(Debug, Clone, PartialEq)]
31pub enum Command {
32    /// Represents a game-level command.
33    Game(GameCommand),
34    /// Represents a challenge-level command.
35    Challenge(ChallengeCommand),
36}
37
38impl CommandTrait for Command {
39    /// Executes the command on the given game state.
40    ///
41    /// This implementation delegates the execution to either the game command
42    /// or the challenge command based on the variant.
43    ///
44    /// # Arguments
45    ///
46    /// * `state` - A mutable reference to the current game state.
47    ///
48    /// # Returns
49    ///
50    /// A `Result` indicating success or containing an error if the command execution failed.
51    fn execute(&self, state: &mut GameState) -> Result<()> {
52        match self {
53            Command::Game(cmd) => cmd.execute(state),
54            Command::Challenge(cmd) => cmd.execute(state),
55        }
56    }
57
58    /// Gets the type of the command.
59    fn get_type(&self) -> CommandType {
60        match self {
61            Command::Game(_) => CommandType::Game,
62            Command::Challenge(_) => CommandType::Challenge,
63        }
64    }
65}