1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
pub fn format_parse_error(
error_msg: &str,
line: usize,
column: usize,
source: &str,
) -> ErrorMessage {
let lines: Vec<&str> = source.lines().collect();
let source_line = if line > 0 && line <= lines.len() {
lines
.get(line - 1)
.map(|s| s.to_string())
.unwrap_or_default()
} else {
String::new()
};
let context = if !source_line.is_empty() {
Some(SourceContext {
line,
column,
source_line: source_line.clone(),
length: 1,
})
} else {
None
};
// Try to suggest a fix based on common parse errors
let suggestion = if error_msg.contains("then") {
Some(Suggestion {
description: "Did you forget 'then'?".to_string(),
fixed_code: Some(format!("{}; then", source_line)),
auto_fixable: false,
})
} else {
None
};
ErrorMessage {
error_type: ErrorType::Parse,
code: Some("P001".to_string()),
severity: Severity::Error,
message: error_msg.to_string(),
context,
explanation: None,
suggestion,
help_topics: vec![],
}
}
/// Creates an error message for lint violations (code quality issues).
///
/// Converts a linter diagnostic into a user-friendly error message with
/// explanations and auto-fix suggestions.
///
/// # Arguments
///
/// * `diagnostic` - The linter diagnostic
/// * `source` - The source code being linted
///
/// # Returns
///
/// Structured `ErrorMessage` with lint code (DET*, IDEM*, SEC*)
///
/// # Examples
///
/// ```
/// use bashrs::repl::errors::format_lint_error;
/// use bashrs::linter::{Diagnostic, Severity, Span, Fix};
///
/// let diagnostic = Diagnostic {
/// code: "IDEM001".to_string(),
/// severity: Severity::Error,
/// message: "mkdir without -p".to_string(),
/// span: Span::new(1, 1, 1, 11),
/// fix: Some(Fix::new("mkdir -p /tmp")),
/// };
///
/// let error = format_lint_error(&diagnostic, "mkdir /tmp");
///
/// assert_eq!(error.code, Some("IDEM001".to_string()));
/// assert!(error.suggestion.is_some());
/// ```
pub fn format_lint_error(diagnostic: &Diagnostic, source: &str) -> ErrorMessage {
let lines: Vec<&str> = source.lines().collect();
let line_idx = diagnostic.span.start_line.saturating_sub(1);
let source_line = if line_idx < lines.len() {
lines
.get(line_idx)
.map(|s| s.to_string())
.unwrap_or_default()
} else {
String::new()
};
let context = if !source_line.is_empty() {
Some(SourceContext {
line: diagnostic.span.start_line,
column: diagnostic.span.start_col,
source_line,
length: diagnostic
.span
.end_col
.saturating_sub(diagnostic.span.start_col)
.max(1),
})
} else {
None
};
// Convert LintSeverity to our Severity
let severity = match diagnostic.severity {
LintSeverity::Error => Severity::Error,
LintSeverity::Warning => Severity::Warning,
LintSeverity::Info => Severity::Info,
LintSeverity::Note => Severity::Info,
LintSeverity::Perf => Severity::Warning,
LintSeverity::Risk => Severity::Warning,
};
// Determine explanation based on violation code
let explanation = match diagnostic.code.as_str() {
code if code.starts_with("IDEM") => {
Some("Command will fail if run multiple times".to_string())
}
code if code.starts_with("DET") => {
Some("Command produces different output on each run".to_string())
}
code if code.starts_with("SEC") => Some("Security vulnerability detected".to_string()),
_ => None,
};
// Create suggestion from fix if available
let suggestion = diagnostic.fix.as_ref().map(|fix| Suggestion {
description: "Use idempotent version".to_string(),
fixed_code: Some(fix.replacement.clone()),
auto_fixable: true, // bashrs can auto-fix lint issues via purify
});
ErrorMessage {
error_type: ErrorType::Lint,
code: Some(diagnostic.code.clone()),
severity,
message: diagnostic.message.clone(),
context,
explanation,
suggestion,
help_topics: vec!["purify".to_string()],
}
}
/// Creates an error message for unknown REPL commands.
///
/// Uses edit distance to suggest similar commands and lists all available commands.
///
/// # Arguments
///
/// * `command` - The unknown command entered by the user
/// * `available_commands` - List of valid command names
///
/// # Returns
///
/// Structured `ErrorMessage` with command suggestion if similar command found
///
/// # Examples
///
/// ```
/// use bashrs::repl::errors::format_command_error;
///
/// let error = format_command_error(
/// "purfy",
/// &["purify", "lint", "quit", "help"]
/// );
///
/// assert!(error.message.contains("purfy"));
/// assert!(error.suggestion.is_some());
/// assert!(error.explanation.is_some());
/// ```
pub fn format_command_error(command: &str, available_commands: &[&str]) -> ErrorMessage {
let message = format!("Unknown command: '{}'", command);
// Try to suggest similar command
let suggestion = suggest_command(command, available_commands).map(|suggested| Suggestion {
description: format!("Did you mean: '{}'?", suggested),
fixed_code: Some(suggested),
auto_fixable: false,
});
// Build list of available commands for explanation
let commands_list = available_commands.join(", ");
let explanation = Some(format!("Available commands: {}", commands_list));
ErrorMessage {
error_type: ErrorType::Command,
code: None,
severity: Severity::Error,
message,
context: None,
explanation,
suggestion,
help_topics: vec!["commands".to_string()],
}
}
/// Suggest similar command using edit distance
pub fn suggest_command(input: &str, commands: &[&str]) -> Option<String> {
if commands.is_empty() {
return None;
}
// Calculate Levenshtein distance to each command
let mut best_match: Option<(String, usize)> = None;
for &command in commands {
let distance = levenshtein_distance(input, command);
// Only suggest if distance is small (< 3)
if distance < 3 {
if let Some((_, best_distance)) = &best_match {
if distance < *best_distance {
best_match = Some((command.to_string(), distance));
}
} else {
best_match = Some((command.to_string(), distance));
}
}
}
best_match.map(|(cmd, _)| cmd)
}
/// Calculate Levenshtein distance between two strings
///
/// This is a standard dynamic programming algorithm with guaranteed safe indexing
/// because the matrix is pre-allocated to (len1+1) x (len2+1) dimensions.
#[allow(clippy::indexing_slicing, clippy::needless_range_loop)]
fn levenshtein_distance(s1: &str, s2: &str) -> usize {
let len1 = s1.len();
let len2 = s2.len();
if len1 == 0 {
return len2;
}
if len2 == 0 {
return len1;
}
let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];
// Initialize first column and row
for i in 0..=len1 {
matrix[i][0] = i;
}
for j in 0..=len2 {
matrix[0][j] = j;
}
// Calculate distances
let s1_chars: Vec<char> = s1.chars().collect();
let s2_chars: Vec<char> = s2.chars().collect();
for i in 1..=len1 {
for j in 1..=len2 {
let cost = usize::from(s1_chars[i - 1] != s2_chars[j - 1]);
matrix[i][j] = (matrix[i - 1][j] + 1) // deletion
.min(matrix[i][j - 1] + 1) // insertion
.min(matrix[i - 1][j - 1] + cost); // substitution
}
}
matrix[len1][len2]
}
/// Format source context with caret indicator
pub fn format_source_context(context: &SourceContext) -> String {
let mut output = String::new();
// Show line number and source
output.push_str(&format!(" {} | {}\n", context.line, context.source_line));
// Show caret indicator pointing to the error
let col = context.column.saturating_sub(1);
let line_num_width = format!(" {} | ", context.line).len();
output.push_str(&" ".repeat(line_num_width));
output.push_str(&" ".repeat(col));
output.push_str(&"^".repeat(context.length));
output
}
#[cfg(test)]
#[path = "errors_tests_repl_015.rs"]
mod tests_extracted;