1use padlock_core::ir::{optimal_order, StructLayout};
6use similar::{ChangeTag, TextDiff};
7
8pub fn generate_c_fix(layout: &StructLayout) -> String {
13 let optimal = optimal_order(layout);
14 let mut out = format!("struct {} {{\n", layout.name);
15 for field in &optimal {
16 let ty = field_type_name(field);
17 out.push_str(&format!(" {ty} {};\n", field.name));
18 }
19 out.push_str("};\n");
20 out
21}
22
23pub fn generate_rust_fix(layout: &StructLayout) -> String {
25 let optimal = optimal_order(layout);
26 let mut out = format!("struct {} {{\n", layout.name);
27 for field in &optimal {
28 let ty = field_type_name(field);
29 out.push_str(&format!(" {}: {ty},\n", field.name));
30 }
31 out.push_str("}\n");
32 out
33}
34
35pub fn generate_go_fix(layout: &StructLayout) -> String {
37 let optimal = optimal_order(layout);
38 let mut out = format!("type {} struct {{\n", layout.name);
39 for field in &optimal {
40 let ty = field_type_name(field);
41 out.push_str(&format!("\t{}\t{ty}\n", field.name));
42 }
43 out.push_str("}\n");
44 out
45}
46
47pub fn unified_diff(original: &str, fixed: &str, context_lines: usize) -> String {
49 if original == fixed {
50 return String::from("(no changes)\n");
51 }
52 let diff = TextDiff::from_lines(original, fixed);
53 let mut out = String::new();
54 for (idx, group) in diff.grouped_ops(context_lines).iter().enumerate() {
55 if idx > 0 {
56 out.push_str("...\n");
57 }
58 for op in group {
59 for change in diff.iter_changes(op) {
60 let prefix = match change.tag() {
61 ChangeTag::Delete => "-",
62 ChangeTag::Insert => "+",
63 ChangeTag::Equal => " ",
64 };
65 out.push_str(&format!("{prefix} {}", change.value()));
66 if !change.value().ends_with('\n') {
67 out.push('\n');
68 }
69 }
70 }
71 }
72 out
73}
74
75fn match_braces(s: &str) -> Option<usize> {
80 let mut depth = 0usize;
81 for (i, c) in s.char_indices() {
82 match c {
83 '{' => depth += 1,
84 '}' => {
85 depth -= 1;
86 if depth == 0 {
87 return Some(i + 1);
88 }
89 }
90 _ => {}
91 }
92 }
93 None
94}
95
96fn consume_semicolon(source: &str, pos: usize) -> usize {
98 let rest = &source[pos..];
99 let ws = rest.len()
100 - rest
101 .trim_start_matches(|c: char| c.is_whitespace() && c != '\n')
102 .len();
103 let after_ws = &rest[ws..];
104 if after_ws.starts_with(';') {
105 pos + ws + 1
106 } else {
107 pos
108 }
109}
110
111pub fn find_c_struct_span(source: &str, struct_name: &str) -> Option<std::ops::Range<usize>> {
114 for kw in &["struct", "union"] {
115 let needle = format!("{kw} {struct_name}");
116 let mut search_from = 0usize;
117 while let Some(rel) = source[search_from..].find(&needle) {
118 let start = search_from + rel;
119 let after_name = start + needle.len();
120 let boundary = source[after_name..].chars().next();
122 if matches!(
123 boundary,
124 Some('{') | Some('\n') | Some('\r') | Some(' ') | Some('\t') | None
125 ) {
126 if let Some(brace_rel) = source[after_name..].find('{') {
128 let brace_start = after_name + brace_rel;
129 if source[after_name..brace_start]
131 .chars()
132 .all(|c| c.is_whitespace())
133 {
134 if let Some(body_len) = match_braces(&source[brace_start..]) {
135 let end = consume_semicolon(source, brace_start + body_len);
136 return Some(start..end);
137 }
138 }
139 }
140 }
141 search_from = start + 1;
142 }
143 }
144 None
145}
146
147pub fn find_rust_struct_span(source: &str, struct_name: &str) -> Option<std::ops::Range<usize>> {
149 let needle = format!("struct {struct_name}");
150 let mut search_from = 0usize;
151 while let Some(rel) = source[search_from..].find(&needle) {
152 let start = search_from + rel;
153 let after_name = start + needle.len();
154 let boundary = source[after_name..].chars().next();
155 if matches!(
156 boundary,
157 Some('{') | Some('\n') | Some('\r') | Some(' ') | Some('\t') | None
158 ) {
159 if let Some(brace_rel) = source[after_name..].find('{') {
160 let brace_start = after_name + brace_rel;
161 if source[after_name..brace_start]
162 .chars()
163 .all(|c| c.is_whitespace())
164 {
165 if let Some(body_len) = match_braces(&source[brace_start..]) {
166 return Some(start..brace_start + body_len);
168 }
169 }
170 }
171 }
172 search_from = start + 1;
173 }
174 None
175}
176
177pub fn find_go_struct_span(source: &str, struct_name: &str) -> Option<std::ops::Range<usize>> {
179 let needle = format!("type {struct_name} struct");
180 let mut search_from = 0usize;
181 while let Some(rel) = source[search_from..].find(&needle) {
182 let start = search_from + rel;
183 let after_kw = start + needle.len();
184 if let Some(brace_rel) = source[after_kw..].find('{') {
185 let brace_start = after_kw + brace_rel;
186 if source[after_kw..brace_start]
187 .chars()
188 .all(|c| c.is_whitespace())
189 {
190 if let Some(body_len) = match_braces(&source[brace_start..]) {
191 return Some(start..brace_start + body_len);
192 }
193 }
194 }
195 search_from = start + 1;
196 }
197 None
198}
199
200pub fn apply_fixes_c(source: &str, layouts: &[&StructLayout]) -> String {
207 apply_fixes(source, layouts, find_c_struct_span, generate_c_fix)
208}
209
210pub fn apply_fixes_rust(source: &str, layouts: &[&StructLayout]) -> String {
212 apply_fixes(source, layouts, find_rust_struct_span, generate_rust_fix)
213}
214
215pub fn apply_fixes_go(source: &str, layouts: &[&StructLayout]) -> String {
217 apply_fixes(source, layouts, find_go_struct_span, generate_go_fix)
218}
219
220fn apply_fixes(
221 source: &str,
222 layouts: &[&StructLayout],
223 find_span: fn(&str, &str) -> Option<std::ops::Range<usize>>,
224 generate: fn(&StructLayout) -> String,
225) -> String {
226 let mut replacements: Vec<(usize, usize, String)> = layouts
228 .iter()
229 .filter_map(|layout| {
230 let span = find_span(source, &layout.name)?;
231 let fixed = generate(layout);
232 Some((span.start, span.end, fixed))
233 })
234 .collect();
235
236 replacements.sort_by_key(|(start, _, _)| *start);
238
239 let mut result = source.to_string();
240 for (start, end, fixed) in replacements.into_iter().rev() {
241 result.replace_range(start..end, &fixed);
242 }
243 result
244}
245
246fn field_type_name(field: &padlock_core::ir::Field) -> &str {
247 match &field.ty {
248 padlock_core::ir::TypeInfo::Primitive { name, .. }
249 | padlock_core::ir::TypeInfo::Opaque { name, .. } => name.as_str(),
250 padlock_core::ir::TypeInfo::Pointer { .. } => "void*",
251 padlock_core::ir::TypeInfo::Array { .. } => "/* array */",
252 padlock_core::ir::TypeInfo::Struct(l) => l.name.as_str(),
253 }
254}
255
256#[cfg(test)]
259mod tests {
260 use super::*;
261 use padlock_core::ir::test_fixtures::connection_layout;
262
263 #[test]
264 fn c_fix_starts_with_struct() {
265 let out = generate_c_fix(&connection_layout());
266 assert!(out.starts_with("struct Connection {"));
267 }
268
269 #[test]
270 fn c_fix_contains_all_fields() {
271 let out = generate_c_fix(&connection_layout());
272 assert!(out.contains("timeout"));
273 assert!(out.contains("port"));
274 assert!(out.contains("is_active"));
275 assert!(out.contains("is_tls"));
276 }
277
278 #[test]
279 fn c_fix_puts_largest_align_first() {
280 let out = generate_c_fix(&connection_layout());
281 let timeout_pos = out.find("timeout").unwrap();
282 let is_active_pos = out.find("is_active").unwrap();
283 assert!(timeout_pos < is_active_pos);
284 }
285
286 #[test]
287 fn rust_fix_uses_colon_syntax() {
288 let out = generate_rust_fix(&connection_layout());
289 assert!(out.contains(": f64"));
290 }
291
292 #[test]
293 fn unified_diff_marks_changes() {
294 let orig = "struct T { char a; double b; };\n";
295 let fixed = "struct T { double b; char a; };\n";
296 let diff = unified_diff(orig, fixed, 1);
297 assert!(diff.contains('-') || diff.contains('+'));
298 }
299
300 #[test]
301 fn unified_diff_identical_is_no_changes() {
302 assert_eq!(unified_diff("x\n", "x\n", 3), "(no changes)\n");
303 }
304
305 #[test]
308 fn find_c_struct_span_basic() {
309 let src = "struct Foo { int x; char y; };\nstruct Bar { double z; };\n";
310 let span = find_c_struct_span(src, "Foo").unwrap();
311 let text = &src[span];
312 assert!(text.starts_with("struct Foo"));
313 assert!(!text.contains("Bar"));
314 }
315
316 #[test]
317 fn find_c_struct_span_missing_returns_none() {
318 let src = "struct Other { int x; };";
319 assert!(find_c_struct_span(src, "Missing").is_none());
320 }
321
322 #[test]
323 fn find_rust_struct_span_basic() {
324 let src = "struct Foo {\n x: u32,\n y: u8,\n}\n";
325 let span = find_rust_struct_span(src, "Foo").unwrap();
326 assert!(src[span].starts_with("struct Foo"));
327 }
328
329 #[test]
330 fn find_go_struct_span_basic() {
331 let src = "type Foo struct {\n\tX int32\n\tY bool\n}\n";
332 let span = find_go_struct_span(src, "Foo").unwrap();
333 assert!(src[span].starts_with("type Foo struct"));
334 }
335
336 #[test]
339 fn apply_fixes_c_reorders_in_place() {
340 let src = "struct Connection { bool is_active; double timeout; bool is_tls; int port; };\n";
342 let layout = connection_layout();
343 let fixed = apply_fixes_c(src, &[&layout]);
344 let timeout_pos = fixed.find("timeout").unwrap();
345 let is_active_pos = fixed.find("is_active").unwrap();
346 assert!(
347 timeout_pos < is_active_pos,
348 "double should appear before bool after reorder"
349 );
350 }
351
352 #[test]
353 fn apply_fixes_rust_reorders_in_place() {
354 let src = "struct Connection {\n is_active: bool,\n timeout: f64,\n is_tls: bool,\n port: i32,\n}\n";
355 let layout = connection_layout();
356 let fixed = apply_fixes_rust(src, &[&layout]);
357 let timeout_pos = fixed.find("timeout").unwrap();
358 let is_active_pos = fixed.find("is_active").unwrap();
359 assert!(timeout_pos < is_active_pos);
360 }
361
362 #[test]
363 fn go_fix_uses_tab_syntax() {
364 let layout = connection_layout();
365 let out = generate_go_fix(&layout);
366 assert!(out.starts_with("type Connection struct"));
367 assert!(out.contains('\t'));
368 }
369}