git_x/core/
traits.rs

1use crate::Result;
2
3/// Trait for commands that can be executed
4pub trait Command {
5    /// Execute the command and return the result
6    fn execute(&self) -> Result<String>;
7
8    /// Get the command name for help and error messages
9    fn name(&self) -> &'static str;
10
11    /// Get a brief description of what the command does
12    fn description(&self) -> &'static str;
13}
14
15/// Trait for commands that support dry-run mode
16pub trait DryRunnable: Command {
17    /// Execute the command in dry-run mode
18    fn execute_dry_run(&self) -> Result<String>;
19
20    /// Check if dry-run mode is enabled
21    fn is_dry_run(&self) -> bool;
22}
23
24/// Trait for commands that perform destructive operations
25pub trait Destructive: Command {
26    /// Get a description of what will be destroyed/changed
27    fn destruction_description(&self) -> String;
28
29    /// Confirm the destructive operation with the user
30    fn confirm_destruction(&self) -> Result<bool> {
31        crate::core::safety::Safety::confirm_destructive_operation(
32            self.name(),
33            &self.destruction_description(),
34        )
35    }
36
37    /// Create a backup before the destructive operation
38    fn create_backup(&self) -> Result<Option<String>> {
39        Ok(None) // Default: no backup
40    }
41}
42
43/// Trait for commands that work with git repositories
44pub trait GitCommand: Command {
45    /// Validate that we're in a git repository
46    fn validate_git_repo(&self) -> Result<()> {
47        crate::core::validation::Validate::in_git_repo()
48    }
49
50    /// Get the repository root path
51    fn repo_root(&self) -> Result<String> {
52        crate::core::git::GitOperations::repo_root()
53    }
54
55    /// Get the current branch
56    fn current_branch(&self) -> Result<String> {
57        crate::core::git::GitOperations::current_branch()
58    }
59}
60
61/// Trait for commands that support interactive mode
62pub trait Interactive: Command {
63    /// Check if the command should run in interactive mode
64    fn is_interactive(&self) -> bool {
65        crate::core::interactive::Interactive::is_interactive()
66    }
67
68    /// Run the command in non-interactive mode (for CI/testing)
69    fn execute_non_interactive(&self) -> Result<String>;
70}
71
72/// Trait for formatting output
73pub trait Formatter {
74    /// Format output for display
75    fn format(&self, content: &str) -> String;
76}
77
78/// Trait for validating inputs
79pub trait Validator<T: ?Sized> {
80    /// Validate the input
81    fn validate(&self, input: &T) -> Result<()>;
82
83    /// Get validation rules for display to users
84    fn validation_rules(&self) -> Vec<&'static str>;
85}
86
87/// Trait for git operations that can be optimized
88pub trait Optimizable {
89    /// Execute with optimization (batching, caching, etc.)
90    fn execute_optimized(&self) -> Result<String>;
91}
92
93/// Trait for commands that support different output formats
94pub trait MultiFormat: Command {
95    /// Available output formats
96    fn supported_formats(&self) -> Vec<&'static str>;
97
98    /// Execute with specific format
99    fn execute_with_format(&self, format: &str) -> Result<String>;
100}
101
102/// Trait for commands that can be configured
103pub trait Configurable: Command {
104    /// Configuration type for this command
105    type Config;
106
107    /// Apply configuration to the command
108    fn with_config(self, config: Self::Config) -> Self;
109}