From 6b30c356dd0043cd720426be4d1207f3fa6853a8 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sat, 4 Feb 2023 08:54:27 -0500 Subject: [PATCH] Change `Cmd::Run` signature to take `&clap::Options` instead of `Option<&clap::Options>` - saves 10k --- src/cmd/base32/mod.rs | 5 +---- src/cmd/base64/mod.rs | 5 +---- src/cmd/basename/mod.rs | 6 +----- src/cmd/bootstrap/mod.rs | 8 ++------ src/cmd/chmod/mod.rs | 5 +---- src/cmd/chown/chgrp.rs | 5 +---- src/cmd/chown/mod.rs | 5 +---- src/cmd/chroot/mod.rs | 5 +---- src/cmd/clear/mod.rs | 2 +- src/cmd/cut/mod.rs | 5 +---- src/cmd/dirname/mod.rs | 40 +++++++++++++++++++-------------------- src/cmd/echo/mod.rs | 2 +- src/cmd/expand/mod.rs | 7 +------ src/cmd/factor/mod.rs | 20 +++++++++----------- src/cmd/false/mod.rs | 2 +- src/cmd/fold/mod.rs | 5 +---- src/cmd/groups/mod.rs | 6 +----- src/cmd/head/mod.rs | 7 ++----- src/cmd/hostid/mod.rs | 2 +- src/cmd/hostname/mod.rs | 3 +-- src/cmd/link/mod.rs | 5 +---- src/cmd/logname/mod.rs | 2 +- src/cmd/md5sum/mod.rs | 5 +---- src/cmd/mkfifo/mod.rs | 7 ++----- src/cmd/mknod/mod.rs | 5 +---- src/cmd/mktemp/mod.rs | 5 +---- src/cmd/mod.rs | 2 +- src/cmd/mountpoint/mod.rs | 5 +---- src/cmd/nologin/mod.rs | 2 +- src/cmd/nproc/mod.rs | 6 +----- src/cmd/printenv/mod.rs | 7 ++----- src/cmd/pwd/mod.rs | 7 ++----- src/cmd/readlink/mod.rs | 7 ++----- src/cmd/realpath/mod.rs | 7 ++----- src/cmd/rev/mod.rs | 7 ++----- src/cmd/rm/mod.rs | 5 +---- src/cmd/rmdir/mod.rs | 7 ++----- src/cmd/sha1sum/mod.rs | 5 +---- src/cmd/sha224sum/mod.rs | 5 +---- src/cmd/sha256sum/mod.rs | 5 +---- src/cmd/sha384sum/mod.rs | 5 +---- src/cmd/sha512sum/mod.rs | 5 +---- src/cmd/shitbox/mod.rs | 13 +++---------- src/cmd/sleep/mod.rs | 4 ++-- src/cmd/sync/mod.rs | 10 +++------- src/cmd/true/mod.rs | 2 +- src/cmd/unlink/mod.rs | 6 +----- src/cmd/wc/mod.rs | 7 ++----- src/cmd/which/mod.rs | 9 +++------ src/cmd/whoami/mod.rs | 2 +- src/cmd/yes/mod.rs | 6 +----- src/hash/mod.rs | 2 -- src/lib.rs | 2 +- 53 files changed, 95 insertions(+), 229 deletions(-) diff --git a/src/cmd/base32/mod.rs b/src/cmd/base32/mod.rs index 97d931a..60ea587 100644 --- a/src/cmd/base32/mod.rs +++ b/src/cmd/base32/mod.rs @@ -20,10 +20,7 @@ impl Cmd for Base32 { .args(args()) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "No input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, diff --git a/src/cmd/base64/mod.rs b/src/cmd/base64/mod.rs index b47b963..c707c69 100644 --- a/src/cmd/base64/mod.rs +++ b/src/cmd/base64/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Base64 { .args(args()) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No input"))); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, diff --git a/src/cmd/basename/mod.rs b/src/cmd/basename/mod.rs index 31e2fe8..3fb0a45 100644 --- a/src/cmd/basename/mod.rs +++ b/src/cmd/basename/mod.rs @@ -1,6 +1,5 @@ use super::Cmd; use clap::{Arg, ArgMatches, Command}; -use std::io; #[derive(Debug, Default)] pub struct Basename; @@ -26,10 +25,7 @@ impl Cmd for Basename { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { if let Some(mut base) = matches .get_one::("NAME") .and_then(|x| x.split('/').last()) diff --git a/src/cmd/bootstrap/mod.rs b/src/cmd/bootstrap/mod.rs index 94418fa..4b2cc73 100644 --- a/src/cmd/bootstrap/mod.rs +++ b/src/cmd/bootstrap/mod.rs @@ -5,8 +5,7 @@ use clap_complete_nushell::Nushell; use clap_mangen::Man; use std::{ error::Error, - fs, - io::{self, ErrorKind}, + fs, io, os::unix::fs::symlink, path::{Path, PathBuf}, }; @@ -152,10 +151,7 @@ impl Cmd for Bootstrap { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(ErrorKind::Other, "No input").into()); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { if let Some(prefix) = matches.get_one::("prefix") { let usr = matches.get_flag("usr"); if let Some(progpath) = crate::progpath() { diff --git a/src/cmd/chmod/mod.rs b/src/cmd/chmod/mod.rs index 960c6b1..ed7fd8b 100644 --- a/src/cmd/chmod/mod.rs +++ b/src/cmd/chmod/mod.rs @@ -43,10 +43,7 @@ impl Cmd for Chmod { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let feedback = Feedback::from_matches(matches); let mode = matches .get_one::("mode") diff --git a/src/cmd/chown/chgrp.rs b/src/cmd/chown/chgrp.rs index 92cf1a6..8613475 100644 --- a/src/cmd/chown/chgrp.rs +++ b/src/cmd/chown/chgrp.rs @@ -36,10 +36,7 @@ impl Cmd for Chgrp { ) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let recurse = Recurse::from_matches(matches); let feedback = Feedback::from_matches(matches); let group = if let Some(grp) = matches.get_one::("group") { diff --git a/src/cmd/chown/mod.rs b/src/cmd/chown/mod.rs index f8722f6..0a7b8fd 100644 --- a/src/cmd/chown/mod.rs +++ b/src/cmd/chown/mod.rs @@ -38,10 +38,7 @@ impl Cmd for Chown { ) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let recurse = Recurse::from_matches(matches); let feedback = Feedback::from_matches(matches); let (user, group) = if let Some(who) = matches.get_one::("user") { diff --git a/src/cmd/chroot/mod.rs b/src/cmd/chroot/mod.rs index 3ad2e1c..8c66c87 100644 --- a/src/cmd/chroot/mod.rs +++ b/src/cmd/chroot/mod.rs @@ -38,10 +38,7 @@ impl Cmd for Chroot { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let Some(newroot) = matches.get_one::("newroot") else { return Err(io::Error::new(io::ErrorKind::Other, "no new root given").into()); }; diff --git a/src/cmd/clear/mod.rs b/src/cmd/clear/mod.rs index 5ae0c02..b576b04 100644 --- a/src/cmd/clear/mod.rs +++ b/src/cmd/clear/mod.rs @@ -12,7 +12,7 @@ impl Cmd for Clear { .version(env!("CARGO_PKG_VERSION")) } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { print!("\x1b[2J\x1b[H"); print!("\x1b[3J\x1b[H"); Ok(()) diff --git a/src/cmd/cut/mod.rs b/src/cmd/cut/mod.rs index 3415db0..47c361e 100644 --- a/src/cmd/cut/mod.rs +++ b/src/cmd/cut/mod.rs @@ -63,10 +63,7 @@ impl Cmd for Cut { ) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let files: Vec = match matches.get_many::("file") { Some(f) => f.cloned().collect(), None => vec!["-".to_string()], diff --git a/src/cmd/dirname/mod.rs b/src/cmd/dirname/mod.rs index 257868c..2201cbd 100644 --- a/src/cmd/dirname/mod.rs +++ b/src/cmd/dirname/mod.rs @@ -19,27 +19,25 @@ impl Cmd for Dirname { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - if let Some(matches) = matches { - if let Some(names) = matches.get_many::("name") { - names.for_each(|name| { - let path = match Path::new(name).parent() { - Some(p) => p, - None => Path::new("."), - }; - let path = path.to_string_lossy(); - let path = if path.is_empty() { - String::from(".") - } else { - path.to_string() - }; - if matches.get_flag("zero") { - print!("{path}\0"); - } else { - println!("{path}"); - } - }); - } + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { + if let Some(names) = matches.get_many::("name") { + names.for_each(|name| { + let path = match Path::new(name).parent() { + Some(p) => p, + None => Path::new("."), + }; + let path = path.to_string_lossy(); + let path = if path.is_empty() { + String::from(".") + } else { + path.to_string() + }; + if matches.get_flag("zero") { + print!("{path}\0"); + } else { + println!("{path}"); + } + }); } Ok(()) } diff --git a/src/cmd/echo/mod.rs b/src/cmd/echo/mod.rs index 15dbd29..1e0bdeb 100644 --- a/src/cmd/echo/mod.rs +++ b/src/cmd/echo/mod.rs @@ -19,7 +19,7 @@ impl Cmd for Echo { ]) } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { let args: Vec = env::args().collect(); let idx = match crate::progname() { Some(s) if s.as_str() == "echo" => 1, diff --git a/src/cmd/expand/mod.rs b/src/cmd/expand/mod.rs index c40e6b8..03a4ef5 100644 --- a/src/cmd/expand/mod.rs +++ b/src/cmd/expand/mod.rs @@ -1,5 +1,3 @@ -use std::io; - use super::Cmd; use clap::{Arg, ArgAction, Command, ValueHint}; @@ -40,10 +38,7 @@ impl Cmd for Expand { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { Ok(()) } diff --git a/src/cmd/factor/mod.rs b/src/cmd/factor/mod.rs index f18ef90..afa4086 100644 --- a/src/cmd/factor/mod.rs +++ b/src/cmd/factor/mod.rs @@ -24,17 +24,15 @@ impl Cmd for Factor { ) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - if let Some(matches) = matches { - match matches.get_many::("number") { - Some(numbers) => { - numbers.for_each(|n| print_factors(*n)); - } - None => { - for line in io::stdin().lock().lines() { - for num in line?.split_whitespace() { - print_factors(num.parse()?); - } + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { + match matches.get_many::("number") { + Some(numbers) => { + numbers.for_each(|n| print_factors(*n)); + } + None => { + for line in io::stdin().lock().lines() { + for num in line?.split_whitespace() { + print_factors(num.parse()?); } } } diff --git a/src/cmd/false/mod.rs b/src/cmd/false/mod.rs index b6ea090..b35ef58 100644 --- a/src/cmd/false/mod.rs +++ b/src/cmd/false/mod.rs @@ -13,7 +13,7 @@ impl Cmd for False { .author("Nathan Fisher") } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { process::exit(1); } diff --git a/src/cmd/fold/mod.rs b/src/cmd/fold/mod.rs index 23d198f..e613a07 100644 --- a/src/cmd/fold/mod.rs +++ b/src/cmd/fold/mod.rs @@ -46,10 +46,7 @@ impl Cmd for Fold { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { let files = match matches.get_many::("FILE") { Some(c) => c.map(String::to_string).collect(), None => vec!["-".to_string()], diff --git a/src/cmd/groups/mod.rs b/src/cmd/groups/mod.rs index 5f3336e..34cd807 100644 --- a/src/cmd/groups/mod.rs +++ b/src/cmd/groups/mod.rs @@ -1,7 +1,6 @@ use super::Cmd; use crate::pw; use clap::{Arg, Command}; -use std::io; #[derive(Debug, Default)] pub struct Groups; @@ -21,10 +20,7 @@ impl Cmd for Groups { .arg(Arg::new("user")) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))) - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let groups = match matches.get_one::("user") { Some(u) => pw::get_group_names_for_name(u)?, None => pw::get_group_names()?, diff --git a/src/cmd/head/mod.rs b/src/cmd/head/mod.rs index 24c1b3e..5a9a068 100644 --- a/src/cmd/head/mod.rs +++ b/src/cmd/head/mod.rs @@ -5,7 +5,7 @@ use std::{ env, error::Error, fs, - io::{self, stdin, Read, Write}, + io::{stdin, Read, Write}, }; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -57,7 +57,7 @@ impl Cmd for Head { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { let args: Vec<_> = env::args().collect(); let idx = match crate::progname() { Some(s) if s.as_str() == "head" => 1, @@ -80,9 +80,6 @@ impl Cmd for Head { } } } - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "No input").into()); - }; if let Some(l) = matches.get_one("LINES") { lines = *l; } diff --git a/src/cmd/hostid/mod.rs b/src/cmd/hostid/mod.rs index 9741b5b..b813c2c 100644 --- a/src/cmd/hostid/mod.rs +++ b/src/cmd/hostid/mod.rs @@ -12,7 +12,7 @@ impl Cmd for Hostid { .version(env!("CARGO_PKG_VERSION")) } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { let hostid = unsafe { libc::gethostid() }; let hostid: String = format!("{hostid:x}").chars().skip(8).collect(); println!("{}", hostid); diff --git a/src/cmd/hostname/mod.rs b/src/cmd/hostname/mod.rs index 32cdbda..56d982d 100644 --- a/src/cmd/hostname/mod.rs +++ b/src/cmd/hostname/mod.rs @@ -22,8 +22,7 @@ impl Cmd for Hostname { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let matches = matches.unwrap(); + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { if let Some(name) = matches.get_one::("NAME") { unistd::sethostname(name)?; Ok(()) diff --git a/src/cmd/link/mod.rs b/src/cmd/link/mod.rs index a97f147..46e66db 100644 --- a/src/cmd/link/mod.rs +++ b/src/cmd/link/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Link { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let Some(f1) = matches.get_one::("file1") else { return Err(Box::new(io::Error::new( io::ErrorKind::Other, diff --git a/src/cmd/logname/mod.rs b/src/cmd/logname/mod.rs index a891038..22be191 100644 --- a/src/cmd/logname/mod.rs +++ b/src/cmd/logname/mod.rs @@ -14,7 +14,7 @@ impl Cmd for Logname { .version(env!("CARGO_PKG_VERSION")) } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { let logname = unsafe { CStr::from_ptr(libc::getlogin()) }; let logname = logname.to_str()?; println!("{logname}"); diff --git a/src/cmd/md5sum/mod.rs b/src/cmd/md5sum/mod.rs index 7175933..8226ca8 100644 --- a/src/cmd/md5sum/mod.rs +++ b/src/cmd/md5sum/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Md5sum { .args([args::check(), args::file()]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { diff --git a/src/cmd/mkfifo/mod.rs b/src/cmd/mkfifo/mod.rs index 968f3f6..d086b04 100644 --- a/src/cmd/mkfifo/mod.rs +++ b/src/cmd/mkfifo/mod.rs @@ -1,7 +1,7 @@ use super::Cmd; use crate::{args, mode::Parser, stat}; use clap::{Arg, ArgMatches, Command}; -use std::{error::Error, io}; +use std::error::Error; #[derive(Debug, Default)] pub struct MkFifo; @@ -35,10 +35,7 @@ impl Cmd for MkFifo { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { let mode = if let Some(m) = matches.get_one::("mode") { Parser::new(0o666).parse(m)? } else { diff --git a/src/cmd/mknod/mod.rs b/src/cmd/mknod/mod.rs index c556380..4c38fb6 100644 --- a/src/cmd/mknod/mod.rs +++ b/src/cmd/mknod/mod.rs @@ -40,10 +40,7 @@ impl Cmd for MkNod { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { let Some(file) = matches.get_one::("file") else { return Err(io::Error::new(io::ErrorKind::Other, "no file given").into()); }; diff --git a/src/cmd/mktemp/mod.rs b/src/cmd/mktemp/mod.rs index bda2c46..56e7cf4 100644 --- a/src/cmd/mktemp/mod.rs +++ b/src/cmd/mktemp/mod.rs @@ -36,10 +36,7 @@ impl Cmd for MkTemp { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let mut path = if let Some(t) = matches.get_one::("tmpdir") { PathBuf::from(t) } else { diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 61715e5..680c472 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -67,7 +67,7 @@ pub trait Cmd: fmt::Debug + Sync { /// Runs the applet /// # Errors /// Bubbles up any errors to the caller - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box>; + fn run(&self, matches: &ArgMatches) -> Result<(), Box>; /// Returns the path relative to the binary where the link to this applet /// will be installed fn path(&self) -> Option; diff --git a/src/cmd/mountpoint/mod.rs b/src/cmd/mountpoint/mod.rs index 0f15d58..c585fb8 100644 --- a/src/cmd/mountpoint/mod.rs +++ b/src/cmd/mountpoint/mod.rs @@ -55,10 +55,7 @@ impl Cmd for Mountpoint { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let file = matches.get_one::("file").unwrap(); if matches.get_flag("fs-devno") { if let Some(maj_min) = fs_devno(file)? { diff --git a/src/cmd/nologin/mod.rs b/src/cmd/nologin/mod.rs index 43fc229..41e2e8d 100644 --- a/src/cmd/nologin/mod.rs +++ b/src/cmd/nologin/mod.rs @@ -12,7 +12,7 @@ impl Cmd for Nologin { .about("Denies a user account login ability") } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { eprintln!("I'm sorry, I can't let you do that, Dave"); process::exit(42); } diff --git a/src/cmd/nproc/mod.rs b/src/cmd/nproc/mod.rs index 51eae8f..d9b769c 100644 --- a/src/cmd/nproc/mod.rs +++ b/src/cmd/nproc/mod.rs @@ -1,6 +1,5 @@ use super::Cmd; use clap::{Arg, ArgAction, Command}; -use std::io; #[derive(Debug, Default)] pub struct Nproc; @@ -19,10 +18,7 @@ impl Cmd for Nproc { ) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if matches.get_flag("ALL") { println!("{}", unsafe { get_nprocs_conf() }); } else { diff --git a/src/cmd/printenv/mod.rs b/src/cmd/printenv/mod.rs index 9c34aa8..8fd657b 100644 --- a/src/cmd/printenv/mod.rs +++ b/src/cmd/printenv/mod.rs @@ -1,4 +1,4 @@ -use std::{env, io}; +use std::env; use super::Cmd; use clap::{Arg, ArgAction, Command}; @@ -25,10 +25,7 @@ impl Cmd for Printenv { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(vars) = matches.get_many::("var") { for var in vars { let val = env::var(var)?; diff --git a/src/cmd/pwd/mod.rs b/src/cmd/pwd/mod.rs index 5705d32..97fb32b 100644 --- a/src/cmd/pwd/mod.rs +++ b/src/cmd/pwd/mod.rs @@ -1,6 +1,6 @@ use super::Cmd; use clap::{Arg, ArgAction, Command}; -use std::{env, io, path::PathBuf}; +use std::{env, path::PathBuf}; #[derive(Debug, Default)] pub struct Pwd; @@ -26,10 +26,7 @@ impl Cmd for Pwd { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let cwd = if matches.get_flag("logical") { PathBuf::from(env::var("PWD")?) } else if matches.get_flag("physical") { diff --git a/src/cmd/readlink/mod.rs b/src/cmd/readlink/mod.rs index ace6c2c..7ac8765 100644 --- a/src/cmd/readlink/mod.rs +++ b/src/cmd/readlink/mod.rs @@ -1,6 +1,6 @@ use crate::Cmd; use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint}; -use std::{fs, io, path::PathBuf}; +use std::{fs, path::PathBuf}; #[derive(Debug, Default)] pub struct Readlink; @@ -31,10 +31,7 @@ impl Cmd for Readlink { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { if let Some(paths) = matches.get_many::("path") { let paths: Vec<_> = paths.collect(); let len = paths.len(); diff --git a/src/cmd/realpath/mod.rs b/src/cmd/realpath/mod.rs index d66d24c..da919be 100644 --- a/src/cmd/realpath/mod.rs +++ b/src/cmd/realpath/mod.rs @@ -1,6 +1,6 @@ use super::Cmd; use clap::{Arg, ArgAction, Command, ValueHint}; -use std::{env, fs, io, process}; +use std::{env, fs, process}; #[derive(Debug, Default)] pub struct Realpath; @@ -24,10 +24,7 @@ impl Cmd for Realpath { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("path") { let mut erred = false; for f in files { diff --git a/src/cmd/rev/mod.rs b/src/cmd/rev/mod.rs index b95585c..2ae69d3 100644 --- a/src/cmd/rev/mod.rs +++ b/src/cmd/rev/mod.rs @@ -3,7 +3,7 @@ use crate::args; use clap::{Arg, Command}; use std::{ fs::File, - io::{self, BufRead, BufReader, ErrorKind, Write}, + io::{self, BufRead, BufReader, Write}, }; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -25,10 +25,7 @@ impl Cmd for Rev { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(ErrorKind::Other, "No input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let color = match matches.get_one::("color").map(String::as_str) { Some("always") => ColorChoice::Always, Some("ansi") => ColorChoice::AlwaysAnsi, diff --git a/src/cmd/rm/mod.rs b/src/cmd/rm/mod.rs index 7c53863..5d7eba8 100644 --- a/src/cmd/rm/mod.rs +++ b/src/cmd/rm/mod.rs @@ -62,10 +62,7 @@ impl Cmd for Rm { ) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let mut actions = AllActions::from(matches); let proceed = match actions.prompt { When::Always => true, diff --git a/src/cmd/rmdir/mod.rs b/src/cmd/rmdir/mod.rs index 7d9bc65..1e8964a 100644 --- a/src/cmd/rmdir/mod.rs +++ b/src/cmd/rmdir/mod.rs @@ -1,7 +1,7 @@ use super::Cmd; use crate::args; use clap::{Arg, ArgAction, Command, ValueHint}; -use std::{error::Error, fs, io, path::Path}; +use std::{error::Error, fs, path::Path}; #[derive(Debug, Default)] pub struct Rmdir; @@ -27,10 +27,7 @@ impl Cmd for Rmdir { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(directories) = matches.get_many::("dir") { for dir in directories { if matches.get_flag("parents") { diff --git a/src/cmd/sha1sum/mod.rs b/src/cmd/sha1sum/mod.rs index 060bfa1..ce553bd 100644 --- a/src/cmd/sha1sum/mod.rs +++ b/src/cmd/sha1sum/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Sha1sum { .args([args::check(), args::file()]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { diff --git a/src/cmd/sha224sum/mod.rs b/src/cmd/sha224sum/mod.rs index 69aaefa..5da6985 100644 --- a/src/cmd/sha224sum/mod.rs +++ b/src/cmd/sha224sum/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Sha224sum { .args([args::check(), args::file()]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { diff --git a/src/cmd/sha256sum/mod.rs b/src/cmd/sha256sum/mod.rs index 1412516..8233482 100644 --- a/src/cmd/sha256sum/mod.rs +++ b/src/cmd/sha256sum/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Sha256sum { .args([args::check(), args::file()]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { diff --git a/src/cmd/sha384sum/mod.rs b/src/cmd/sha384sum/mod.rs index b7e8672..94b4b34 100644 --- a/src/cmd/sha384sum/mod.rs +++ b/src/cmd/sha384sum/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Sha384sum { .args([args::check(), args::file()]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { diff --git a/src/cmd/sha512sum/mod.rs b/src/cmd/sha512sum/mod.rs index 3d63bf0..a600360 100644 --- a/src/cmd/sha512sum/mod.rs +++ b/src/cmd/sha512sum/mod.rs @@ -19,10 +19,7 @@ impl Cmd for Sha512sum { .args([args::check(), args::file()]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { diff --git a/src/cmd/shitbox/mod.rs b/src/cmd/shitbox/mod.rs index 20063e5..664db57 100644 --- a/src/cmd/shitbox/mod.rs +++ b/src/cmd/shitbox/mod.rs @@ -1,10 +1,6 @@ use super::{Cmd, COMMANDS}; use clap::Command; -use std::{ - error::Error, - io::{self, ErrorKind}, - process, -}; +use std::{error::Error, process}; #[derive(Debug, Default)] pub struct Shitbox; @@ -33,13 +29,10 @@ impl Cmd for Shitbox { .subcommands(&subcommands) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(ErrorKind::Other, "No input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some((name, matches)) = matches.subcommand() { if let Some(command) = crate::cmd::get(name) { - if let Err(e) = command.run(Some(matches)) { + if let Err(e) = command.run(matches) { eprintln!("Error: {name}: {e}"); process::exit(1); } diff --git a/src/cmd/sleep/mod.rs b/src/cmd/sleep/mod.rs index b7111fe..196e938 100644 --- a/src/cmd/sleep/mod.rs +++ b/src/cmd/sleep/mod.rs @@ -27,8 +27,8 @@ impl Cmd for Sleep { } #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - if let Some(raw) = matches.unwrap().get_one::("seconds") { + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { + if let Some(raw) = matches.get_one::("seconds") { let seconds = *raw as u64; let nanos = ((raw % 1.0) * 10e-9) as u32; let s = Duration::new(seconds, nanos); diff --git a/src/cmd/sync/mod.rs b/src/cmd/sync/mod.rs index fd238c2..7e6d8d8 100644 --- a/src/cmd/sync/mod.rs +++ b/src/cmd/sync/mod.rs @@ -1,8 +1,7 @@ -use crate::unistd; - use super::Cmd; +use crate::unistd; use clap::{Arg, ArgAction, Command}; -use std::{error::Error, fs::OpenOptions, io}; +use std::{error::Error, fs::OpenOptions}; #[derive(Debug, Default)] pub struct Sync; @@ -34,10 +33,7 @@ impl Cmd for Sync { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("FILE") { for f in files { let mut opts = OpenOptions::new(); diff --git a/src/cmd/true/mod.rs b/src/cmd/true/mod.rs index 3506d76..d80ce2d 100644 --- a/src/cmd/true/mod.rs +++ b/src/cmd/true/mod.rs @@ -13,7 +13,7 @@ impl Cmd for True { .author("Nathan Fisher") } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { process::exit(0); } diff --git a/src/cmd/unlink/mod.rs b/src/cmd/unlink/mod.rs index d3adb5d..3eeea2e 100644 --- a/src/cmd/unlink/mod.rs +++ b/src/cmd/unlink/mod.rs @@ -1,7 +1,6 @@ use super::Cmd; use crate::{args, unistd}; use clap::{Arg, Command}; -use std::io; #[derive(Debug, Default)] pub struct Unlink; @@ -21,10 +20,7 @@ impl Cmd for Unlink { ]) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { if let Some(files) = matches.get_many::("file") { for f in files { unistd::unlink(f)?; diff --git a/src/cmd/wc/mod.rs b/src/cmd/wc/mod.rs index 545b2bf..6eadb07 100644 --- a/src/cmd/wc/mod.rs +++ b/src/cmd/wc/mod.rs @@ -6,7 +6,7 @@ use std::{ error::Error, fmt::{self, Write}, fs, - io::{self, stdin, Read}, + io::{stdin, Read}, ops::{AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}, str::FromStr, }; @@ -52,10 +52,7 @@ impl Cmd for Wc { ]) } - fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); - }; + fn run(&self, matches: &ArgMatches) -> Result<(), Box> { let mut flags = 0; for arg in &["LINES", "WORDS", "CHARS", "BYTES", "MAX"] { if matches.get_flag(arg) { diff --git a/src/cmd/which/mod.rs b/src/cmd/which/mod.rs index 710a0ff..ed51c46 100644 --- a/src/cmd/which/mod.rs +++ b/src/cmd/which/mod.rs @@ -1,7 +1,7 @@ use super::Cmd; use crate::{bitflags::BitFlags, mode::Bit}; use clap::{Arg, Command}; -use std::{env, fs::File, io, os::unix::prelude::MetadataExt, path::PathBuf, process}; +use std::{env, fs::File, os::unix::prelude::MetadataExt, path::PathBuf, process}; #[derive(Debug, Default)] pub struct Which; @@ -15,10 +15,7 @@ impl Cmd for Which { .arg(Arg::new("COMMAND").num_args(1..)) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let rawpath = if let Ok(p) = env::var("PATH") { p } else { @@ -31,7 +28,7 @@ impl Cmd for Which { if let Some(p) = which(command, &path) { println!("{p}"); } else { - println!("which: no {} in ({})", command, &rawpath); + println!("which: no {command} in ({})", &rawpath); failures += 1; } } diff --git a/src/cmd/whoami/mod.rs b/src/cmd/whoami/mod.rs index adea215..ee065a7 100644 --- a/src/cmd/whoami/mod.rs +++ b/src/cmd/whoami/mod.rs @@ -15,7 +15,7 @@ impl Cmd for Whoami { .author("Nathan Fisher") } - fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { let pwnam = unsafe { let pw = *getpwuid(geteuid()); CStr::from_ptr((pw).pw_name) diff --git a/src/cmd/yes/mod.rs b/src/cmd/yes/mod.rs index 37ef44c..9175b05 100644 --- a/src/cmd/yes/mod.rs +++ b/src/cmd/yes/mod.rs @@ -1,6 +1,5 @@ use super::Cmd; use clap::{Arg, Command}; -use std::io; #[derive(Debug, Default)] pub struct Yes; @@ -13,10 +12,7 @@ impl Cmd for Yes { .arg(Arg::new("msg").num_args(1).default_value("y")) } - fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let Some(matches) = matches else { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input"))); - }; + fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { let msg = matches.get_one::("msg").unwrap(); loop { println!("{msg}"); diff --git a/src/hash/mod.rs b/src/hash/mod.rs index 61fad88..fd203aa 100644 --- a/src/hash/mod.rs +++ b/src/hash/mod.rs @@ -12,7 +12,6 @@ use { }; pub enum HashType { - Blake2b, Md5, Sha1, Sha224, @@ -54,7 +53,6 @@ pub fn check_sums(file: &str, hashtype: HashType, erred: &mut usize) -> Result<( io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(), )?; let s = match hashtype { - HashType::Blake2b => unimplemented!(), HashType::Md5 => compute_hash(file, Md5::new())?, HashType::Sha1 => compute_hash(file, Sha1::new())?, HashType::Sha224 => compute_hash(file, Sha224::new())?, diff --git a/src/lib.rs b/src/lib.rs index c4d7a20..926bf0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,7 +68,7 @@ pub fn run() { if let Some(progname) = progname() { if let Some(command) = cmd::get(&progname) { let cli = command.cli(); - if let Err(e) = command.run(Some(&cli.get_matches())) { + if let Err(e) = command.run(&cli.get_matches()) { eprintln!("{progname}: Error: {e}"); process::exit(1); }