interactive_test/
interactive_test.rs

1// MIT/Apache2 License
2
3use std::io::{self, prelude::*};
4use tinydeque::ArrayDeque;
5
6fn help() {
7    println!(
8        "
9pb [item] - Push an item onto the back of the stack.
10pf [item] - Push an item onto the back of the stack.
11ob - Pop an item from the back.
12of - Pop an item from the front.
13c - Get capacity.
14l - Get length.
15e - Get whether or not it's empty.
16f - Get whether or not it's full.
17d - Print debug info.
18h - Print this help menu.
19q - Quit."
20    );
21}
22
23fn main() {
24    let mut test_deque: ArrayDeque<[i32; 10]> = ArrayDeque::new();
25
26    help();
27    loop {
28        print!("> ");
29        io::stdout().flush().unwrap();
30        let mut line = String::new();
31        io::stdin().read_line(&mut line).unwrap();
32        if line.is_empty() {
33            continue;
34        }
35        let command = (
36            line.remove(0),
37            if !line.is_empty() {
38                Some(line.remove(0))
39            } else {
40                None
41            },
42        );
43        if line.chars().next() == Some(' ') {
44            line.remove(0);
45            line.pop();
46        }
47        let item = if line.len() > 0 {
48            match line.parse::<i32>() {
49                Ok(s) => s,
50                Err(_) => 0,
51            }
52        } else {
53            0i32
54        };
55
56        match command {
57            ('p', Some('b')) => {
58                if let Err(reject) = test_deque.try_push_back(item) {
59                    println!("Unable to push element onto deque back: {}", reject);
60                }
61            }
62            ('p', Some('f')) => {
63                if let Err(reject) = test_deque.try_push_front(item) {
64                    println!("Unable to push element onto deque front: {}", reject);
65                }
66            }
67            ('o', Some('b')) => println!("{:?}", test_deque.pop_back()),
68            ('o', Some('f')) => println!("{:?}", test_deque.pop_front()),
69            ('c', _) => println!("Capacity: {}", ArrayDeque::<[i32; 10]>::capacity()),
70            ('l', _) => println!("Length: {}", test_deque.len()),
71            ('e', _) => println!(
72                "Deque is{} empty",
73                if test_deque.is_empty() { "" } else { " not" }
74            ),
75            ('f', _) => println!(
76                "Deque is{} full",
77                if test_deque.is_full() { "" } else { " not " }
78            ),
79            ('d', _) => println!("{:?}", &test_deque),
80            ('h', _) => help(),
81            ('q', _) => return,
82            _ => println!("Unrecognized command"),
83        }
84    }
85}