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

Merge pull request #2812 from jfinkels/paste-uresult

paste: return UResult from uumain() function
This commit is contained in:
Terts Diepraam 2021-12-29 15:09:12 +01:00 committed by GitHub
commit 36b4ab4e7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,14 +7,12 @@
// spell-checker:ignore (ToDO) delim // spell-checker:ignore (ToDO) delim
#[macro_use]
extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use std::fs::File; use std::fs::File;
use std::io::{stdin, BufRead, BufReader, Read}; use std::io::{stdin, BufRead, BufReader, Read};
use std::iter::repeat; use std::iter::repeat;
use std::path::Path; use std::path::Path;
use uucore::error::{FromIo, UResult};
static ABOUT: &str = "Write lines consisting of the sequentially corresponding lines from each static ABOUT: &str = "Write lines consisting of the sequentially corresponding lines from each
FILE, separated by TABs, to standard output."; FILE, separated by TABs, to standard output.";
@ -36,7 +34,8 @@ fn read_line<R: Read>(
} }
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(args); let matches = uu_app().get_matches_from(args);
let serial = matches.is_present(options::SERIAL); let serial = matches.is_present(options::SERIAL);
@ -46,9 +45,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.unwrap() .unwrap()
.map(|s| s.to_owned()) .map(|s| s.to_owned())
.collect(); .collect();
paste(files, serial, delimiters); paste(files, serial, delimiters)
0
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -78,18 +75,18 @@ pub fn uu_app() -> App<'static, 'static> {
) )
} }
fn paste(filenames: Vec<String>, serial: bool, delimiters: String) { fn paste(filenames: Vec<String>, serial: bool, delimiters: String) -> UResult<()> {
let mut files: Vec<_> = filenames let mut files = vec![];
.into_iter() for name in filenames {
.map(|name| { let file = if name == "-" {
if name == "-" { None
None } else {
} else { let path = Path::new(&name);
let r = crash_if_err!(1, File::open(Path::new(&name))); let r = File::open(path).map_err_context(String::new)?;
Some(BufReader::new(r)) Some(BufReader::new(r))
} };
}) files.push(file);
.collect(); }
let delimiters: Vec<String> = unescape(delimiters) let delimiters: Vec<String> = unescape(delimiters)
.chars() .chars()
@ -108,7 +105,7 @@ fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
output.push_str(line.trim_end()); output.push_str(line.trim_end());
output.push_str(&delimiters[delim_count % delimiters.len()]); output.push_str(&delimiters[delim_count % delimiters.len()]);
} }
Err(e) => crash!(1, "{}", e.to_string()), Err(e) => return Err(e.map_err_context(String::new)),
} }
delim_count += 1; delim_count += 1;
} }
@ -130,7 +127,7 @@ fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
eof_count += 1; eof_count += 1;
} }
Ok(_) => output.push_str(line.trim_end()), Ok(_) => output.push_str(line.trim_end()),
Err(e) => crash!(1, "{}", e.to_string()), Err(e) => return Err(e.map_err_context(String::new)),
} }
} }
output.push_str(&delimiters[delim_count % delimiters.len()]); output.push_str(&delimiters[delim_count % delimiters.len()]);
@ -143,6 +140,7 @@ fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
delim_count = 0; delim_count = 0;
} }
} }
Ok(())
} }
// Unescape all special characters // Unescape all special characters