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
296
297
298
299
300
301
302
303
304
305
306
// REPL State Module
//
// Task: REPL-003-002 - ReplState struct
// Test Approach: RED → GREEN → REFACTOR → PROPERTY → MUTATION
//
// Quality targets:
// - Unit tests: 10+ scenarios
// - Property tests: 3+ generators
// - Mutation score: ≥90%
// - Complexity: <10 per function
use crate::repl::ReplMode;
use std::collections::HashMap;
use std::path::PathBuf;
/// Mutable state for a REPL session.
///
/// `ReplState` manages all stateful aspects of an interactive REPL session:
/// - **Command history**: Navigable with up/down arrows
/// - **Session variables**: Persist values across commands
/// - **Exit management**: Clean shutdown signaling
/// - **Error tracking**: Statistics for debugging
/// - **Mode switching**: Different behaviors (Normal, Purify, Lint, Debug, Explain)
/// - **Script loading**: Track loaded files for `:reload` command
/// - **Function tracking**: Extract and manage functions from scripts
///
/// Inspired by Ruchy REPL state management patterns.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
///
/// // Add commands to history
/// state.add_history("x=5".to_string());
/// state.add_history("echo $x".to_string());
/// assert_eq!(state.history_len(), 2);
///
/// // Set and retrieve variables
/// state.set_variable("user".to_string(), "alice".to_string());
/// assert_eq!(state.get_variable("user"), Some(&"alice".to_string()));
/// ```
///
/// ## Mode switching
///
/// ```
/// use bashrs::repl::{ReplState, ReplMode};
///
/// let mut state = ReplState::new();
/// assert_eq!(state.mode(), ReplMode::Normal);
///
/// state.set_mode(ReplMode::Purify);
/// assert_eq!(state.mode(), ReplMode::Purify);
/// ```
///
/// ## Error tracking
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// assert_eq!(state.error_count(), 0);
///
/// state.record_error();
/// state.record_error();
/// assert_eq!(state.error_count(), 2);
///
/// state.reset_error_count();
/// assert_eq!(state.error_count(), 0);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ReplState {
/// Command history (for up/down arrow navigation).
history: Vec<String>,
/// Session variables (persist across commands).
variables: HashMap<String, String>,
/// Exit requested flag (for clean shutdown).
exit_requested: bool,
/// Error count (for debugging and statistics).
error_count: usize,
/// Current REPL mode.
mode: ReplMode,
/// Last loaded script path (for `:reload` command).
last_loaded_script: Option<PathBuf>,
/// Functions extracted from loaded scripts.
loaded_functions: Vec<String>,
}
impl ReplState {
/// Creates a new `ReplState` with default values.
///
/// Initializes an empty state with:
/// - No command history
/// - No session variables
/// - Normal mode
/// - Zero error count
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let state = ReplState::new();
/// assert_eq!(state.history_len(), 0);
/// assert_eq!(state.variable_count(), 0);
/// ```
pub fn new() -> Self {
Self::default()
}
// === Command History Management ===
/// Adds a command to the history.
///
/// Commands are stored in chronological order for navigation with up/down arrows.
///
/// # Arguments
///
/// * `command` - The command string to add to history
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.add_history("x=5".to_string());
/// state.add_history("echo $x".to_string());
///
/// assert_eq!(state.history_len(), 2);
/// assert_eq!(state.history()[0], "x=5");
/// ```
pub fn add_history(&mut self, command: String) {
self.history.push(command);
}
/// Returns a slice of all history entries.
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.add_history("cmd1".to_string());
/// state.add_history("cmd2".to_string());
///
/// let history = state.history();
/// assert_eq!(history.len(), 2);
/// assert_eq!(history[1], "cmd2");
/// ```
pub fn history(&self) -> &[String] {
&self.history
}
/// Gets a specific history entry by index.
///
/// # Arguments
///
/// * `index` - Zero-based index into history
///
/// # Returns
///
/// * `Some(&String)` if index is valid
/// * `None` if index is out of bounds
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.add_history("first".to_string());
///
/// assert_eq!(state.get_history(0), Some(&"first".to_string()));
/// assert_eq!(state.get_history(1), None);
/// ```
pub fn get_history(&self, index: usize) -> Option<&String> {
self.history.get(index)
}
/// Clears all command history.
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.add_history("cmd".to_string());
/// assert_eq!(state.history_len(), 1);
///
/// state.clear_history();
/// assert_eq!(state.history_len(), 0);
/// ```
pub fn clear_history(&mut self) {
self.history.clear();
}
/// Returns the number of history entries.
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// assert_eq!(state.history_len(), 0);
///
/// state.add_history("cmd".to_string());
/// assert_eq!(state.history_len(), 1);
/// ```
pub fn history_len(&self) -> usize {
self.history.len()
}
// === Session Variable Management ===
/// Sets a session variable.
///
/// Variables persist across commands within a REPL session.
///
/// # Arguments
///
/// * `name` - Variable name
/// * `value` - Variable value
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.set_variable("USER".to_string(), "alice".to_string());
///
/// assert_eq!(state.get_variable("USER"), Some(&"alice".to_string()));
/// ```
pub fn set_variable(&mut self, name: String, value: String) {
self.variables.insert(name, value);
}
/// Gets a session variable by name.
///
/// # Arguments
///
/// * `name` - Variable name to lookup
///
/// # Returns
///
/// * `Some(&String)` if variable exists
/// * `None` if variable not found
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.set_variable("PATH".to_string(), "/usr/bin".to_string());
///
/// assert_eq!(state.get_variable("PATH"), Some(&"/usr/bin".to_string()));
/// assert_eq!(state.get_variable("MISSING"), None);
/// ```
pub fn get_variable(&self, name: &str) -> Option<&String> {
self.variables.get(name)
}
/// Removes a session variable.
///
/// # Arguments
///
/// * `name` - Variable name to remove
///
/// # Returns
///
/// * `Some(String)` with the removed value
/// * `None` if variable didn't exist
///
/// # Examples
///
/// ```
/// use bashrs::repl::ReplState;
///
/// let mut state = ReplState::new();
/// state.set_variable("TEMP".to_string(), "value".to_string());
///
/// let removed = state.remove_variable("TEMP");
/// assert_eq!(removed, Some("value".to_string()));
/// assert_eq!(state.get_variable("TEMP"), None);
/// ```
pub fn remove_variable(&mut self, name: &str) -> Option<String> {
self.variables.remove(name)
}
}
include!("state_methods.rs");