-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.rs
More file actions
244 lines (207 loc) · 7.05 KB
/
calculator.rs
File metadata and controls
244 lines (207 loc) · 7.05 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
//! A simple calculator REPL with just three commands:
//!
//! * `add` -- Adds a value to the register and prints its contents.
//! * `subtract` -- Subtracts a value from the register and prints its contents.
//! * `print` -- Prints the contents of the register, leaving it unchanged.
//!
//! The example also includes the `help` and `quit` built-in commands.
use revolver::command;
use revolver::command::Commander;
use revolver::command::NamedCommandParser;
use revolver::looper::Looper;
use revolver::terminal::{AccessTerminalError, Streaming, Terminal};
use std::convert::Infallible;
#[derive(Debug, Default)]
pub struct Register {
value: f64,
}
impl Register {
/// Prints the contents of the register.
///
/// # Errors
/// [`AccessTerminalError`] if a terminal I/O error occurs.
fn print(&self, terminal: &mut impl Terminal) -> Result<(), AccessTerminalError> {
terminal.print_line(&format!("{:?}", self))
}
}
/// Creates a new [`Commander`] instance.
fn commander<T: Terminal>() -> Commander<Register, Infallible, T> {
let parsers: Vec<Box<dyn NamedCommandParser<_, Context=_, Error=_>>> = vec![
Box::new(add::Parser),
Box::new(print::Parser),
Box::new(subtract::Parser),
Box::new(command::help::Parser::default()),
Box::new(command::quit::Parser::default()),
];
Commander::new(parsers)
}
fn main() {
let mut terminal = Streaming::default();
let commander = commander();
let mut register = Register::default();
let mut looper = Looper::new(&mut terminal, &commander, &mut register);
looper.run().unwrap();
}
mod add {
//! A command and parser pair for adding a value to the register.
use crate::Register;
use revolver::command::{
ApplyCommandError, ApplyOutcome, Command, Description, Example, NamedCommandParser,
ParseCommandError,
};
use revolver::looper::Looper;
use revolver::terminal::Terminal;
use std::borrow::Cow;
use std::convert::Infallible;
use std::str::FromStr;
struct Add {
value: f64,
}
impl<T: Terminal> Command<T> for Add {
type Context = Register;
type Error = Infallible;
fn apply(
&mut self,
looper: &mut Looper<Register, Infallible, T>,
) -> Result<ApplyOutcome, ApplyCommandError<Infallible>> {
let (terminal, _, register) = looper.split();
register.value += self.value;
register.print(terminal)?;
Ok(ApplyOutcome::Applied)
}
}
pub struct Parser;
impl<T: Terminal> NamedCommandParser<T> for Parser {
type Context = Register;
type Error = Infallible;
fn parse(
&self,
s: &str,
) -> Result<Box<dyn Command<T, Context = Self::Context, Error = Self::Error>>, ParseCommandError> {
let value = f64::from_str(s).map_err(ParseCommandError::convert)?;
Ok(Box::new(Add { value }))
}
fn shorthand(&self) -> Option<Cow<'static, str>> {
Some("a".into())
}
fn name(&self) -> Cow<'static, str> {
"add".into()
}
fn description(&self) -> Description {
Description {
purpose: "Adds a value to the register.".into(),
usage: "<value>".into(),
examples: vec![Example {
scenario: "adds 1.5 to the register".into(),
command: "1.5".into(),
}],
}
}
}
}
mod subtract {
//! A command and parser pair for subtracting a value from the register.
use crate::Register;
use revolver::command::{
ApplyCommandError, ApplyOutcome, Command, Description, Example, NamedCommandParser,
ParseCommandError,
};
use revolver::looper::Looper;
use revolver::terminal::Terminal;
use std::borrow::Cow;
use std::convert::Infallible;
use std::str::FromStr;
struct Subtract {
value: f64,
}
impl<T: Terminal> Command<T> for Subtract {
type Context = Register;
type Error = Infallible;
fn apply(
&mut self,
looper: &mut Looper<Self::Context, Self::Error, T>,
) -> Result<ApplyOutcome, ApplyCommandError<Infallible>> {
let (terminal, _, register) = looper.split();
register.value -= self.value;
register.print(terminal)?;
Ok(ApplyOutcome::Applied)
}
}
pub struct Parser;
impl<T: Terminal> NamedCommandParser<T> for Parser {
type Context = Register;
type Error = Infallible;
fn parse(
&self,
s: &str,
) -> Result<Box<dyn Command<T, Context = Self::Context, Error = Self::Error>>, ParseCommandError> {
let value = f64::from_str(s).map_err(ParseCommandError::convert)?;
Ok(Box::new(Subtract { value }))
}
fn shorthand(&self) -> Option<Cow<'static, str>> {
Some("s".into())
}
fn name(&self) -> Cow<'static, str> {
"subtract".into()
}
fn description(&self) -> Description {
Description {
purpose: "Subtracts a value from the register.".into(),
usage: "<value>".into(),
examples: vec![Example {
scenario: "subtracts 1.5 from the register".into(),
command: "1.5".into(),
}],
}
}
}
}
mod print {
//! A command and parser pair for printing the contents of the register.
use crate::Register;
use revolver::command::{
ApplyCommandError, ApplyOutcome, Command, Description, NamedCommandParser,
ParseCommandError,
};
use revolver::looper::Looper;
use revolver::terminal::Terminal;
use std::borrow::Cow;
use std::convert::Infallible;
struct Print;
impl<T: Terminal> Command<T> for Print {
type Context = Register;
type Error = Infallible;
fn apply(
&mut self,
looper: &mut Looper<Self::Context, Self::Error, T>,
) -> Result<ApplyOutcome, ApplyCommandError<Self::Error>> {
let (terminal, _, register) = looper.split();
register.print(terminal)?;
Ok(ApplyOutcome::Applied)
}
}
pub struct Parser;
impl<T: Terminal> NamedCommandParser<T> for Parser {
type Context = Register;
type Error = Infallible;
fn parse(
&self,
s: &str,
) -> Result<Box<dyn Command<T, Context = Self::Context, Error = Self::Error>>, ParseCommandError> {
self.parse_no_args(s, || Print)
}
fn shorthand(&self) -> Option<Cow<'static, str>> {
Some("p".into())
}
fn name(&self) -> Cow<'static, str> {
"print".into()
}
fn description(&self) -> Description {
Description {
purpose: "Prints the contents of the register.".into(),
usage: "".into(),
examples: Vec::default(),
}
}
}
}