Skip to main content

cargo_coupling/balance/
action.rs

1/// Specific refactoring actions
2#[derive(Debug, Clone)]
3pub enum RefactoringAction {
4    /// Introduce a trait to abstract the coupling
5    IntroduceTrait {
6        suggested_name: String,
7        methods: Vec<String>,
8    },
9    /// Move the component closer (same module/crate)
10    MoveCloser { target_location: String },
11    /// Extract an interface/adapter
12    ExtractAdapter {
13        adapter_name: String,
14        purpose: String,
15    },
16    /// Split a large module
17    SplitModule { suggested_modules: Vec<String> },
18    /// Remove unnecessary abstraction
19    SimplifyAbstraction { direct_usage: String },
20    /// Break circular dependency
21    BreakCycle { suggested_direction: String },
22    /// Add stable interface
23    StabilizeInterface { interface_name: String },
24    /// General refactoring suggestion
25    General { action: String },
26    /// Add getter methods to replace direct field access
27    AddGetters { fields: Vec<String> },
28    /// Introduce newtype pattern for type safety
29    IntroduceNewtype {
30        suggested_name: String,
31        wrapped_type: String,
32    },
33}
34
35impl std::fmt::Display for RefactoringAction {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            RefactoringAction::IntroduceTrait {
39                suggested_name,
40                methods,
41            } => {
42                write!(
43                    f,
44                    "Introduce trait `{}` with methods: {}",
45                    suggested_name,
46                    methods.join(", ")
47                )
48            }
49            RefactoringAction::MoveCloser { target_location } => {
50                write!(f, "Move component to `{}`", target_location)
51            }
52            RefactoringAction::ExtractAdapter {
53                adapter_name,
54                purpose,
55            } => {
56                write!(f, "Extract adapter `{}` to {}", adapter_name, purpose)
57            }
58            RefactoringAction::SplitModule { suggested_modules } => {
59                write!(f, "Split into modules: {}", suggested_modules.join(", "))
60            }
61            RefactoringAction::SimplifyAbstraction { direct_usage } => {
62                write!(f, "Replace with direct usage: {}", direct_usage)
63            }
64            RefactoringAction::BreakCycle {
65                suggested_direction,
66            } => {
67                write!(f, "Break cycle by {}", suggested_direction)
68            }
69            RefactoringAction::StabilizeInterface { interface_name } => {
70                write!(f, "Add stable interface `{}`", interface_name)
71            }
72            RefactoringAction::General { action } => {
73                write!(f, "{}", action)
74            }
75            RefactoringAction::AddGetters { fields } => {
76                write!(f, "Add getter methods for: {}", fields.join(", "))
77            }
78            RefactoringAction::IntroduceNewtype {
79                suggested_name,
80                wrapped_type,
81            } => {
82                write!(
83                    f,
84                    "Introduce newtype: `struct {}({});`",
85                    suggested_name, wrapped_type
86                )
87            }
88        }
89    }
90}