1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #2845 from jfinkels/unexpand-uresult

unexpand: return UResult from uumain() function
This commit is contained in:
Sylvestre Ledru 2022-01-02 19:02:30 +01:00 committed by GitHub
commit e7dd56c1d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,6 +17,7 @@ use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Stdout, Write}
use std::str::from_utf8; use std::str::from_utf8;
use unicode_width::UnicodeWidthChar; use unicode_width::UnicodeWidthChar;
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static NAME: &str = "unexpand"; static NAME: &str = "unexpand";
@ -90,16 +91,15 @@ impl Options {
} }
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
let matches = uu_app().get_matches_from(args); let matches = uu_app().get_matches_from(args);
unexpand(Options::new(matches)); unexpand(Options::new(matches)).map_err_context(String::new)
0
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -242,7 +242,7 @@ fn next_char_info(uflag: bool, buf: &[u8], byte: usize) -> (CharType, usize, usi
(ctype, cwidth, nbytes) (ctype, cwidth, nbytes)
} }
fn unexpand(options: Options) { fn unexpand(options: Options) -> std::io::Result<()> {
let mut output = BufWriter::new(stdout()); let mut output = BufWriter::new(stdout());
let ts = &options.tabstops[..]; let ts = &options.tabstops[..];
let mut buf = Vec::new(); let mut buf = Vec::new();
@ -273,7 +273,7 @@ fn unexpand(options: Options) {
init, init,
true, true,
); );
crash_if_err!(1, output.write_all(&buf[byte..])); output.write_all(&buf[byte..])?;
scol = col; scol = col;
break; break;
} }
@ -293,7 +293,7 @@ fn unexpand(options: Options) {
}; };
if !tabs_buffered { if !tabs_buffered {
crash_if_err!(1, output.write_all(&buf[byte..byte + nbytes])); output.write_all(&buf[byte..byte + nbytes])?;
scol = col; // now printed up to this column scol = col; // now printed up to this column
} }
} }
@ -318,7 +318,7 @@ fn unexpand(options: Options) {
} else { } else {
0 0
}; };
crash_if_err!(1, output.write_all(&buf[byte..byte + nbytes])); output.write_all(&buf[byte..byte + nbytes])?;
scol = col; // we've now printed up to this column scol = col; // we've now printed up to this column
} }
} }
@ -337,9 +337,9 @@ fn unexpand(options: Options) {
init, init,
true, true,
); );
crash_if_err!(1, output.flush()); output.flush()?;
buf.truncate(0); // clear out the buffer buf.truncate(0); // clear out the buffer
} }
} }
crash_if_err!(1, output.flush()) output.flush()
} }