1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum EditMode {
8 Surgical,
11 Reformat,
14}
15
16impl Default for EditMode {
17 fn default() -> Self {
18 EditMode::Surgical
19 }
20}
21
22impl std::fmt::Display for EditMode {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 match self {
25 EditMode::Surgical => write!(f, "surgical"),
26 EditMode::Reformat => write!(f, "reformat"),
27 }
28 }
29}
30
31impl std::str::FromStr for EditMode {
32 type Err = String;
33
34 fn from_str(s: &str) -> Result<Self, Self::Err> {
35 match s.to_lowercase().as_str() {
36 "surgical" => Ok(EditMode::Surgical),
37 "reformat" => Ok(EditMode::Reformat),
38 _ => Err(format!("Invalid edit mode: {}. Valid values are 'surgical' or 'reformat'", s)),
39 }
40 }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(tag = "type")]
45pub enum Operation {
46 AddStructField(AddStructFieldOp),
47 UpdateStructField(UpdateStructFieldOp),
48 RemoveStructField(RemoveStructFieldOp),
49 AddStructLiteralField(AddStructLiteralFieldOp),
50 AddEnumVariant(AddEnumVariantOp),
51 UpdateEnumVariant(UpdateEnumVariantOp),
52 RemoveEnumVariant(RemoveEnumVariantOp),
53 AddMatchArm(AddMatchArmOp),
54 UpdateMatchArm(UpdateMatchArmOp),
55 RemoveMatchArm(RemoveMatchArmOp),
56 AddImplMethod(AddImplMethodOp),
57 AddUseStatement(AddUseStatementOp),
58 AddDerive(AddDeriveOp),
59 Transform(TransformOp),
60 RenameEnumVariant(RenameEnumVariantOp),
61 RenameFunction(RenameFunctionOp),
62 AddDocComment(AddDocCommentOp),
63 UpdateDocComment(UpdateDocCommentOp),
64 RemoveDocComment(RemoveDocCommentOp),
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct AddStructFieldOp {
69 pub struct_name: String,
70 pub field_def: String, pub position: InsertPosition,
72 #[serde(default)]
73 pub literal_default: Option<String>, #[serde(default)]
75 pub where_filter: Option<String>, }
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct UpdateStructFieldOp {
80 pub struct_name: String,
81 pub field_def: String, #[serde(default)]
83 pub where_filter: Option<String>, }
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct RemoveStructFieldOp {
88 pub struct_name: String,
89 pub field_name: String, #[serde(default)]
91 pub where_filter: Option<String>, }
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct AddStructLiteralFieldOp {
96 pub struct_name: String,
97 pub field_def: String, pub position: InsertPosition,
99 #[serde(default)]
100 pub struct_path: Option<String>, }
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct AddEnumVariantOp {
105 pub enum_name: String,
106 pub variant_def: String, pub position: InsertPosition,
108 #[serde(default)]
109 pub where_filter: Option<String>, }
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct UpdateEnumVariantOp {
114 pub enum_name: String,
115 pub variant_def: String, #[serde(default)]
117 pub where_filter: Option<String>, }
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct RemoveEnumVariantOp {
122 pub enum_name: String,
123 pub variant_name: String, #[serde(default)]
125 pub where_filter: Option<String>, }
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct AddMatchArmOp {
130 pub pattern: String, pub body: String, pub function_name: Option<String>, #[serde(default)]
134 pub auto_detect: bool, pub enum_name: Option<String>, }
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct UpdateMatchArmOp {
140 pub pattern: String, pub new_body: String, pub function_name: Option<String>, }
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct RemoveMatchArmOp {
147 pub pattern: String, pub function_name: Option<String>, }
150
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct AddImplMethodOp {
153 pub target: String, pub method_def: String, pub position: InsertPosition,
156}
157
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct AddUseStatementOp {
160 pub use_path: String, pub position: InsertPosition,
162}
163
164#[derive(Debug, Clone, Serialize, Deserialize)]
165pub struct AddDeriveOp {
166 pub target_name: String, pub target_type: String, pub derives: Vec<String>, #[serde(default)]
170 pub where_filter: Option<String>, }
172
173#[derive(Debug, Clone, Serialize, Deserialize)]
174pub enum InsertPosition {
175 First,
176 Last,
177 After(String), Before(String), }
180
181#[derive(Debug, Serialize, Deserialize)]
182pub struct BatchSpec {
183 pub base_path: PathBuf,
184 pub operations: Vec<Operation>,
185}
186
187#[derive(Debug, Serialize, Deserialize, Clone)]
188pub struct NodeLocation {
189 pub line: usize,
190 pub column: usize,
191 pub end_line: usize,
192 pub end_column: usize,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct BackupNode {
198 pub node_type: String, pub identifier: String, pub original_content: String, pub location: NodeLocation,
202}
203
204#[derive(Debug)]
206pub struct ModificationResult {
207 pub changed: bool,
208 pub modified_nodes: Vec<BackupNode>,
209}
210
211#[derive(Debug, Serialize, Deserialize)]
213pub struct InspectResult {
214 pub file_path: String,
215 pub node_type: String, pub identifier: String, pub location: NodeLocation,
218 pub snippet: String, }
220
221#[derive(Debug, Clone, Serialize, Deserialize)]
223pub struct TransformOp {
224 pub node_type: String, pub name_filter: Option<String>, pub content_filter: Option<String>, pub action: TransformAction, }
229
230#[derive(Debug, Clone, Serialize, Deserialize)]
232#[serde(tag = "type")]
233pub enum TransformAction {
234 Comment, Remove, Replace { with: String }, }
238
239#[derive(Debug, Clone, Serialize, Deserialize)]
241pub struct RenameEnumVariantOp {
242 pub enum_name: String, pub old_variant: String, pub new_variant: String, #[serde(default)]
246 pub enum_path: Option<String>, #[serde(default)]
248 pub edit_mode: EditMode, }
250
251#[derive(Debug, Clone, Serialize, Deserialize)]
253pub struct RenameFunctionOp {
254 pub old_name: String, pub new_name: String, #[serde(default)]
257 pub function_path: Option<String>, #[serde(default)]
259 pub edit_mode: EditMode, }
261
262#[derive(Debug, Clone, Serialize, Deserialize)]
264pub struct AddDocCommentOp {
265 pub target_type: String, pub name: String, pub doc_comment: String, #[serde(default)]
269 pub style: DocCommentStyle, }
271
272#[derive(Debug, Clone, Serialize, Deserialize)]
274pub struct UpdateDocCommentOp {
275 pub target_type: String, pub name: String, pub doc_comment: String, }
279
280#[derive(Debug, Clone, Serialize, Deserialize)]
282pub struct RemoveDocCommentOp {
283 pub target_type: String, pub name: String, }
286
287#[derive(Debug, Clone, Serialize, Deserialize)]
289#[serde(rename_all = "lowercase")]
290pub enum DocCommentStyle {
291 Line, Block, }
294
295impl Default for DocCommentStyle {
296 fn default() -> Self {
297 DocCommentStyle::Line
298 }
299}
300
301impl std::str::FromStr for DocCommentStyle {
302 type Err = String;
303
304 fn from_str(s: &str) -> Result<Self, Self::Err> {
305 match s.to_lowercase().as_str() {
306 "line" => Ok(DocCommentStyle::Line),
307 "block" => Ok(DocCommentStyle::Block),
308 _ => Err(format!("Invalid doc comment style: {}. Valid values are 'line' or 'block'", s)),
309 }
310 }
311}