Skip to content

Commit 06f3db8

Browse files
shuf: use UResult
1 parent bcef1d6 commit 06f3db8

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/uu/shuf/src/shuf.rs

+34-34
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
// spell-checker:ignore (ToDO) cmdline evec seps rvec fdata
99

10-
#[macro_use]
11-
extern crate uucore;
12-
1310
use clap::{crate_version, App, Arg};
1411
use rand::Rng;
1512
use std::fs::File;
1613
use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write};
1714
use uucore::display::Quotable;
15+
use uucore::error::{FromIo, UResult, USimpleError};
1816
use uucore::InvalidEncodingHandling;
1917

2018
enum Mode {
@@ -52,7 +50,8 @@ mod options {
5250
pub static FILE: &str = "file";
5351
}
5452

55-
pub fn uumain(args: impl uucore::Args) -> i32 {
53+
#[uucore_procs::gen_uumain]
54+
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5655
let args = args
5756
.collect_str(InvalidEncodingHandling::ConvertLossy)
5857
.accept_any();
@@ -65,7 +64,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
6564
match parse_range(range) {
6665
Ok(m) => Mode::InputRange(m),
6766
Err(msg) => {
68-
crash!(1, "{}", msg);
67+
return Err(USimpleError::new(1, msg));
6968
}
7069
}
7170
} else {
@@ -77,8 +76,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
7776
Some(count) => match count.parse::<usize>() {
7877
Ok(val) => val,
7978
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+
));
8283
}
8384
},
8485
None => std::usize::MAX,
@@ -97,22 +98,22 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
9798
Mode::Echo(args) => {
9899
let mut evec = args.iter().map(String::as_bytes).collect::<Vec<_>>();
99100
find_seps(&mut evec, options.sep);
100-
shuf_bytes(&mut evec, options);
101+
shuf_bytes(&mut evec, options)?;
101102
}
102103
Mode::InputRange((b, e)) => {
103104
let rvec = (b..e).map(|x| format!("{}", x)).collect::<Vec<String>>();
104105
let mut rvec = rvec.iter().map(String::as_bytes).collect::<Vec<&[u8]>>();
105-
shuf_bytes(&mut rvec, options);
106+
shuf_bytes(&mut rvec, options)?;
106107
}
107108
Mode::Default(filename) => {
108-
let fdata = read_input_file(&filename);
109+
let fdata = read_input_file(&filename)?;
109110
let mut fdata = vec![&fdata[..]];
110111
find_seps(&mut fdata, options.sep);
111-
shuf_bytes(&mut fdata, options);
112+
shuf_bytes(&mut fdata, options)?;
112113
}
113114
}
114115

115-
0
116+
Ok(())
116117
}
117118

118119
pub fn uu_app() -> App<'static, 'static> {
@@ -180,22 +181,20 @@ pub fn uu_app() -> App<'static, 'static> {
180181
.arg(Arg::with_name(options::FILE).takes_value(true))
181182
}
182183

183-
fn read_input_file(filename: &str) -> Vec<u8> {
184+
fn read_input_file(filename: &str) -> UResult<Vec<u8>> {
184185
let mut file = BufReader::new(if filename == "-" {
185186
Box::new(stdin()) as Box<dyn Read>
186187
} 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>
191191
});
192192

193193
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()))?;
197196

198-
data
197+
Ok(data)
199198
}
200199

201200
fn find_seps(data: &mut Vec<&[u8]>, sep: u8) {
@@ -231,22 +230,22 @@ fn find_seps(data: &mut Vec<&[u8]>, sep: u8) {
231230
}
232231
}
233232

234-
fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) {
233+
fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) -> UResult<()> {
235234
let mut output = BufWriter::new(match opts.output {
236235
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+
}
241241
});
242242

243243
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+
}
250249
None => WrappedRng::RngDefault(rand::thread_rng()),
251250
};
252251

@@ -268,10 +267,10 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) {
268267
// write the randomly chosen value and the separator
269268
output
270269
.write_all(input[r])
271-
.unwrap_or_else(|e| crash!(1, "write failed: {}", e));
270+
.map_err_context(|| "write failed".to_string())?;
272271
output
273272
.write_all(&[opts.sep])
274-
.unwrap_or_else(|e| crash!(1, "write failed: {}", e));
273+
.map_err_context(|| "write failed".to_string())?;
275274

276275
// if we do not allow repeats, remove the chosen value from the input vector
277276
if !opts.repeat {
@@ -284,6 +283,7 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) {
284283

285284
count -= 1;
286285
}
286+
Ok(())
287287
}
288288

289289
fn parse_range(input_range: &str) -> Result<(usize, usize), String> {

0 commit comments

Comments
 (0)