@@ -10,7 +10,7 @@ extern crate rustyline;
10
10
extern crate serde_derive;
11
11
12
12
#[ macro_use]
13
- extern crate error_chain ;
13
+ extern crate failure ;
14
14
15
15
use std:: fs:: File ;
16
16
use std:: io:: Read ;
@@ -25,6 +25,7 @@ use chalk_engine::fallible::NoSolution;
25
25
use chalk_solve:: ext:: * ;
26
26
use chalk_solve:: solve:: SolverChoice ;
27
27
use docopt:: Docopt ;
28
+ use failure:: Fallible ;
28
29
use rustyline:: error:: ReadlineError ;
29
30
30
31
const USAGE : & ' static str = "
@@ -51,18 +52,6 @@ struct Args {
51
52
flag_no_cache : bool ,
52
53
}
53
54
54
- error_chain ! {
55
- links {
56
- Parse ( chalk_parse:: errors:: Error , chalk_parse:: errors:: ErrorKind ) ;
57
- Chalk ( chalk:: errors:: Error , chalk:: errors:: ErrorKind ) ;
58
- }
59
-
60
- foreign_links {
61
- Io ( :: std:: io:: Error ) ;
62
- Rustyline ( ReadlineError ) ;
63
- }
64
- }
65
-
66
55
/// A loaded and parsed program.
67
56
struct Program {
68
57
text : String ,
@@ -75,7 +64,7 @@ impl Program {
75
64
/// a [`SolverChoice`].
76
65
///
77
66
/// [`SolverChoice`]: struct.solve.SolverChoice.html
78
- fn new ( text : String , solver_choice : SolverChoice ) -> Result < Program > {
67
+ fn new ( text : String , solver_choice : SolverChoice ) -> Fallible < Program > {
79
68
ChalkDatabase :: with_program ( Arc :: new ( text. clone ( ) ) , solver_choice, |db| {
80
69
let ir = db. checked_program ( ) . unwrap ( ) ;
81
70
let env = db. environment ( ) . unwrap ( ) ;
@@ -84,9 +73,7 @@ impl Program {
84
73
}
85
74
}
86
75
87
- quick_main ! ( run) ;
88
-
89
- fn run ( ) -> Result < ( ) > {
76
+ fn run ( ) -> Fallible < ( ) > {
90
77
// Parse the command line arguments.
91
78
let args: & Args = & Docopt :: new ( USAGE )
92
79
. and_then ( |d| d. deserialize ( ) )
@@ -121,12 +108,13 @@ fn run() -> Result<()> {
121
108
// Check that a program was provided.
122
109
// TODO: It's customary to print Usage info when an error like this
123
110
// happens.
124
- let prog =
125
- prog. ok_or ( "error: cannot eval without a program; use `--program` to specify one." ) ?;
111
+ let prog = prog. ok_or ( format_err ! (
112
+ "error: cannot eval without a program; use `--program` to specify one."
113
+ ) ) ?;
126
114
127
115
// Evaluate the goal(s). If any goal returns an error, print the error
128
116
// and exit.
129
- chalk_ir:: tls:: set_current_program ( & prog. ir , || -> Result < ( ) > {
117
+ chalk_ir:: tls:: set_current_program ( & prog. ir , || -> Fallible < ( ) > {
130
118
for g in & args. flag_goal {
131
119
if let Err ( e) = goal ( & args, g, & prog) {
132
120
eprintln ! ( "error: {}" , e) ;
@@ -145,7 +133,7 @@ fn run() -> Result<()> {
145
133
///
146
134
/// The loop terminates (and the program ends) when EOF is reached or if an error
147
135
/// occurs while reading the next line.
148
- fn readline_loop < F > ( rl : & mut rustyline:: Editor < ( ) > , prompt : & str , mut f : F ) -> Result < ( ) >
136
+ fn readline_loop < F > ( rl : & mut rustyline:: Editor < ( ) > , prompt : & str , mut f : F ) -> Fallible < ( ) >
149
137
where
150
138
F : FnMut ( & mut rustyline:: Editor < ( ) > , & str ) ,
151
139
{
@@ -179,7 +167,7 @@ fn process(
179
167
command : & str ,
180
168
rl : & mut rustyline:: Editor < ( ) > ,
181
169
prog : & mut Option < Program > ,
182
- ) -> Result < ( ) > {
170
+ ) -> Fallible < ( ) > {
183
171
if command == "help" || command == "h" {
184
172
// Print out interpreter commands.
185
173
// TODO: Implement "help <command>" for more specific help.
@@ -200,12 +188,12 @@ fn process(
200
188
// The command is either "print", "lowered", or a goal.
201
189
202
190
// Check that a program has been loaded.
203
- let prog = prog
204
- . as_ref ( )
205
- . ok_or ( "no program currently loaded; type 'help' to see available commands" ) ?;
191
+ let prog = prog. as_ref ( ) . ok_or ( format_err ! (
192
+ "no program currently loaded; type 'help' to see available commands"
193
+ ) ) ?;
206
194
207
195
// Attempt to parse the program.
208
- chalk_ir:: tls:: set_current_program ( & prog. ir , || -> Result < ( ) > {
196
+ chalk_ir:: tls:: set_current_program ( & prog. ir , || -> Fallible < ( ) > {
209
197
match command {
210
198
// Print out the loaded program.
211
199
"print" => println ! ( "{}" , prog. text) ,
@@ -228,7 +216,7 @@ fn process(
228
216
/// Load the file into a string, and parse it.
229
217
// TODO: Could we pass in an Options struct or something? The Args struct
230
218
// still has Strings where it should have Enums... (e.g. solver_choice)
231
- fn load_program ( args : & Args , filename : & str ) -> Result < Program > {
219
+ fn load_program ( args : & Args , filename : & str ) -> Fallible < Program > {
232
220
let mut text = String :: new ( ) ;
233
221
File :: open ( filename) ?. read_to_string ( & mut text) ?;
234
222
Ok ( Program :: new ( text, args. solver_choice ( ) ) ?)
@@ -248,8 +236,8 @@ fn help() {
248
236
}
249
237
250
238
/// Read a program from the command-line. Stop reading when EOF is read. If
251
- /// an error occurs while reading, a Result:: Err is returned.
252
- fn read_program ( rl : & mut rustyline:: Editor < ( ) > ) -> Result < String > {
239
+ /// an error occurs while reading, a ` Err` is returned.
240
+ fn read_program ( rl : & mut rustyline:: Editor < ( ) > ) -> Fallible < String > {
253
241
println ! ( "Enter a program; press Ctrl-D when finished" ) ;
254
242
let mut text = String :: new ( ) ;
255
243
readline_loop ( rl, "| " , |_, line| {
@@ -262,7 +250,7 @@ fn read_program(rl: &mut rustyline::Editor<()>) -> Result<String> {
262
250
/// Parse a goal and attempt to solve it, using the specified solver.
263
251
// TODO: Could we pass in an Options struct or something? The Args struct
264
252
// still has Strings where it should have Enums... (e.g. solver_choice)
265
- fn goal ( args : & Args , text : & str , prog : & Program ) -> Result < ( ) > {
253
+ fn goal ( args : & Args , text : & str , prog : & Program ) -> Fallible < ( ) > {
266
254
let goal = chalk_parse:: parse_goal ( text) ?. lower ( & * prog. ir ) ?;
267
255
let peeled_goal = goal. into_peeled_goal ( ) ;
268
256
match args
@@ -283,3 +271,15 @@ impl Args {
283
271
}
284
272
}
285
273
}
274
+
275
+ fn main ( ) {
276
+ use :: std:: io:: Write ;
277
+
278
+ :: std:: process:: exit ( match run ( ) {
279
+ Ok ( _) => 0 ,
280
+ Err ( ref e) => {
281
+ write ! ( & mut :: std:: io:: stderr( ) , "{}" , e) . expect ( "Error writing to stderr" ) ;
282
+ 1
283
+ }
284
+ } ) ;
285
+ }
0 commit comments