1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::path::PathBuf;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Refactoring {
10 pub id: String,
12 pub refactoring_type: RefactoringType,
14 pub target: RefactoringTarget,
16 pub options: RefactoringOptions,
18}
19
20#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
22pub enum RefactoringType {
23 Rename,
25 Extract,
27 Inline,
29 Move,
31 ChangeSignature,
33 RemoveUnused,
35 Simplify,
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct RefactoringTarget {
42 pub file: PathBuf,
44 pub symbol: String,
46 pub range: Option<String>,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct RefactoringOptions {
53 pub dry_run: bool,
55 pub auto_rollback_on_failure: bool,
57 pub run_tests_after: bool,
59 pub extra: HashMap<String, String>,
61}
62
63impl Default for RefactoringOptions {
64 fn default() -> Self {
65 Self {
66 dry_run: false,
67 auto_rollback_on_failure: true,
68 run_tests_after: false,
69 extra: HashMap::new(),
70 }
71 }
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct RefactoringResult {
77 pub changes: Vec<FileChange>,
79 pub impact: Option<String>,
81 pub validation: Option<String>,
83 pub success: bool,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct FileChange {
90 pub file: PathBuf,
92 pub original: String,
94 pub new: String,
96 pub change_type: ChangeType,
98}
99
100#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
102pub enum ChangeType {
103 Modified,
105 Created,
107 Deleted,
109 Renamed,
111}
112
113impl std::fmt::Display for RefactoringType {
114 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115 match self {
116 RefactoringType::Rename => write!(f, "rename"),
117 RefactoringType::Extract => write!(f, "extract"),
118 RefactoringType::Inline => write!(f, "inline"),
119 RefactoringType::Move => write!(f, "move"),
120 RefactoringType::ChangeSignature => write!(f, "change_signature"),
121 RefactoringType::RemoveUnused => write!(f, "remove_unused"),
122 RefactoringType::Simplify => write!(f, "simplify"),
123 }
124 }
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct ValidationResult {
130 pub passed: bool,
132 pub errors: Vec<String>,
134 pub warnings: Vec<String>,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct RefactoringConfig {
141 pub language: String,
143 pub extensions: Vec<String>,
145 pub rules: Vec<RefactoringRule>,
147 pub transformations: Vec<RefactoringTransformation>,
149 pub provider: Option<String>,
151}
152
153impl RefactoringConfig {
154 pub fn validate(&self) -> crate::error::Result<()> {
156 if self.language.is_empty() {
157 return Err(crate::error::RefactoringError::ConfigError(
158 "Language name cannot be empty".to_string(),
159 ));
160 }
161 if self.extensions.is_empty() {
162 return Err(crate::error::RefactoringError::ConfigError(
163 "At least one file extension must be specified".to_string(),
164 ));
165 }
166 Ok(())
167 }
168
169 pub fn generic_fallback(language: &str) -> Self {
171 Self {
172 language: language.to_string(),
173 extensions: vec![],
174 rules: vec![],
175 transformations: vec![],
176 provider: None,
177 }
178 }
179}
180
181#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct RefactoringRule {
184 pub name: String,
186 pub pattern: String,
188 pub refactoring_type: RefactoringType,
190 pub enabled: bool,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize)]
196pub struct RefactoringTransformation {
197 pub name: String,
199 pub from_pattern: String,
201 pub to_pattern: String,
203 pub description: String,
205}
206
207#[derive(Debug, Clone, Serialize, Deserialize)]
209pub struct ImpactAnalysis {
210 pub affected_files: Vec<PathBuf>,
212 pub affected_symbols: Vec<String>,
214 pub risk_level: RiskLevel,
216 pub estimated_effort: u8,
218}
219
220#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
222pub enum RiskLevel {
223 Low,
225 Medium,
227 High,
229}
230
231#[derive(Debug, Clone, Serialize, Deserialize)]
233pub struct RefactoringPreview {
234 pub changes: Vec<FileChange>,
236 pub impact: ImpactAnalysis,
238 pub estimated_time_seconds: u32,
240}
241
242#[derive(Debug, Clone, Serialize, Deserialize)]
244pub struct BackupInfo {
245 pub id: String,
247 pub timestamp: String,
249 pub files: HashMap<PathBuf, String>,
251}
252
253impl std::str::FromStr for RefactoringType {
254 type Err = String;
255
256 fn from_str(s: &str) -> Result<Self, Self::Err> {
257 match s {
258 "rename" => Ok(RefactoringType::Rename),
259 "extract" => Ok(RefactoringType::Extract),
260 "inline" => Ok(RefactoringType::Inline),
261 "move" => Ok(RefactoringType::Move),
262 "change_signature" => Ok(RefactoringType::ChangeSignature),
263 "remove_unused" => Ok(RefactoringType::RemoveUnused),
264 "simplify" => Ok(RefactoringType::Simplify),
265 _ => Err(format!("Unknown refactoring type: {}", s)),
266 }
267 }
268}