1
+ use failure:: Fallible ;
1
2
use size_format:: SizeFormatterBinary ;
2
3
use std:: { env, io:: Write , process, time:: Instant } ;
3
4
4
- fn main ( ) {
5
- better_panic:: install ( ) ;
5
+ struct Args {
6
+ partitions : u32 ,
7
+ free : Vec < String > ,
8
+ }
6
9
7
- let all_args: Vec < String > = env:: args ( ) . collect ( ) ;
8
- let args = & all_args[ 1 ..] ;
10
+ enum Command {
11
+ Crosscheck ,
12
+ Bench ,
13
+ Run ,
14
+ }
9
15
10
- if args. len ( ) == 0 {
11
- println ! ( "Usage: divsuftest bench|crosscheck INPUT [LENGTH]" ) ;
12
- process:: exit ( 1 ) ;
16
+ impl Command {
17
+ fn parse ( s : & str ) -> Option < Self > {
18
+ match s {
19
+ "crosscheck" => Some ( Self :: Crosscheck ) ,
20
+ "bench" => Some ( Self :: Bench ) ,
21
+ "run" => Some ( Self :: Run ) ,
22
+ _ => None ,
23
+ }
13
24
}
25
+ }
14
26
15
- let ( cmd, args) = ( & args[ 0 ] , & args[ 1 ..] ) ;
27
+ fn main ( ) -> Fallible < ( ) > {
28
+ better_panic:: install ( ) ;
16
29
17
- let input_path = args. get ( 0 ) . expect ( "INPUT argument expected" ) ;
30
+ let mut args = pico_args:: Arguments :: from_env ( ) ;
31
+ let args = Args {
32
+ partitions : args. opt_value_from_str ( "--partitions" ) ?. unwrap_or ( 1 ) ,
33
+ free : args. free ( ) ?,
34
+ } ;
35
+
36
+ if args. free . is_empty ( ) {
37
+ usage ( ) ;
38
+ }
39
+ let cmd = Command :: parse ( & args. free . get ( 0 ) . unwrap_or_else ( || {
40
+ usage ( ) ;
41
+ unreachable ! ( ) ;
42
+ } ) )
43
+ . expect ( "Command should be one of crosscheck bench or run" ) ;
44
+
45
+ let input_path = args. free . get ( 1 ) . unwrap_or_else ( || {
46
+ usage ( ) ;
47
+ unreachable ! ( ) ;
48
+ } ) ;
18
49
let input_full = std:: fs:: read ( input_path) . unwrap ( ) ;
19
50
let len = args
20
- . get ( 1 )
51
+ . free
52
+ . get ( 2 )
21
53
. map ( parse_size)
22
54
. unwrap_or_else ( || input_full. len ( ) ) ;
23
55
let input = & input_full[ ..len] ;
@@ -26,8 +58,8 @@ fn main() {
26
58
SizeFormatterBinary :: new( input. len( ) as u64 )
27
59
) ;
28
60
29
- match cmd. as_ref ( ) {
30
- "crosscheck" => {
61
+ match cmd {
62
+ Command :: Crosscheck => {
31
63
#[ cfg( not( feature = "crosscheck" ) ) ]
32
64
{
33
65
println ! (
@@ -38,12 +70,17 @@ fn main() {
38
70
}
39
71
40
72
#[ cfg( feature = "crosscheck" ) ]
41
- command_crosscheck ( input) ;
73
+ command_crosscheck ( input) ? ;
42
74
}
43
- "bench" => command_bench ( input) ,
44
- "run" => command_run ( input) ,
45
- x => panic ! ( "unknown command {:?}" , x) ,
75
+ Command :: Bench => command_bench ( input) ?,
76
+ Command :: Run => command_run ( input) ?,
46
77
}
78
+ Ok ( ( ) )
79
+ }
80
+
81
+ fn usage ( ) {
82
+ println ! ( "Usage: divsuftest bench|crosscheck|run INPUT [LENGTH]" ) ;
83
+ process:: exit ( 1 ) ;
47
84
}
48
85
49
86
#[ cfg( feature = "crosscheck" ) ]
@@ -79,13 +116,15 @@ fn command_crosscheck(input: &[u8]) {
79
116
} ;
80
117
}
81
118
82
- fn command_run ( input : & [ u8 ] ) {
119
+ fn command_run ( input : & [ u8 ] ) -> Fallible < ( ) > {
83
120
let before = Instant :: now ( ) ;
84
121
divsufsort:: sort ( input) ;
85
122
println ! ( "Done in {:?}" , before. elapsed( ) ) ;
123
+
124
+ Ok ( ( ) )
86
125
}
87
126
88
- fn command_bench ( input : & [ u8 ] ) {
127
+ fn command_bench ( input : & [ u8 ] ) -> Fallible < ( ) > {
89
128
#[ cfg( debug_assertions) ]
90
129
{
91
130
println ! ( "==========================================" ) ;
@@ -151,6 +190,7 @@ fn command_bench(input: &[u8]) {
151
190
152
191
Table :: new ( rows, Default :: default ( ) ) . print_stdout ( ) . unwrap ( ) ;
153
192
}
193
+ Ok ( ( ) )
154
194
}
155
195
156
196
fn parse_size < I : AsRef < str > > ( input : I ) -> usize {
0 commit comments