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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![warn(clippy::all)]
use crate::utils::{self, parse, FnPtr};
use std::{collections::HashMap, io, io::Write};
/// Main Repl Struct that contains all logic for the crate
#[derive(Debug, Clone)]
pub struct Repl<'a, T> {
/// struct to store data, used as an argument in all functions
pub data: T,
/// prompt that is displayed before asking for input
prompt: &'a str,
/// command used to exit. defaults to "exit"
exit: &'a str,
/// text displayed when repl is closed: defaults to "repl terminated"
exit_message: &'a str,
/// text displayed when repl gets an empty line as argument
empty_argument_message: &'a str,
/// text displayed when repl gets an unknown command as argument
unknown_command_message: &'a str,
/// hashmap that stores functions
functions: HashMap<&'a str, FnPtr<T>>,
/// parser function for input
parser_fn: fn(String) -> Vec<String>,
}
impl<T: Default> Default for Repl<'_, T> {
/// builds a new Repl from the given data
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default();
/// ```
#[inline]
fn default() -> Self {
Repl::new(Default::default())
}
}
impl<'a, T> Repl<'a, T> {
/// builds a new Repl from the given data
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::new(());
/// ```
#[inline]
pub fn new(data: T) -> Self {
Self {
data,
prompt: ">>>",
exit: "exit",
exit_message: "repl terminated",
functions: HashMap::new(),
empty_argument_message: "",
unknown_command_message: "",
parser_fn: parse,
}
}
/// runs the repl
/// functions which have command `""` will be called if none of the other commands are not called.
///
/// # Examples
///
/// ```rust, no_run
/// use repl_framework::Repl;
/// Repl::default().with_function("", |_: &mut (), b| println!("{:?}", b)).run();
/// ```
///
/// # Errors
/// - reading from stdin fails
/// - flushing stdout fails
pub fn run(&mut self) -> io::Result<()> {
loop {
let arguments = self.get_input()?;
if arguments.is_empty() {
println!("{}", self.empty_argument_message);
} else if arguments.concat() == self.exit {
println!("{}", self.exit_message);
break;
} else if self.functions.contains_key(arguments[0].as_str()) {
self.functions[arguments[0].as_str()](
&mut self.data,
arguments[1..arguments.len()].to_vec(),
);
} else if self.functions.contains_key("") {
self.functions[""](&mut self.data, arguments[0..arguments.len()].to_vec());
} else {
println!("{}", self.unknown_command_message);
}
}
Ok(())
}
/// builds a new Repl from the given data, use for compatibility reasons only, the new parser is better in pretty much every way possible
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::new_with_depreciated_parser(());
/// ```
#[inline]
pub fn new_with_depreciated_parser(data: T) -> Self {
Self {
data,
prompt: ">>>",
exit: "exit",
exit_message: "repl terminated",
functions: HashMap::new(),
empty_argument_message: "",
unknown_command_message: "",
parser_fn: |buf| {
buf.trim_end_matches('\n')
.trim_end_matches('\r')
.split_ascii_whitespace()
.map(|f| f.to_owned())
.collect()
},
}
}
/// same as `take_arg`, but returns the argument instead of storing it in self.argument
///
/// # Errors
/// this function returns an error if
/// - reading from stdin fails
/// - flushing stdout fails
pub fn get_input(&self) -> io::Result<Vec<String>> {
print!("{}", &self.prompt);
// flushing stdout because print! doesn't do it by default
io::stdout().flush()?;
let mut buf = String::new();
io::stdin().read_line(&mut buf)?;
Ok((self.parser_fn)(buf.trim().to_owned()))
}
/// builder style method for adding a function.
/// this function is chainable, use `add_function` if you don't want it to be chainable.
///
/// # Example
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_function("hello", hello);
///
/// fn hello(_: &mut (), _: Vec<String>) {
/// println!("hello");
/// }
/// ```
#[inline]
pub fn with_function(mut self, name: &'a str, func: fn(&mut T, Vec<String>)) -> Self {
self.add_function(name, func);
self
}
/// builder style method for changing the parser.
/// this function is chainable, use `set_parser` if you don't want it to be chainable.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_parser(|raw| vec![raw]);
/// ```
#[inline]
pub fn with_parser(mut self, parser: fn(String) -> Vec<String>) -> Self {
self.set_parser(parser);
self
}
/// builder style method for changing the data.
/// this function is chainable, use `set_data` if you don't want it to be chainable.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_data(());
/// ```
#[inline]
pub fn with_data(mut self, data: T) -> Self {
self.set_data(data);
self
}
/// builder style method for changing the prompt.
/// this function is chainable, use `set_prompt` if you don't want it to be chainable.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_prompt("+>");
/// // the repl will display +> after every message now, instead of the default ">>>"
/// ```
#[inline]
pub fn with_prompt(mut self, prompt: &'a str) -> Self {
self.set_prompt(prompt);
self
}
/// builder style method for changing the exit command.
/// this function is chainable, use `set_exit_command` if you don't want it to be chainable.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_exit_command("close");
/// // the repl will close if you give "close" as input now
/// ```
#[inline]
pub fn with_exit_command(mut self, exit: &'a str) -> Self {
self.set_exit_command(exit);
self
}
/// builder style method for changing the exit message.
/// this function is chainable, use `set_exit_message` if you don't want it to be chainable.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_exit_message("repl closed!");
/// // the repl will display "repl closed!" on termination
/// ```
#[inline]
pub fn with_exit_message(mut self, exit_message: &'a str) -> Self {
self.set_exit_message(exit_message);
self
}
/// builder style method for changing the exit message.
/// this function is chainable, use `set_exit_message` if you don't want it to be chainable.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let repl: Repl<()> = Repl::default().with_empty_argument_message("empty arg :(");
/// // the repl will display "empty arg :(" on termination
/// ```
#[inline]
pub fn with_empty_argument_message(mut self, empty_argument_message: &'a str) -> Self {
self.set_empty_argument_message(empty_argument_message);
self
}
/// adds function to Repl, not chainable, use `with_function` if you want chaining
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<()> = Repl::default();
/// repl.add_function("hello", hello);
/// fn hello(_: &mut (), _: Vec<String>) {
/// println!("Hello!")
/// }
/// ```
#[inline]
pub fn add_function(&mut self, name: &'a str, func: fn(&mut T, Vec<String>)) {
self.functions.insert(name, utils::FnPtr(func));
}
/// sets data to argument, NOT chainable, use `with_data` if you want chaining.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<i32> = Repl::default();
/// repl.set_data(100);
/// ```
#[inline]
pub fn set_data(&mut self, data: T) {
self.data = data;
}
/// sets prompt to argument, NOT chainable, use `with_prompt` if you want chaining.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<()> = Repl::default();
/// repl.set_prompt(":>");
///
/// ```
#[inline]
pub fn set_prompt(&mut self, prompt: &'a str) {
self.prompt = prompt;
}
/// sets exit command to argument, NOT chainable, use `with_exit_command` if you want chaining.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<()> = Repl::default();
/// repl.set_exit_command("close!");
/// ```
#[inline]
pub fn set_exit_command(&mut self, exit: &'a str) {
self.exit = exit;
}
/// sets exit message to argument, NOT chainable, use `with_exit_message` if you want chaining.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<()> = Repl::default();
/// repl.set_exit_message("bye!");
/// ```
#[inline]
pub fn set_exit_message(&mut self, exit_message: &'a str) {
self.exit_message = exit_message;
}
/// sets exit message to argument, NOT chainable, use `with_exit_message` if you want chaining.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<()> = Repl::default();
/// repl.set_empty_argument_message("empty argument list!");
/// ```
#[inline]
pub fn set_empty_argument_message(&mut self, empty_argument_message: &'a str) {
self.empty_argument_message = empty_argument_message;
}
/// sets parser function to argument, NOT chainable, use `with_parser` if you want chaining.
///
/// # Examples
///
/// ```rust
/// use repl_framework::Repl;
/// let mut repl: Repl<i32> = Repl::default();
/// repl.set_parser(|raw| vec![raw]);
/// ```
#[inline]
pub fn set_parser(&mut self, parser: fn(String) -> Vec<String>) {
self.parser_fn = parser;
}
}