-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathinput.rs
More file actions
354 lines (312 loc) · 10.5 KB
/
input.rs
File metadata and controls
354 lines (312 loc) · 10.5 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
mod input_state;
mod io_layer;
use input_state::InputState;
use io_layer::IoLayer;
/// Main entrypoint for this module. Display a prompt and read one or more values from the user.
/// See [the crate docs](module@crate)
pub fn get_input<BI: BasicInput, S: AsRef<str>>(prompt: S) -> BI {
let mut io = IoLayer::get_io();
get_input_from_io(prompt, &mut io)
}
// This is the trait that we return to the user, which allows us to have
// a different function for each return type.
// let one_input : f32 = get_input("...");
// let (x, y) : (f32, f32) = get_input("...");
// let (a, b, c) : (f32, f32, f32) = get_input("...");
pub trait BasicInput {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError>
where
Self: Sized;
}
// --------------------------------------------------------------------------------
impl BasicInput for f32 {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_f32s(s, 1)?;
Ok(state.data[0].as_f32())
}
}
impl BasicInput for (f32, f32) {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_f32s(s, 2)?;
Ok((state.data[0].as_f32(), state.data[1].as_f32()))
}
}
impl BasicInput for (f32, f32, f32) {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_f32s(s, 3)?;
Ok((
state.data[0].as_f32(),
state.data[1].as_f32(),
state.data[2].as_f32(),
))
}
}
impl BasicInput for (f32, f32, f32, f32) {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_f32s(s, 4)?;
Ok((
state.data[0].as_f32(),
state.data[1].as_f32(),
state.data[2].as_f32(),
state.data[3].as_f32(),
))
}
}
impl BasicInput for (f32, f32, f32, f32, f32, f32, f32, f32, f32, f32) {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_f32s(s, 10)?;
Ok((
state.data[0].as_f32(),
state.data[1].as_f32(),
state.data[2].as_f32(),
state.data[3].as_f32(),
state.data[4].as_f32(),
state.data[5].as_f32(),
state.data[6].as_f32(),
state.data[7].as_f32(),
state.data[8].as_f32(),
state.data[9].as_f32(),
))
}
}
impl BasicInput for String {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_strings(s, 1)?;
Ok(state.data[0].as_string())
}
}
impl BasicInput for (String, String) {
fn from_string<S: AsRef<str>>(s: S, state: &mut InputState) -> Result<Self, BasicInputError> {
state.input_strings(s, 2)?;
Ok((state.data[0].as_string(), state.data[1].as_string()))
}
}
// --------------------------------------------------------------------------------
fn get_input_from_io<BI: BasicInput, S: AsRef<str>>(prompt: S, io: &mut IoLayer) -> BI {
let mut state = InputState {
io,
data: Vec::new(),
};
state.io.raw_output_string(format!("{}? ", prompt.as_ref()));
loop {
let buffer = state.io.raw_input_string();
match BI::from_string(buffer, &mut state) {
Ok(result) => return result,
Err(e) => match e {
BasicInputError::ParseFailed => {
state.io.raw_output_string("?REENTER\n");
state.io.raw_output_string(format!("{}? ", prompt.as_ref()));
state.data.clear();
}
BasicInputError::MissingInput => {
state.io.raw_output_string("??? ");
}
},
}
}
}
#[derive(Debug)]
pub enum BasicInputError {
ParseFailed,
MissingInput,
}
#[cfg(test)]
mod test {
use crate::input::get_input_from_io;
use crate::input::io_layer::{IoLayer, TextInput, TextOutput};
use std::collections::VecDeque;
fn getio() -> IoLayer {
IoLayer {
output: Box::new(WriteTestLayer::new()),
input: Box::new(ReadTestLayer::new()),
}
}
#[test]
fn test_trait_single_input() {
let mut testio = getio();
testio.input.set_input_text("2");
let result: f32 = get_input_from_io("prompt", &mut testio);
assert_eq!(2.0, result);
}
#[test]
fn test_trait_dual_input() {
let mut testio = getio();
testio.input.set_input_text("2,3");
let result: (f32, f32) = get_input_from_io("prompt", &mut testio);
assert_eq!(
"prompt? ".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(2.0, result.0);
assert_eq!(3.0, result.1);
}
#[test]
fn test_invalid_input() {
let mut testio = getio();
testio.input.set_input_text("abc");
testio.input.set_input_text("2");
let result: f32 = get_input_from_io("prompt", &mut testio);
assert_eq!(
"prompt? ".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(
"?REENTER\n".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(2.0, result);
}
#[test]
fn test_multiple_invalid_input() {
let mut testio = getio();
testio.input.set_input_text("abc");
testio.input.set_input_text("2,a");
testio.input.set_input_text("2,3");
let result: (f32, f32) = get_input_from_io("prompt", &mut testio);
assert_eq!(
"prompt? ".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(
"?REENTER\n".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(
"prompt? ".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(
"?REENTER\n".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(
"prompt? ".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!(2.0, result.0);
assert_eq!(3.0, result.1);
}
#[test]
fn two_inputs_over_multiple_lines() {
let mut testio = getio();
testio.input.set_input_text("2");
testio.input.set_input_text("3");
let result: (f32, f32) = get_input_from_io("prompt", &mut testio);
assert_eq!(
"prompt? ".to_string(),
testio.output.get_last_line().unwrap()
);
assert_eq!("??? ".to_string(), testio.output.get_last_line().unwrap());
assert_eq!(2.0, result.0);
assert_eq!(3.0, result.1);
}
#[test]
fn test_triple_f32() {
let mut testio = getio();
testio.input.set_input_text("1, 2, 3");
let result: (f32, f32, f32) = get_input_from_io("enter 3 numbers", &mut testio);
assert_eq!(1.0, result.0);
assert_eq!(2.0, result.1);
assert_eq!(3.0, result.2);
}
#[test]
fn test_input_string() {
let mut testio = getio();
testio.input.set_input_text("hello");
let result: String = get_input_from_io("String prompt", &mut testio);
assert_eq!("hello".to_string(), result);
}
#[test]
fn test_two_input_strings() {
let mut testio = getio();
testio.input.set_input_text("hello, world");
let result: (String, String) = get_input_from_io("Double string prompt", &mut testio);
assert_eq!("hello".to_string(), result.0);
assert_eq!("world".to_string(), result.1);
}
#[test]
fn test_four_nums() {
let mut testio = getio();
testio.input.set_input_text("1, 2, 3, 4");
let result: (f32, f32, f32, f32) = get_input_from_io("whatever", &mut testio);
assert_eq!(4.0, result.3);
}
#[test]
fn test_extra_ignored() {
let mut testio = getio();
testio.input.set_input_text("1, 2, 3, 4");
let result: (f32, f32) = get_input_from_io("whatever", &mut testio);
assert_eq!(Some("whatever? "), testio.output.get_last_line().as_deref());
assert_eq!(
Some("?EXTRA IGNORED\n"),
testio.output.get_last_line().as_deref()
);
assert_eq!(2.0, result.1);
}
#[test]
fn test_extra_ignored_bad_parse() {
let mut testio = getio();
testio.input.set_input_text("1, 2, hello");
let result: (f32, f32) = get_input_from_io("whatever", &mut testio);
assert_eq!(Some("whatever? "), testio.output.get_last_line().as_deref());
assert_eq!(
Some("?EXTRA IGNORED\n"),
testio.output.get_last_line().as_deref()
);
assert_eq!(2.0, result.1);
}
#[test]
fn test_ten_nums() {
let mut testio = getio();
testio.input.set_input_text("1, 2, 3, 4, 5, 6, 7, 8, 9, 10");
let result: (f32, f32, f32, f32, f32, f32, f32, f32, f32, f32) =
get_input_from_io("whatever", &mut testio);
assert_eq!(Some("whatever? "), testio.output.get_last_line().as_deref());
assert_eq!(None, testio.output.get_last_line());
assert_eq!(10.0, result.9);
}
//
// Helpers for test i/o
//
pub struct ReadTestLayer {
next_line: VecDeque<String>,
}
impl ReadTestLayer {
pub fn new() -> Self {
Self {
next_line: VecDeque::new(),
}
}
}
impl TextInput for ReadTestLayer {
fn read(&mut self) -> String {
if let Some(line) = self.next_line.pop_front() {
println!(">> returning {line}");
line
} else {
panic!("Test tried to read too much input");
}
}
fn set_input_text(&mut self, text: &str) {
self.next_line.push_back(text.to_string());
}
}
pub struct WriteTestLayer {
last_items_written: VecDeque<String>,
}
impl WriteTestLayer {
pub fn new() -> Self {
Self {
last_items_written: VecDeque::new(),
}
}
}
impl TextOutput for WriteTestLayer {
fn write(&mut self, to_write: &str) {
self.last_items_written.push_back(to_write.into());
}
fn get_last_line(&mut self) -> Option<String> {
self.last_items_written.pop_front()
}
}
}