From 0ed8b97a3f3eb8a0ee1cd992d7fa42e3db893517 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Wed, 17 Aug 2022 14:29:53 +0200 Subject: [PATCH 1/3] uucore: remove panic encoding handling We never want utilities to panic on invalid input and it is not currently in use, so it can be removed safely. --- src/uucore/src/lib/lib.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index d8860cfda..58893623d 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -153,7 +153,6 @@ pub fn execution_phrase() -> &'static str { pub enum InvalidEncodingHandling { Ignore, ConvertLossy, - Panic, } #[must_use] @@ -192,11 +191,9 @@ pub trait Args: Iterator + Sized { /// Converts each iterator item to a String and collects these into a vector /// On invalid encoding, the result will depend on the argument. This method allows to either drop entries with illegal encoding /// completely (```InvalidEncodingHandling::Ignore```), convert them using lossy-conversion (```InvalidEncodingHandling::ConvertLossy```) - /// which will result in strange strings or can chosen to panic (```InvalidEncodingHandling::Panic```). + /// which will result in strange strings or can chosen to panic. /// # Arguments /// * `handling` - This switch allows to switch the behavior, when invalid encoding is encountered - /// # Panics - /// * Occurs, when invalid encoding is encountered and handling is set to ```InvalidEncodingHandling::Panic``` fn collect_str(self, handling: InvalidEncodingHandling) -> ConversionResult { let mut full_conversion = true; let result_vector: Vec = self @@ -213,9 +210,6 @@ pub trait Args: Iterator + Sized { InvalidEncodingHandling::ConvertLossy => { Err(s_ret.to_string_lossy().into_owned()) } - InvalidEncodingHandling::Panic => { - panic!("Broken encoding found but caller cannot handle it") - } } } }) From 5621502a95e12b1542bf123730e7dc7cf1f23f19 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Wed, 17 Aug 2022 14:57:28 +0200 Subject: [PATCH 2/3] all: remove `accept_{any, complete, lossy}` and `ConversionResult` Outside of tests, only `accept_any` was used, meaning that this unnecessarily complicated the code. The behaviour of `accept_any` is now the default (and only) option. --- src/uu/base32/src/base_common.rs | 4 +- src/uu/basename/src/basename.rs | 4 +- src/uu/basenc/src/basenc.rs | 5 +- src/uu/cat/src/cat.rs | 4 +- src/uu/chmod/src/chmod.rs | 4 +- src/uu/chroot/src/chroot.rs | 4 +- src/uu/cksum/src/cksum.rs | 4 +- src/uu/comm/src/comm.rs | 4 +- src/uu/csplit/src/csplit.rs | 4 +- src/uu/cut/src/cut.rs | 4 +- src/uu/dd/src/dd.rs | 1 - src/uu/dircolors/src/dircolors.rs | 4 +- src/uu/dirname/src/dirname.rs | 4 +- src/uu/du/src/du.rs | 4 +- src/uu/echo/src/echo.rs | 4 +- src/uu/expand/src/expand.rs | 4 +- src/uu/expr/src/expr.rs | 4 +- src/uu/fold/src/fold.rs | 4 +- src/uu/kill/src/kill.rs | 4 +- src/uu/logname/src/logname.rs | 4 +- src/uu/mkdir/src/mkdir.rs | 4 +- src/uu/mkfifo/src/mkfifo.rs | 4 +- src/uu/mknod/src/mknod.rs | 4 +- src/uu/mktemp/src/mktemp.rs | 4 +- src/uu/nl/src/nl.rs | 4 +- src/uu/nohup/src/nohup.rs | 4 +- src/uu/numfmt/src/numfmt.rs | 4 +- src/uu/od/src/od.rs | 4 +- src/uu/pathchk/src/pathchk.rs | 4 +- src/uu/pinky/src/pinky.rs | 4 +- src/uu/pr/src/pr.rs | 4 +- src/uu/printf/src/printf.rs | 4 +- src/uu/ptx/src/ptx.rs | 4 +- src/uu/relpath/src/relpath.rs | 4 +- src/uu/shred/src/shred.rs | 4 +- src/uu/shuf/src/shuf.rs | 4 +- src/uu/sort/src/sort.rs | 4 +- src/uu/stdbuf/src/stdbuf.rs | 4 +- src/uu/sum/src/sum.rs | 4 +- src/uu/tac/src/tac.rs | 4 +- src/uu/timeout/src/timeout.rs | 4 +- src/uu/tr/src/tr.rs | 4 +- src/uu/tsort/src/tsort.rs | 4 +- src/uu/tty/src/tty.rs | 4 +- src/uu/unexpand/src/unexpand.rs | 4 +- src/uu/who/src/who.rs | 4 +- src/uucore/src/lib/lib.rs | 105 ++++++++---------------------- tests/common/util.rs | 3 +- 48 files changed, 74 insertions(+), 216 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index d49f02bc8..41a9fcc6a 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -87,9 +87,7 @@ impl Config { pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult { let command = base_app(about, usage); - let arg_list = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let arg_list = args.collect_str(InvalidEncodingHandling::ConvertLossy); Config::from(&command.try_get_matches_from(arg_list)?) } diff --git a/src/uu/basename/src/basename.rs b/src/uu/basename/src/basename.rs index b52717287..461dabb66 100644 --- a/src/uu/basename/src/basename.rs +++ b/src/uu/basename/src/basename.rs @@ -28,9 +28,7 @@ pub mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); // Since options have to go before names, // if the first argument is not an option, then there is no option, diff --git a/src/uu/basenc/src/basenc.rs b/src/uu/basenc/src/basenc.rs index 1ecacf936..9a4f19f2b 100644 --- a/src/uu/basenc/src/basenc.rs +++ b/src/uu/basenc/src/basenc.rs @@ -52,10 +52,7 @@ pub fn uu_app<'a>() -> Command<'a> { fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> { let matches = uu_app() - .try_get_matches_from( - args.collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(), - ) + .try_get_matches_from(args.collect_str(InvalidEncodingHandling::ConvertLossy)) .with_exit_code(1)?; let format = ENCODINGS .iter() diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 62afc0aa3..584e1d735 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -184,9 +184,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().try_get_matches_from(args)?; diff --git a/src/uu/chmod/src/chmod.rs b/src/uu/chmod/src/chmod.rs index d1a051a1c..458f788b2 100644 --- a/src/uu/chmod/src/chmod.rs +++ b/src/uu/chmod/src/chmod.rs @@ -46,9 +46,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let mut args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let mut args = args.collect_str(InvalidEncodingHandling::ConvertLossy); // Before we can parse 'args' with clap (and previously getopts), // a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE"). diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 6f575f4b1..79f679561 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -33,9 +33,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index fd9796769..62d658969 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -114,9 +114,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index d553a87ca..5ba9b434f 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -132,9 +132,7 @@ fn open_file(name: &str) -> io::Result { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); let filename1 = matches.value_of(options::FILE_1).unwrap(); diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index fc60ec1ca..4f94020b6 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -713,9 +713,7 @@ mod tests { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/cut/src/cut.rs b/src/uu/cut/src/cut.rs index 2d76648d0..f9319d0e8 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -398,9 +398,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let delimiter_is_equal = args.contains(&"-d=".to_string()); // special case let matches = uu_app().get_matches_from(args); diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index 94da4b7d3..d60ad1e3c 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -707,7 +707,6 @@ fn append_dashes_if_not_present(mut acc: Vec, mut s: String) -> Vec UResult<()> { let dashed_args = args .collect_str(InvalidEncodingHandling::Ignore) - .accept_any() .into_iter() .fold(Vec::new(), append_dashes_if_not_present); diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 5a014eafe..04a9f6dd8 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -65,9 +65,7 @@ pub fn guess_syntax() -> OutputFmt { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(&args); diff --git a/src/uu/dirname/src/dirname.rs b/src/uu/dirname/src/dirname.rs index 5b7fa3c56..670a4a589 100644 --- a/src/uu/dirname/src/dirname.rs +++ b/src/uu/dirname/src/dirname.rs @@ -28,9 +28,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let after_help = get_long_usage(); diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 22d229f83..649edcd8d 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -516,9 +516,7 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult> { #[uucore::main] #[allow(clippy::cognitive_complexity)] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index f02f938f2..3904d6225 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -110,9 +110,7 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); let no_newline = matches.contains_id(options::NO_NEWLINE); diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 06d903679..f1ece7d0c 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -269,9 +269,7 @@ fn expand_shortcuts(args: &[String]) -> Vec { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(expand_shortcuts(&args)); diff --git a/src/uu/expr/src/expr.rs b/src/uu/expr/src/expr.rs index 104398241..cf4e27d01 100644 --- a/src/uu/expr/src/expr.rs +++ b/src/uu/expr/src/expr.rs @@ -33,9 +33,7 @@ pub fn uu_app<'a>() -> Command<'a> { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); // For expr utility we do not want getopts. // The following usage should work without escaping hyphens: `expr -15 = 1 + 2 \* \( 3 - -4 \)` diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index 6484a56f9..b923e6fa7 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -31,9 +31,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let (args, obs_width) = handle_obsolete(&args[..]); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index 3c8f62d59..f7548dbed 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -38,9 +38,7 @@ pub enum Mode { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let mut args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let mut args = args.collect_str(InvalidEncodingHandling::Ignore); let obs_signal = handle_obsolete(&mut args); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/logname/src/logname.rs b/src/uu/logname/src/logname.rs index 29a74a373..5fe2181af 100644 --- a/src/uu/logname/src/logname.rs +++ b/src/uu/logname/src/logname.rs @@ -37,9 +37,7 @@ static ABOUT: &str = "Print user's login name"; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let _ = uu_app().get_matches_from(args); diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index 8a6d2e9dd..b6a13f2fc 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -83,9 +83,7 @@ fn strip_minus_from_mode(args: &mut Vec) -> bool { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let mut args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let mut args = args.collect_str(InvalidEncodingHandling::ConvertLossy); // Before we can parse 'args' with clap (and previously getopts), // a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE"). diff --git a/src/uu/mkfifo/src/mkfifo.rs b/src/uu/mkfifo/src/mkfifo.rs index 3530b5c9e..2e8d0c7b1 100644 --- a/src/uu/mkfifo/src/mkfifo.rs +++ b/src/uu/mkfifo/src/mkfifo.rs @@ -28,9 +28,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/mknod/src/mknod.rs b/src/uu/mknod/src/mknod.rs index cda500f65..1b6f8c5c5 100644 --- a/src/uu/mknod/src/mknod.rs +++ b/src/uu/mknod/src/mknod.rs @@ -81,9 +81,7 @@ fn _mknod(file_name: &str, mode: mode_t, dev: dev_t) -> i32 { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); // Linux-specific options, not implemented // opts.optflag("Z", "", "set the SELinux security context to default type"); // opts.optopt("", "context", "like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX"); diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 561281c72..ab0970f15 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -11,7 +11,7 @@ use clap::{crate_version, Arg, ArgMatches, Command}; use uucore::display::{println_verbatim, Quotable}; use uucore::error::{FromIo, UError, UResult}; -use uucore::format_usage; +use uucore::{format_usage, InvalidEncodingHandling}; use std::env; use std::error::Error; @@ -316,7 +316,7 @@ impl Params { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str_lossy().accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().try_get_matches_from(&args)?; diff --git a/src/uu/nl/src/nl.rs b/src/uu/nl/src/nl.rs index 7cea6f80f..b9311adba 100644 --- a/src/uu/nl/src/nl.rs +++ b/src/uu/nl/src/nl.rs @@ -84,9 +84,7 @@ pub mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/nohup/src/nohup.rs b/src/uu/nohup/src/nohup.rs index 600dff388..2e4a45ec5 100644 --- a/src/uu/nohup/src/nohup.rs +++ b/src/uu/nohup/src/nohup.rs @@ -86,9 +86,7 @@ impl Display for NohupError { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index d259d001d..401b3348e 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -261,9 +261,7 @@ fn concat_format_arg_and_value(args: &[String]) -> Vec { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(concat_format_arg_and_value(&args)); diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index bdbe78fc7..0666e9752 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -256,9 +256,7 @@ impl OdOptions { /// opens the input and calls `odfunc` to process the input. #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let clap_opts = uu_app(); diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index 53689e4ff..b8fe0c485 100644 --- a/src/uu/pathchk/src/pathchk.rs +++ b/src/uu/pathchk/src/pathchk.rs @@ -39,9 +39,7 @@ const POSIX_NAME_MAX: usize = 14; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index 6235566b1..e9fd28bd4 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -49,9 +49,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let after_help = get_long_usage(); diff --git a/src/uu/pr/src/pr.rs b/src/uu/pr/src/pr.rs index e062b6c7f..8112555f5 100644 --- a/src/uu/pr/src/pr.rs +++ b/src/uu/pr/src/pr.rs @@ -378,9 +378,7 @@ pub fn uu_app<'a>() -> Command<'a> { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(uucore::InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(uucore::InvalidEncodingHandling::Ignore); let opt_args = recreate_arguments(&args); diff --git a/src/uu/printf/src/printf.rs b/src/uu/printf/src/printf.rs index b0c3e0b50..b1b6d146e 100644 --- a/src/uu/printf/src/printf.rs +++ b/src/uu/printf/src/printf.rs @@ -271,9 +271,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); let format_string = matches diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index cb593c356..8873607f4 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -722,9 +722,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); // let mut opts = Options::new(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/relpath/src/relpath.rs b/src/uu/relpath/src/relpath.rs index 1fd0f0680..aa54c5716 100644 --- a/src/uu/relpath/src/relpath.rs +++ b/src/uu/relpath/src/relpath.rs @@ -27,9 +27,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index e0ab886bb..7b08a09b1 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -266,9 +266,7 @@ pub mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index 418924ec7..e4b1516ca 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -56,9 +56,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 5b2cbc714..d3ba90489 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -1055,9 +1055,7 @@ fn make_sort_mode_arg<'a>(mode: &'a str, short: char, help: &'a str) -> Arg<'a> #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let mut settings: GlobalSettings = Default::default(); let matches = match uu_app().try_get_matches_from(args) { diff --git a/src/uu/stdbuf/src/stdbuf.rs b/src/uu/stdbuf/src/stdbuf.rs index 49654c39a..72f923606 100644 --- a/src/uu/stdbuf/src/stdbuf.rs +++ b/src/uu/stdbuf/src/stdbuf.rs @@ -156,9 +156,7 @@ fn get_preload_env(tmp_dir: &mut TempDir) -> io::Result<(String, PathBuf)> { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/sum/src/sum.rs b/src/uu/sum/src/sum.rs index 01758d90a..abd536900 100644 --- a/src/uu/sum/src/sum.rs +++ b/src/uu/sum/src/sum.rs @@ -109,9 +109,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index f6baed971..b9786123b 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -37,9 +37,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index 187294ba3..6c42b17eb 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -106,9 +106,7 @@ impl Config { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let command = uu_app(); diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 3208a7ee1..825188383 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -40,9 +40,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let after_help = get_long_usage(); diff --git a/src/uu/tsort/src/tsort.rs b/src/uu/tsort/src/tsort.rs index 056c2be8d..47fb05c83 100644 --- a/src/uu/tsort/src/tsort.rs +++ b/src/uu/tsort/src/tsort.rs @@ -25,9 +25,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/tty/src/tty.rs b/src/uu/tty/src/tty.rs index 953d6d954..4b492c50f 100644 --- a/src/uu/tty/src/tty.rs +++ b/src/uu/tty/src/tty.rs @@ -24,9 +24,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::ConvertLossy) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index 1b67785a2..f333f9c18 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -165,9 +165,7 @@ fn expand_shortcuts(args: &[String]) -> Vec { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let matches = uu_app().get_matches_from(expand_shortcuts(&args)); diff --git a/src/uu/who/src/who.rs b/src/uu/who/src/who.rs index a01cb0dda..76b01f850 100644 --- a/src/uu/who/src/who.rs +++ b/src/uu/who/src/who.rs @@ -56,9 +56,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + let args = args.collect_str(InvalidEncodingHandling::Ignore); let after_help = get_long_usage(); diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 58893623d..c6c3200ef 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -155,38 +155,6 @@ pub enum InvalidEncodingHandling { ConvertLossy, } -#[must_use] -pub enum ConversionResult { - Complete(Vec), - Lossy(Vec), -} - -impl ConversionResult { - pub fn accept_any(self) -> Vec { - match self { - Self::Complete(result) | Self::Lossy(result) => result, - } - } - - pub fn expect_lossy(self, msg: &str) -> Vec { - match self { - Self::Lossy(result) => result, - Self::Complete(_) => { - panic!("{}", msg); - } - } - } - - pub fn expect_complete(self, msg: &str) -> Vec { - match self { - Self::Complete(result) => result, - Self::Lossy(_) => { - panic!("{}", msg); - } - } - } -} - pub trait Args: Iterator + Sized { /// Converts each iterator item to a String and collects these into a vector /// On invalid encoding, the result will depend on the argument. This method allows to either drop entries with illegal encoding @@ -194,45 +162,31 @@ pub trait Args: Iterator + Sized { /// which will result in strange strings or can chosen to panic. /// # Arguments /// * `handling` - This switch allows to switch the behavior, when invalid encoding is encountered - fn collect_str(self, handling: InvalidEncodingHandling) -> ConversionResult { - let mut full_conversion = true; - let result_vector: Vec = self - .map(|s| match s.into_string() { - Ok(string) => Ok(string), - Err(s_ret) => { - full_conversion = false; - eprintln!( - "Input with broken encoding occurred! (s = {}) ", - s_ret.quote() - ); - match handling { - InvalidEncodingHandling::Ignore => Err(String::new()), - InvalidEncodingHandling::ConvertLossy => { - Err(s_ret.to_string_lossy().into_owned()) - } + fn collect_str(self, handling: InvalidEncodingHandling) -> Vec { + self.map(|s| match s.into_string() { + Ok(string) => Ok(string), + Err(s_ret) => { + eprintln!( + "Input with broken encoding occurred! (s = {}) ", + s_ret.quote() + ); + match handling { + InvalidEncodingHandling::Ignore => Err(String::new()), + InvalidEncodingHandling::ConvertLossy => { + Err(s_ret.to_string_lossy().into_owned()) } } - }) - .filter(|s| match handling { - InvalidEncodingHandling::Ignore => s.is_ok(), - _ => true, - }) - .map(|s| match s { - Ok(v) => v, - Err(e) => e, - }) - .collect(); - - if full_conversion { - ConversionResult::Complete(result_vector) - } else { - ConversionResult::Lossy(result_vector) - } - } - - /// convenience function for a more slim interface - fn collect_str_lossy(self) -> ConversionResult { - self.collect_str(InvalidEncodingHandling::ConvertLossy) + } + }) + .filter(|s| match handling { + InvalidEncodingHandling::Ignore => s.is_ok(), + _ => true, + }) + .map(|s| match s { + Ok(v) => v, + Err(e) => e, + }) + .collect() } } @@ -255,7 +209,7 @@ mod tests { ] } - fn collect_os_str(vec: Vec, handling: InvalidEncodingHandling) -> ConversionResult { + fn collect_os_str(vec: Vec, handling: InvalidEncodingHandling) -> Vec { vec.into_iter().collect_str(handling) } @@ -265,8 +219,7 @@ mod tests { assert!(os_str.to_os_string().into_string().is_err()); let test_vec = make_os_vec(os_str); let collected_to_str = - collect_os_str(test_vec.clone(), InvalidEncodingHandling::ConvertLossy) - .expect_lossy("Lossy conversion expected in this test: bad encoding entries should be converted as good as possible"); + collect_os_str(test_vec.clone(), InvalidEncodingHandling::ConvertLossy); //conservation of length - when accepting lossy conversion no arguments may be dropped assert_eq!(collected_to_str.len(), test_vec.len()); //first indices identical @@ -288,10 +241,7 @@ mod tests { //assert our string is invalid utf8 assert!(os_str.to_os_string().into_string().is_err()); let test_vec = make_os_vec(os_str); - let collected_to_str = collect_os_str(test_vec.clone(), InvalidEncodingHandling::Ignore) - .expect_lossy( - "Lossy conversion expected in this test: bad encoding entries should be filtered", - ); + let collected_to_str = collect_os_str(test_vec.clone(), InvalidEncodingHandling::Ignore); //assert that the broken entry is filtered out assert_eq!(collected_to_str.len(), test_vec.len() - 1); //assert that the unbroken indices are converted as expected @@ -308,8 +258,7 @@ mod tests { //create a vector containing only correct encoding let test_vec = make_os_vec(&OsString::from("test2")); //expect complete conversion without losses, even when lossy conversion is accepted - let _ = collect_os_str(test_vec, InvalidEncodingHandling::ConvertLossy) - .expect_complete("Lossy conversion not expected in this test"); + let _ = collect_os_str(test_vec, InvalidEncodingHandling::ConvertLossy); } #[cfg(any(unix, target_os = "redox"))] diff --git a/tests/common/util.rs b/tests/common/util.rs index 93c7e3d12..03876a570 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -1021,8 +1021,7 @@ impl UCommand { let strings = args .iter() .map(|s| s.as_ref().to_os_string()) - .collect_str(InvalidEncodingHandling::Ignore) - .accept_any(); + .collect_str(InvalidEncodingHandling::Ignore); for s in strings { self.comm_string.push(' '); From ba713b6365b4787af88211d36031f39c861428f2 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Wed, 17 Aug 2022 15:20:20 +0200 Subject: [PATCH 3/3] Simplify invalid encoding handling into two small methods of Args The previous encoding handling was unnecessarily complex. This commit removes the enum that specifies the handling and instead has two separate methods to collect the strings either with lossy conversion or by ignoring invalidly encoded strings. --- src/uu/base32/src/base_common.rs | 4 +- src/uu/basename/src/basename.rs | 4 +- src/uu/basenc/src/basenc.rs | 3 +- src/uu/cat/src/cat.rs | 4 +- src/uu/chmod/src/chmod.rs | 4 +- src/uu/chroot/src/chroot.rs | 4 +- src/uu/cksum/src/cksum.rs | 3 +- src/uu/comm/src/comm.rs | 4 +- src/uu/csplit/src/csplit.rs | 4 +- src/uu/cut/src/cut.rs | 4 +- src/uu/dd/src/dd.rs | 4 +- src/uu/dircolors/src/dircolors.rs | 4 +- src/uu/dirname/src/dirname.rs | 4 +- src/uu/du/src/du.rs | 3 +- src/uu/echo/src/echo.rs | 4 +- src/uu/expand/src/expand.rs | 4 +- src/uu/expr/src/expr.rs | 3 +- src/uu/fold/src/fold.rs | 4 +- src/uu/kill/src/kill.rs | 4 +- src/uu/logname/src/logname.rs | 3 +- src/uu/mkdir/src/mkdir.rs | 4 +- src/uu/mkfifo/src/mkfifo.rs | 4 +- src/uu/mknod/src/mknod.rs | 4 +- src/uu/mktemp/src/mktemp.rs | 4 +- src/uu/nl/src/nl.rs | 4 +- src/uu/nohup/src/nohup.rs | 4 +- src/uu/numfmt/src/numfmt.rs | 4 +- src/uu/od/src/od.rs | 3 +- src/uu/pathchk/src/pathchk.rs | 4 +- src/uu/pinky/src/pinky.rs | 4 +- src/uu/pr/src/pr.rs | 2 +- src/uu/printf/src/printf.rs | 3 +- src/uu/ptx/src/ptx.rs | 4 +- src/uu/relpath/src/relpath.rs | 4 +- src/uu/shred/src/shred.rs | 4 +- src/uu/shuf/src/shuf.rs | 4 +- src/uu/sort/src/sort.rs | 4 +- src/uu/stdbuf/src/stdbuf.rs | 4 +- src/uu/sum/src/sum.rs | 4 +- src/uu/tac/src/tac.rs | 3 +- src/uu/timeout/src/timeout.rs | 4 +- src/uu/tr/src/tr.rs | 4 +- src/uu/tsort/src/tsort.rs | 4 +- src/uu/tty/src/tty.rs | 4 +- src/uu/unexpand/src/unexpand.rs | 4 +- src/uu/who/src/who.rs | 4 +- src/uucore/src/lib/lib.rs | 82 +++++++++---------------------- tests/common/util.rs | 4 +- 48 files changed, 107 insertions(+), 153 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 41a9fcc6a..b535260c5 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -12,7 +12,7 @@ use std::io::{stdout, Read, Write}; use uucore::display::Quotable; use uucore::encoding::{wrap_print, Data, Format}; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; use std::fs::File; use std::io::{BufReader, Stdin}; @@ -87,7 +87,7 @@ impl Config { pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult { let command = base_app(about, usage); - let arg_list = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let arg_list = args.collect_lossy(); Config::from(&command.try_get_matches_from(arg_list)?) } diff --git a/src/uu/basename/src/basename.rs b/src/uu/basename/src/basename.rs index 461dabb66..af961f5fb 100644 --- a/src/uu/basename/src/basename.rs +++ b/src/uu/basename/src/basename.rs @@ -11,7 +11,7 @@ use clap::{crate_version, Arg, Command}; use std::path::{is_separator, PathBuf}; use uucore::display::Quotable; use uucore::error::{UResult, UUsageError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = r#"Print NAME with any leading directory components removed If specified, also remove a trailing SUFFIX"#; @@ -28,7 +28,7 @@ pub mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); // Since options have to go before names, // if the first argument is not an option, then there is no option, diff --git a/src/uu/basenc/src/basenc.rs b/src/uu/basenc/src/basenc.rs index 9a4f19f2b..d884c04e3 100644 --- a/src/uu/basenc/src/basenc.rs +++ b/src/uu/basenc/src/basenc.rs @@ -14,7 +14,6 @@ use uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR}; use uucore::{ encoding::Format, error::{UResult, UUsageError}, - InvalidEncodingHandling, }; use std::io::{stdin, Read}; @@ -52,7 +51,7 @@ pub fn uu_app<'a>() -> Command<'a> { fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> { let matches = uu_app() - .try_get_matches_from(args.collect_str(InvalidEncodingHandling::ConvertLossy)) + .try_get_matches_from(args.collect_lossy()) .with_exit_code(1)?; let format = ENCODINGS .iter() diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 584e1d735..602a340cb 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -36,7 +36,7 @@ use std::net::Shutdown; use std::os::unix::fs::FileTypeExt; #[cfg(unix)] use unix_socket::UnixStream; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static NAME: &str = "cat"; static USAGE: &str = "{} [OPTION]... [FILE]..."; @@ -184,7 +184,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().try_get_matches_from(args)?; diff --git a/src/uu/chmod/src/chmod.rs b/src/uu/chmod/src/chmod.rs index 458f788b2..181517fea 100644 --- a/src/uu/chmod/src/chmod.rs +++ b/src/uu/chmod/src/chmod.rs @@ -18,7 +18,7 @@ use uucore::fs::is_symlink; use uucore::libc::mode_t; #[cfg(not(windows))] use uucore::mode; -use uucore::{format_usage, show_error, InvalidEncodingHandling}; +use uucore::{format_usage, show_error}; static ABOUT: &str = "Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE."; @@ -46,7 +46,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let mut args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let mut args = args.collect_lossy(); // Before we can parse 'args' with clap (and previously getopts), // a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE"). diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 79f679561..1e5f62d5f 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -17,7 +17,7 @@ use std::path::Path; use std::process; use uucore::error::{set_exit_code, UResult}; use uucore::libc::{self, chroot, setgid, setgroups, setuid}; -use uucore::{entries, format_usage, InvalidEncodingHandling}; +use uucore::{entries, format_usage}; static ABOUT: &str = "Run COMMAND with root directory set to NEWROOT."; static USAGE: &str = "{} [OPTION]... NEWROOT [COMMAND [ARG]...]"; @@ -33,7 +33,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 62d658969..2d8c930fe 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -12,7 +12,6 @@ use std::io::{self, stdin, BufReader, Read}; use std::path::Path; use uucore::display::Quotable; use uucore::error::{FromIo, UResult}; -use uucore::InvalidEncodingHandling; use uucore::{format_usage, show}; // NOTE: CRC_TABLE_LEN *must* be <= 256 as we cast 0..CRC_TABLE_LEN to u8 @@ -114,7 +113,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index 5ba9b434f..5e08613cd 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -13,7 +13,7 @@ use std::io::{self, stdin, BufRead, BufReader, Stdin}; use std::path::Path; use uucore::error::FromIo; use uucore::error::UResult; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; use clap::{crate_version, Arg, ArgMatches, Command}; @@ -132,7 +132,7 @@ fn open_file(name: &str) -> io::Result { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); let filename1 = matches.value_of(options::FILE_1).unwrap(); diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 4f94020b6..143d2d2e8 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -16,7 +16,7 @@ use clap::{crate_version, Arg, ArgMatches, Command}; use regex::Regex; use uucore::display::Quotable; use uucore::error::{FromIo, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; mod csplit_error; mod patterns; @@ -713,7 +713,7 @@ mod tests { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/cut/src/cut.rs b/src/uu/cut/src/cut.rs index f9319d0e8..a1883adc5 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -19,8 +19,8 @@ use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; use self::searcher::Searcher; +use uucore::format_usage; use uucore::ranges::Range; -use uucore::{format_usage, InvalidEncodingHandling}; mod searcher; @@ -398,7 +398,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let delimiter_is_equal = args.contains(&"-d=".to_string()); // special case let matches = uu_app().get_matches_from(args); diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index d60ad1e3c..0f5948710 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -37,7 +37,7 @@ use clap::{crate_version, Arg, ArgMatches, Command}; use gcd::Gcd; use uucore::display::Quotable; use uucore::error::{FromIo, UResult}; -use uucore::{show_error, InvalidEncodingHandling}; +use uucore::show_error; const ABOUT: &str = "copy, and optionally convert, a file system resource"; const BUF_INIT_BYTE: u8 = 0xDD; @@ -706,7 +706,7 @@ fn append_dashes_if_not_present(mut acc: Vec, mut s: String) -> Vec UResult<()> { let dashed_args = args - .collect_str(InvalidEncodingHandling::Ignore) + .collect_ignore() .into_iter() .fold(Vec::new(), append_dashes_if_not_present); diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 04a9f6dd8..3224f07e1 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -65,7 +65,7 @@ pub fn guess_syntax() -> OutputFmt { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(&args); @@ -276,7 +276,7 @@ enum ParseState { } use std::collections::HashMap; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; fn parse(lines: T, fmt: &OutputFmt, fp: &str) -> Result where diff --git a/src/uu/dirname/src/dirname.rs b/src/uu/dirname/src/dirname.rs index 670a4a589..ed3deb7aa 100644 --- a/src/uu/dirname/src/dirname.rs +++ b/src/uu/dirname/src/dirname.rs @@ -9,7 +9,7 @@ use clap::{crate_version, Arg, Command}; use std::path::Path; use uucore::display::print_verbatim; use uucore::error::{UResult, UUsageError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "strip last component from file name"; const USAGE: &str = "{} [OPTION] NAME..."; @@ -28,7 +28,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let after_help = get_long_usage(); diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 649edcd8d..41a532ffd 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -39,7 +39,6 @@ use uucore::error::{UError, UResult}; use uucore::format_usage; use uucore::parse_glob; use uucore::parse_size::{parse_size, ParseSizeError}; -use uucore::InvalidEncodingHandling; #[cfg(windows)] use winapi::shared::minwindef::{DWORD, LPVOID}; #[cfg(windows)] @@ -516,7 +515,7 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult> { #[uucore::main] #[allow(clippy::cognitive_complexity)] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 3904d6225..6f4d2e674 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -11,7 +11,7 @@ use std::io::{self, Write}; use std::iter::Peekable; use std::str::Chars; use uucore::error::{FromIo, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; const NAME: &str = "echo"; const ABOUT: &str = "display a line of text"; @@ -110,7 +110,7 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); let no_newline = matches.contains_id(options::NO_NEWLINE); diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index f1ece7d0c..bbbe2de71 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -22,7 +22,7 @@ use std::str::from_utf8; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "Convert tabs in each FILE to spaces, writing to standard output. With no FILE, or when FILE is -, read standard input."; @@ -269,7 +269,7 @@ fn expand_shortcuts(args: &[String]) -> Vec { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(expand_shortcuts(&args)); diff --git a/src/uu/expr/src/expr.rs b/src/uu/expr/src/expr.rs index cf4e27d01..9b4ec0b19 100644 --- a/src/uu/expr/src/expr.rs +++ b/src/uu/expr/src/expr.rs @@ -7,7 +7,6 @@ use clap::{crate_version, Arg, Command}; use uucore::error::{UResult, USimpleError}; -use uucore::InvalidEncodingHandling; mod syntax_tree; mod tokens; @@ -33,7 +32,7 @@ pub fn uu_app<'a>() -> Command<'a> { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); // For expr utility we do not want getopts. // The following usage should work without escaping hyphens: `expr -15 = 1 + 2 \* \( 3 - -4 \)` diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index b923e6fa7..6587d5eae 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -13,7 +13,7 @@ use std::io::{stdin, BufRead, BufReader, Read}; use std::path::Path; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; const TAB_WIDTH: usize = 8; @@ -31,7 +31,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let (args, obs_width) = handle_obsolete(&args[..]); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index f7548dbed..abb72799d 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -16,8 +16,8 @@ use nix::unistd::Pid; use std::io::Error; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult, USimpleError}; +use uucore::format_usage; use uucore::signals::{signal_by_name_or_value, ALL_SIGNALS}; -use uucore::{format_usage, InvalidEncodingHandling}; static ABOUT: &str = "Send signal to processes or list information about signals."; const USAGE: &str = "{} [OPTIONS]... PID..."; @@ -38,7 +38,7 @@ pub enum Mode { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let mut args = args.collect_str(InvalidEncodingHandling::Ignore); + let mut args = args.collect_ignore(); let obs_signal = handle_obsolete(&mut args); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/logname/src/logname.rs b/src/uu/logname/src/logname.rs index 5fe2181af..112923296 100644 --- a/src/uu/logname/src/logname.rs +++ b/src/uu/logname/src/logname.rs @@ -15,7 +15,6 @@ extern crate uucore; use clap::{crate_version, Command}; use std::ffi::CStr; use uucore::error::UResult; -use uucore::InvalidEncodingHandling; extern "C" { // POSIX requires using getlogin (or equivalent code) @@ -37,7 +36,7 @@ static ABOUT: &str = "Print user's login name"; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let _ = uu_app().get_matches_from(args); diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index b6a13f2fc..3f7cbf1b9 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -15,10 +15,10 @@ use std::path::{Path, PathBuf}; #[cfg(not(windows))] use uucore::error::FromIo; use uucore::error::{UResult, USimpleError}; +use uucore::format_usage; #[cfg(not(windows))] use uucore::mode; use uucore::{display::Quotable, fs::dir_strip_dot_for_creation}; -use uucore::{format_usage, InvalidEncodingHandling}; static DEFAULT_PERM: u32 = 0o755; @@ -83,7 +83,7 @@ fn strip_minus_from_mode(args: &mut Vec) -> bool { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let mut args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let mut args = args.collect_lossy(); // Before we can parse 'args' with clap (and previously getopts), // a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE"). diff --git a/src/uu/mkfifo/src/mkfifo.rs b/src/uu/mkfifo/src/mkfifo.rs index 2e8d0c7b1..1c70f52c6 100644 --- a/src/uu/mkfifo/src/mkfifo.rs +++ b/src/uu/mkfifo/src/mkfifo.rs @@ -11,9 +11,9 @@ extern crate uucore; use clap::{crate_version, Arg, Command}; use libc::mkfifo; use std::ffi::CString; +use uucore::display::Quotable; use uucore::error::{UResult, USimpleError}; use uucore::format_usage; -use uucore::{display::Quotable, InvalidEncodingHandling}; static NAME: &str = "mkfifo"; static USAGE: &str = "{} [OPTION]... NAME..."; @@ -28,7 +28,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/mknod/src/mknod.rs b/src/uu/mknod/src/mknod.rs index 1b6f8c5c5..551d1e90d 100644 --- a/src/uu/mknod/src/mknod.rs +++ b/src/uu/mknod/src/mknod.rs @@ -15,7 +15,7 @@ use libc::{S_IFBLK, S_IFCHR, S_IFIFO, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOT use uucore::display::Quotable; use uucore::error::{set_exit_code, UResult, USimpleError, UUsageError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "Create the special file NAME of the given TYPE."; static USAGE: &str = "{} [OPTION]... NAME TYPE [MAJOR MINOR]"; @@ -81,7 +81,7 @@ fn _mknod(file_name: &str, mode: mode_t, dev: dev_t) -> i32 { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); // Linux-specific options, not implemented // opts.optflag("Z", "", "set the SELinux security context to default type"); // opts.optopt("", "context", "like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX"); diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index ab0970f15..1131e0f01 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -11,7 +11,7 @@ use clap::{crate_version, Arg, ArgMatches, Command}; use uucore::display::{println_verbatim, Quotable}; use uucore::error::{FromIo, UError, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; use std::env; use std::error::Error; @@ -316,7 +316,7 @@ impl Params { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().try_get_matches_from(&args)?; diff --git a/src/uu/nl/src/nl.rs b/src/uu/nl/src/nl.rs index b9311adba..7f36cb424 100644 --- a/src/uu/nl/src/nl.rs +++ b/src/uu/nl/src/nl.rs @@ -14,7 +14,7 @@ use std::io::{stdin, BufRead, BufReader, Read}; use std::iter::repeat; use std::path::Path; use uucore::error::{FromIo, UResult, USimpleError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; mod helper; @@ -84,7 +84,7 @@ pub mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/nohup/src/nohup.rs b/src/uu/nohup/src/nohup.rs index 2e4a45ec5..6b11ea920 100644 --- a/src/uu/nohup/src/nohup.rs +++ b/src/uu/nohup/src/nohup.rs @@ -22,7 +22,7 @@ use std::os::unix::prelude::*; use std::path::{Path, PathBuf}; use uucore::display::Quotable; use uucore::error::{set_exit_code, UError, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "Run COMMAND ignoring hangup signals."; static LONG_HELP: &str = " @@ -86,7 +86,7 @@ impl Display for NohupError { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index 401b3348e..4139b6d31 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -16,8 +16,8 @@ use std::io::{BufRead, Write}; use units::{IEC_BASES, SI_BASES}; use uucore::display::Quotable; use uucore::error::UResult; +use uucore::format_usage; use uucore::ranges::Range; -use uucore::{format_usage, InvalidEncodingHandling}; pub mod errors; pub mod format; @@ -261,7 +261,7 @@ fn concat_format_arg_and_value(args: &[String]) -> Vec { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(concat_format_arg_and_value(&args)); diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 0666e9752..81068f0b7 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -48,7 +48,6 @@ use uucore::display::Quotable; use uucore::error::{UResult, USimpleError}; use uucore::format_usage; use uucore::parse_size::ParseSizeError; -use uucore::InvalidEncodingHandling; const PEEK_BUFFER_SIZE: usize = 4; // utf-8 can be 4 bytes static ABOUT: &str = "dump files in octal and other formats"; @@ -256,7 +255,7 @@ impl OdOptions { /// opens the input and calls `odfunc` to process the input. #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let clap_opts = uu_app(); diff --git a/src/uu/pathchk/src/pathchk.rs b/src/uu/pathchk/src/pathchk.rs index b8fe0c485..fb4d44494 100644 --- a/src/uu/pathchk/src/pathchk.rs +++ b/src/uu/pathchk/src/pathchk.rs @@ -13,7 +13,7 @@ use std::fs; use std::io::{ErrorKind, Write}; use uucore::display::Quotable; use uucore::error::{set_exit_code, UResult, UUsageError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; // operating mode enum Mode { @@ -39,7 +39,7 @@ const POSIX_NAME_MAX: usize = 14; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index e9fd28bd4..039dd0a5e 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -20,7 +20,7 @@ use std::os::unix::fs::MetadataExt; use clap::{crate_version, Arg, Command}; use std::path::PathBuf; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "lightweight finger"; const USAGE: &str = "{} [OPTION]... [USER]..."; @@ -49,7 +49,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let after_help = get_long_usage(); diff --git a/src/uu/pr/src/pr.rs b/src/uu/pr/src/pr.rs index 8112555f5..a27804fa6 100644 --- a/src/uu/pr/src/pr.rs +++ b/src/uu/pr/src/pr.rs @@ -378,7 +378,7 @@ pub fn uu_app<'a>() -> Command<'a> { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(uucore::InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let opt_args = recreate_arguments(&args); diff --git a/src/uu/printf/src/printf.rs b/src/uu/printf/src/printf.rs index b1b6d146e..e95e10cc8 100644 --- a/src/uu/printf/src/printf.rs +++ b/src/uu/printf/src/printf.rs @@ -4,7 +4,6 @@ use clap::{crate_version, Arg, Command}; use uucore::error::{UResult, UUsageError}; -use uucore::InvalidEncodingHandling; use uucore::{format_usage, memo}; const VERSION: &str = "version"; @@ -271,7 +270,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); let format_string = matches diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index 8873607f4..fc5820065 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -19,7 +19,7 @@ use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; use std::num::ParseIntError; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static NAME: &str = "ptx"; const USAGE: &str = "\ @@ -722,7 +722,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); // let mut opts = Options::new(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/relpath/src/relpath.rs b/src/uu/relpath/src/relpath.rs index aa54c5716..0e6051adb 100644 --- a/src/uu/relpath/src/relpath.rs +++ b/src/uu/relpath/src/relpath.rs @@ -12,8 +12,8 @@ use std::env; use std::path::{Path, PathBuf}; use uucore::display::println_verbatim; use uucore::error::{FromIo, UResult}; +use uucore::format_usage; use uucore::fs::{canonicalize, MissingHandling, ResolveMode}; -use uucore::{format_usage, InvalidEncodingHandling}; static ABOUT: &str = "Convert TO destination to the relative path from the FROM dir. If FROM path is omitted, current working dir will be used."; @@ -27,7 +27,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/shred/src/shred.rs b/src/uu/shred/src/shred.rs index 7b08a09b1..8fa525482 100644 --- a/src/uu/shred/src/shred.rs +++ b/src/uu/shred/src/shred.rs @@ -20,7 +20,7 @@ use std::io::SeekFrom; use std::path::{Path, PathBuf}; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; -use uucore::{format_usage, util_name, InvalidEncodingHandling}; +use uucore::{format_usage, util_name}; #[macro_use] extern crate uucore; @@ -266,7 +266,7 @@ pub mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index e4b1516ca..50594cedb 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -15,7 +15,7 @@ use std::fs::File; use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write}; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; mod rand_read_adapter; @@ -56,7 +56,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index d3ba90489..25c6cd5b6 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -47,9 +47,9 @@ use std::str::Utf8Error; use unicode_width::UnicodeWidthStr; use uucore::display::Quotable; use uucore::error::{set_exit_code, strip_errno, UError, UResult, USimpleError, UUsageError}; +use uucore::format_usage; use uucore::parse_size::{parse_size, ParseSizeError}; use uucore::version_cmp::version_cmp; -use uucore::{format_usage, InvalidEncodingHandling}; use crate::tmp_dir::TmpDirWrapper; @@ -1055,7 +1055,7 @@ fn make_sort_mode_arg<'a>(mode: &'a str, short: char, help: &'a str) -> Arg<'a> #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let mut settings: GlobalSettings = Default::default(); let matches = match uu_app().try_get_matches_from(args) { diff --git a/src/uu/stdbuf/src/stdbuf.rs b/src/uu/stdbuf/src/stdbuf.rs index 72f923606..a74091739 100644 --- a/src/uu/stdbuf/src/stdbuf.rs +++ b/src/uu/stdbuf/src/stdbuf.rs @@ -19,8 +19,8 @@ use std::process; use tempfile::tempdir; use tempfile::TempDir; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; +use uucore::format_usage; use uucore::parse_size::parse_size; -use uucore::{format_usage, InvalidEncodingHandling}; static ABOUT: &str = "Run COMMAND, with modified buffering operations for its standard streams.\n\n\ @@ -156,7 +156,7 @@ fn get_preload_env(tmp_dir: &mut TempDir) -> io::Result<(String, PathBuf)> { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/sum/src/sum.rs b/src/uu/sum/src/sum.rs index abd536900..1422e473b 100644 --- a/src/uu/sum/src/sum.rs +++ b/src/uu/sum/src/sum.rs @@ -16,7 +16,7 @@ use std::io::{stdin, Read}; use std::path::Path; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static NAME: &str = "sum"; static USAGE: &str = "{} [OPTION]... [FILE]..."; @@ -109,7 +109,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index b9786123b..924e30cfe 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -19,7 +19,6 @@ use std::{ use uucore::display::Quotable; use uucore::error::UError; use uucore::error::UResult; -use uucore::InvalidEncodingHandling; use uucore::{format_usage, show}; use crate::error::TacError; @@ -37,7 +36,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index 6c42b17eb..ed55780ef 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -20,9 +20,9 @@ use std::process::{self, Child, Stdio}; use std::time::Duration; use uucore::display::Quotable; use uucore::error::{UResult, USimpleError, UUsageError}; +use uucore::format_usage; use uucore::process::ChildExt; use uucore::signals::{signal_by_name_or_value, signal_name_by_value}; -use uucore::{format_usage, InvalidEncodingHandling}; static ABOUT: &str = "Start COMMAND, and kill it if still running after DURATION."; const USAGE: &str = "{} [OPTION] DURATION COMMAND..."; @@ -106,7 +106,7 @@ impl Config { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let command = uu_app(); diff --git a/src/uu/tr/src/tr.rs b/src/uu/tr/src/tr.rs index 825188383..61ada1ab7 100644 --- a/src/uu/tr/src/tr.rs +++ b/src/uu/tr/src/tr.rs @@ -18,8 +18,8 @@ use std::io::{stdin, stdout, BufReader, BufWriter}; use uucore::{format_usage, show}; use crate::operation::DeleteOperation; +use uucore::display::Quotable; use uucore::error::{UResult, USimpleError, UUsageError}; -use uucore::{display::Quotable, InvalidEncodingHandling}; static ABOUT: &str = "translate or delete characters"; const USAGE: &str = "{} [OPTION]... SET1 [SET2]"; @@ -40,7 +40,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let after_help = get_long_usage(); diff --git a/src/uu/tsort/src/tsort.rs b/src/uu/tsort/src/tsort.rs index 47fb05c83..4aa9aca83 100644 --- a/src/uu/tsort/src/tsort.rs +++ b/src/uu/tsort/src/tsort.rs @@ -12,7 +12,7 @@ use std::io::{stdin, BufRead, BufReader, Read}; use std::path::Path; use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "Topological sort the strings in FILE. Strings are defined as any sequence of tokens separated by whitespace (tab, space, or newline). @@ -25,7 +25,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/tty/src/tty.rs b/src/uu/tty/src/tty.rs index 4b492c50f..a8806703b 100644 --- a/src/uu/tty/src/tty.rs +++ b/src/uu/tty/src/tty.rs @@ -13,7 +13,7 @@ use clap::{crate_version, Arg, Command}; use std::ffi::CStr; use std::io::Write; use uucore::error::UResult; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static ABOUT: &str = "Print the file name of the terminal connected to standard input."; const USAGE: &str = "{} [OPTION]..."; @@ -24,7 +24,7 @@ mod options { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::ConvertLossy); + let args = args.collect_lossy(); let matches = uu_app().get_matches_from(args); diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index f333f9c18..e42bb11a3 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -21,7 +21,7 @@ use std::str::from_utf8; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult}; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; static NAME: &str = "unexpand"; static USAGE: &str = "{} [OPTION]... [FILE]..."; @@ -165,7 +165,7 @@ fn expand_shortcuts(args: &[String]) -> Vec { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let matches = uu_app().get_matches_from(expand_shortcuts(&args)); diff --git a/src/uu/who/src/who.rs b/src/uu/who/src/who.rs index 76b01f850..a679609e7 100644 --- a/src/uu/who/src/who.rs +++ b/src/uu/who/src/who.rs @@ -18,7 +18,7 @@ use std::ffi::CStr; use std::fmt::Write; use std::os::unix::fs::MetadataExt; use std::path::PathBuf; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::format_usage; mod options { pub const ALL: &str = "all"; @@ -56,7 +56,7 @@ fn get_long_usage() -> String { #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let args = args.collect_str(InvalidEncodingHandling::Ignore); + let args = args.collect_ignore(); let after_help = get_long_usage(); diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index c6c3200ef..e216b8fe3 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -82,8 +82,6 @@ use std::sync::atomic::Ordering; use once_cell::sync::Lazy; -use crate::display::Quotable; - #[macro_export] macro_rules! bin { ($util:ident) => { @@ -150,43 +148,15 @@ pub fn execution_phrase() -> &'static str { &EXECUTION_PHRASE } -pub enum InvalidEncodingHandling { - Ignore, - ConvertLossy, -} - pub trait Args: Iterator + Sized { - /// Converts each iterator item to a String and collects these into a vector - /// On invalid encoding, the result will depend on the argument. This method allows to either drop entries with illegal encoding - /// completely (```InvalidEncodingHandling::Ignore```), convert them using lossy-conversion (```InvalidEncodingHandling::ConvertLossy```) - /// which will result in strange strings or can chosen to panic. - /// # Arguments - /// * `handling` - This switch allows to switch the behavior, when invalid encoding is encountered - fn collect_str(self, handling: InvalidEncodingHandling) -> Vec { - self.map(|s| match s.into_string() { - Ok(string) => Ok(string), - Err(s_ret) => { - eprintln!( - "Input with broken encoding occurred! (s = {}) ", - s_ret.quote() - ); - match handling { - InvalidEncodingHandling::Ignore => Err(String::new()), - InvalidEncodingHandling::ConvertLossy => { - Err(s_ret.to_string_lossy().into_owned()) - } - } - } - }) - .filter(|s| match handling { - InvalidEncodingHandling::Ignore => s.is_ok(), - _ => true, - }) - .map(|s| match s { - Ok(v) => v, - Err(e) => e, - }) - .collect() + /// Collects the iterator into a `Vec`, lossily converting the `OsString`s to `Strings`. + fn collect_lossy(self) -> Vec { + self.map(|s| s.to_string_lossy().into_owned()).collect() + } + + /// Collects the iterator into a `Vec`, removing any elements that contain invalid encoding. + fn collect_ignore(self) -> Vec { + self.filter_map(|s| s.into_string().ok()).collect() } } @@ -209,42 +179,34 @@ mod tests { ] } - fn collect_os_str(vec: Vec, handling: InvalidEncodingHandling) -> Vec { - vec.into_iter().collect_str(handling) - } - #[cfg(any(unix, target_os = "redox"))] fn test_invalid_utf8_args_lossy(os_str: &OsStr) { - //assert our string is invalid utf8 + // assert our string is invalid utf8 assert!(os_str.to_os_string().into_string().is_err()); let test_vec = make_os_vec(os_str); - let collected_to_str = - collect_os_str(test_vec.clone(), InvalidEncodingHandling::ConvertLossy); - //conservation of length - when accepting lossy conversion no arguments may be dropped + let collected_to_str = test_vec.clone().into_iter().collect_lossy(); + // conservation of length - when accepting lossy conversion no arguments may be dropped assert_eq!(collected_to_str.len(), test_vec.len()); - //first indices identical + // first indices identical for index in 0..2 { - assert_eq!( - collected_to_str.get(index).unwrap(), - test_vec.get(index).unwrap().to_str().unwrap() - ); + assert_eq!(collected_to_str[index], test_vec[index].to_str().unwrap()); } - //lossy conversion for string with illegal encoding is done + // lossy conversion for string with illegal encoding is done assert_eq!( - *collected_to_str.get(2).unwrap(), + *collected_to_str[2], os_str.to_os_string().to_string_lossy() ); } #[cfg(any(unix, target_os = "redox"))] fn test_invalid_utf8_args_ignore(os_str: &OsStr) { - //assert our string is invalid utf8 + // assert our string is invalid utf8 assert!(os_str.to_os_string().into_string().is_err()); let test_vec = make_os_vec(os_str); - let collected_to_str = collect_os_str(test_vec.clone(), InvalidEncodingHandling::Ignore); - //assert that the broken entry is filtered out + let collected_to_str = test_vec.clone().into_iter().collect_ignore(); + // assert that the broken entry is filtered out assert_eq!(collected_to_str.len(), test_vec.len() - 1); - //assert that the unbroken indices are converted as expected + // assert that the unbroken indices are converted as expected for index in 0..2 { assert_eq!( collected_to_str.get(index).unwrap(), @@ -255,10 +217,10 @@ mod tests { #[test] fn valid_utf8_encoding_args() { - //create a vector containing only correct encoding + // create a vector containing only correct encoding let test_vec = make_os_vec(&OsString::from("test2")); - //expect complete conversion without losses, even when lossy conversion is accepted - let _ = collect_os_str(test_vec, InvalidEncodingHandling::ConvertLossy); + // expect complete conversion without losses, even when lossy conversion is accepted + let _ = test_vec.into_iter().collect_lossy(); } #[cfg(any(unix, target_os = "redox"))] diff --git a/tests/common/util.rs b/tests/common/util.rs index 03876a570..46bf4d85e 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -30,7 +30,7 @@ use std::rc::Rc; use std::thread::sleep; use std::time::Duration; use tempfile::TempDir; -use uucore::{Args, InvalidEncodingHandling}; +use uucore::Args; #[cfg(windows)] static PROGNAME: &str = concat!(env!("CARGO_PKG_NAME"), ".exe"); @@ -1021,7 +1021,7 @@ impl UCommand { let strings = args .iter() .map(|s| s.as_ref().to_os_string()) - .collect_str(InvalidEncodingHandling::Ignore); + .collect_ignore(); for s in strings { self.comm_string.push(' ');