7
7
8
8
// spell-checker:ignore (ToDO) cmdline evec seps rvec fdata
9
9
10
- #[ macro_use]
11
- extern crate uucore;
12
-
13
10
use clap:: { crate_version, App , Arg } ;
14
11
use rand:: Rng ;
15
12
use std:: fs:: File ;
16
13
use std:: io:: { stdin, stdout, BufReader , BufWriter , Read , Write } ;
17
14
use uucore:: display:: Quotable ;
15
+ use uucore:: error:: { FromIo , UResult , USimpleError } ;
18
16
use uucore:: InvalidEncodingHandling ;
19
17
20
18
enum Mode {
@@ -52,7 +50,8 @@ mod options {
52
50
pub static FILE : & str = "file" ;
53
51
}
54
52
55
- pub fn uumain ( args : impl uucore:: Args ) -> i32 {
53
+ #[ uucore_procs:: gen_uumain]
54
+ pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
56
55
let args = args
57
56
. collect_str ( InvalidEncodingHandling :: ConvertLossy )
58
57
. accept_any ( ) ;
@@ -65,7 +64,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
65
64
match parse_range ( range) {
66
65
Ok ( m) => Mode :: InputRange ( m) ,
67
66
Err ( msg) => {
68
- crash ! ( 1 , "{}" , msg) ;
67
+ return Err ( USimpleError :: new ( 1 , msg) ) ;
69
68
}
70
69
}
71
70
} else {
@@ -77,8 +76,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
77
76
Some ( count) => match count. parse :: < usize > ( ) {
78
77
Ok ( val) => val,
79
78
Err ( _) => {
80
- show_error ! ( "invalid line count: {}" , count. quote( ) ) ;
81
- return 1 ;
79
+ return Err ( USimpleError :: new (
80
+ 1 ,
81
+ format ! ( "invalid line count: {}" , count. quote( ) ) ,
82
+ ) ) ;
82
83
}
83
84
} ,
84
85
None => std:: usize:: MAX ,
@@ -97,22 +98,22 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
97
98
Mode :: Echo ( args) => {
98
99
let mut evec = args. iter ( ) . map ( String :: as_bytes) . collect :: < Vec < _ > > ( ) ;
99
100
find_seps ( & mut evec, options. sep ) ;
100
- shuf_bytes ( & mut evec, options) ;
101
+ shuf_bytes ( & mut evec, options) ? ;
101
102
}
102
103
Mode :: InputRange ( ( b, e) ) => {
103
104
let rvec = ( b..e) . map ( |x| format ! ( "{}" , x) ) . collect :: < Vec < String > > ( ) ;
104
105
let mut rvec = rvec. iter ( ) . map ( String :: as_bytes) . collect :: < Vec < & [ u8 ] > > ( ) ;
105
- shuf_bytes ( & mut rvec, options) ;
106
+ shuf_bytes ( & mut rvec, options) ? ;
106
107
}
107
108
Mode :: Default ( filename) => {
108
- let fdata = read_input_file ( & filename) ;
109
+ let fdata = read_input_file ( & filename) ? ;
109
110
let mut fdata = vec ! [ & fdata[ ..] ] ;
110
111
find_seps ( & mut fdata, options. sep ) ;
111
- shuf_bytes ( & mut fdata, options) ;
112
+ shuf_bytes ( & mut fdata, options) ? ;
112
113
}
113
114
}
114
115
115
- 0
116
+ Ok ( ( ) )
116
117
}
117
118
118
119
pub fn uu_app ( ) -> App < ' static , ' static > {
@@ -180,22 +181,20 @@ pub fn uu_app() -> App<'static, 'static> {
180
181
. arg ( Arg :: with_name ( options:: FILE ) . takes_value ( true ) )
181
182
}
182
183
183
- fn read_input_file ( filename : & str ) -> Vec < u8 > {
184
+ fn read_input_file ( filename : & str ) -> UResult < Vec < u8 > > {
184
185
let mut file = BufReader :: new ( if filename == "-" {
185
186
Box :: new ( stdin ( ) ) as Box < dyn Read >
186
187
} else {
187
- match File :: open ( filename) {
188
- Ok ( f) => Box :: new ( f) as Box < dyn Read > ,
189
- Err ( e) => crash ! ( 1 , "failed to open {}: {}" , filename. quote( ) , e) ,
190
- }
188
+ let file = File :: open ( filename)
189
+ . map_err_context ( || format ! ( "failed to open {}" , filename. quote( ) ) ) ?;
190
+ Box :: new ( file) as Box < dyn Read >
191
191
} ) ;
192
192
193
193
let mut data = Vec :: new ( ) ;
194
- if let Err ( e) = file. read_to_end ( & mut data) {
195
- crash ! ( 1 , "failed reading {}: {}" , filename. quote( ) , e)
196
- } ;
194
+ file. read_to_end ( & mut data)
195
+ . map_err_context ( || format ! ( "failed reading {}" , filename. quote( ) ) ) ?;
197
196
198
- data
197
+ Ok ( data)
199
198
}
200
199
201
200
fn find_seps ( data : & mut Vec < & [ u8 ] > , sep : u8 ) {
@@ -231,22 +230,22 @@ fn find_seps(data: &mut Vec<&[u8]>, sep: u8) {
231
230
}
232
231
}
233
232
234
- fn shuf_bytes ( input : & mut Vec < & [ u8 ] > , opts : Options ) {
233
+ fn shuf_bytes ( input : & mut Vec < & [ u8 ] > , opts : Options ) -> UResult < ( ) > {
235
234
let mut output = BufWriter :: new ( match opts. output {
236
235
None => Box :: new ( stdout ( ) ) as Box < dyn Write > ,
237
- Some ( s) => match File :: create ( & s[ ..] ) {
238
- Ok ( f) => Box :: new ( f) as Box < dyn Write > ,
239
- Err ( e) => crash ! ( 1 , "failed to open {} for writing: {}" , s. quote( ) , e) ,
240
- } ,
236
+ Some ( s) => {
237
+ let file = File :: create ( & s[ ..] )
238
+ . map_err_context ( || format ! ( "failed to open {} for writing" , s. quote( ) ) ) ?;
239
+ Box :: new ( file) as Box < dyn Write >
240
+ }
241
241
} ) ;
242
242
243
243
let mut rng = match opts. random_source {
244
- Some ( r) => WrappedRng :: RngFile ( rand:: rngs:: adapter:: ReadRng :: new (
245
- match File :: open ( & r[ ..] ) {
246
- Ok ( f) => f,
247
- Err ( e) => crash ! ( 1 , "failed to open random source {}: {}" , r. quote( ) , e) ,
248
- } ,
249
- ) ) ,
244
+ Some ( r) => {
245
+ let file = File :: open ( & r[ ..] )
246
+ . map_err_context ( || format ! ( "failed to open random source {}" , r. quote( ) ) ) ?;
247
+ WrappedRng :: RngFile ( rand:: rngs:: adapter:: ReadRng :: new ( file) )
248
+ }
250
249
None => WrappedRng :: RngDefault ( rand:: thread_rng ( ) ) ,
251
250
} ;
252
251
@@ -268,10 +267,10 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) {
268
267
// write the randomly chosen value and the separator
269
268
output
270
269
. write_all ( input[ r] )
271
- . unwrap_or_else ( |e| crash ! ( 1 , "write failed: {}" , e ) ) ;
270
+ . map_err_context ( || "write failed" . to_string ( ) ) ? ;
272
271
output
273
272
. write_all ( & [ opts. sep ] )
274
- . unwrap_or_else ( |e| crash ! ( 1 , "write failed: {}" , e ) ) ;
273
+ . map_err_context ( || "write failed" . to_string ( ) ) ? ;
275
274
276
275
// if we do not allow repeats, remove the chosen value from the input vector
277
276
if !opts. repeat {
@@ -284,6 +283,7 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) {
284
283
285
284
count -= 1 ;
286
285
}
286
+ Ok ( ( ) )
287
287
}
288
288
289
289
fn parse_range ( input_range : & str ) -> Result < ( usize , usize ) , String > {
0 commit comments