From 95b781ae9af7e3090931bb7be59f1f1e70bdc990 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 3 Jan 2023 19:47:00 -0500 Subject: [PATCH] Fix some clippy lints --- src/cmd/base32/mod.rs | 25 ++++++++++--------- src/cmd/bootstrap/mod.rs | 4 +--- src/cmd/dirname/mod.rs | 5 ++-- src/cmd/factor/mod.rs | 52 ++++++++++++++++++++-------------------- src/cmd/head/mod.rs | 8 +++---- src/cmd/shitbox/mod.rs | 8 +++---- src/lib.rs | 8 ++++--- src/math.rs | 18 ++++++++++++++ 8 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 src/math.rs diff --git a/src/cmd/base32/mod.rs b/src/cmd/base32/mod.rs index 70b8625..1d0b770 100644 --- a/src/cmd/base32/mod.rs +++ b/src/cmd/base32/mod.rs @@ -1,5 +1,5 @@ use super::Cmd; -use clap::{Arg, ArgAction, Command, value_parser}; +use clap::{value_parser, Arg, ArgAction, Command}; use data_encoding::BASE32; use std::{ fs, @@ -61,20 +61,19 @@ impl Cmd for Base32 { } fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let matches = match matches { - Some(m) => m, - None => return Err(io::Error::new(io::ErrorKind::Other, "No input").into()), + let Some(matches) = matches else { + return Err(io::Error::new(io::ErrorKind::Other, "No input").into()); }; let files: Vec<_> = match matches.get_many::("INPUT") { - Some(c) => c.map(|x| x.to_owned()).collect(), + Some(c) => c.map(|x| x.clone()).collect(), None => vec![String::from("-")], }; let len = files.len(); for (index, file) in files.into_iter().enumerate() { if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") { match index { - 0 => println!("===> {} <===", file), - _ => println!("\n===> {} <===", file), + 0 => println!("===> {file} <==="), + _ => println!("\n===> {file} <==="), }; } else if index > 0 { println!(); @@ -111,14 +110,14 @@ fn decode_base32(mut contents: String, ignore: bool) { let decoded = match BASE32.decode(contents.as_bytes()) { Ok(c) => c, Err(e) => { - eprintln!("base32: {}", e); + eprintln!("base32: {e}"); process::exit(1); } }; let output = match String::from_utf8(decoded) { Ok(c) => c, Err(e) => { - eprintln!("base32: {}", e); + eprintln!("base32: {e}"); process::exit(1); } }; @@ -134,7 +133,7 @@ fn encode_base32(contents: &str, wrap: usize) { .map(|c| c.iter().collect::()) .collect::>(); for line in &encoded { - println!("{}", line); + println!("{line}"); } } @@ -144,15 +143,15 @@ fn get_contents(file: &str) -> String { match io::stdin().read_to_string(&mut contents) { Ok(_) => true, Err(e) => { - eprintln!("base32: {}", e); + eprintln!("base32: {e}"); process::exit(1); } }; } else { - contents = match fs::read_to_string(&file) { + contents = match fs::read_to_string(file) { Ok(c) => c, Err(e) => { - eprintln!("base32: {}", e); + eprintln!("base32: {e}"); process::exit(1); } }; diff --git a/src/cmd/bootstrap/mod.rs b/src/cmd/bootstrap/mod.rs index 6026117..e2ac804 100644 --- a/src/cmd/bootstrap/mod.rs +++ b/src/cmd/bootstrap/mod.rs @@ -163,9 +163,7 @@ impl Cmd for Bootstrap { } fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { - let matches = if let Some(m) = matches { - m - } else { + let Some(matches) = matches else { return Err(io::Error::new(ErrorKind::Other, "No input").into()); }; if let Some(prefix) = matches.get_one::("prefix") { diff --git a/src/cmd/dirname/mod.rs b/src/cmd/dirname/mod.rs index 4478b2b..7219b12 100644 --- a/src/cmd/dirname/mod.rs +++ b/src/cmd/dirname/mod.rs @@ -46,9 +46,9 @@ impl Cmd for Dirname { path.to_string() }; if matches.get_flag("zero") { - print!("{}\0", path); + print!("{path}\0"); } else { - println!("{}", path); + println!("{path}"); } }); } @@ -60,4 +60,3 @@ impl Cmd for Dirname { self.path } } - diff --git a/src/cmd/factor/mod.rs b/src/cmd/factor/mod.rs index 8c258af..8c6792c 100644 --- a/src/cmd/factor/mod.rs +++ b/src/cmd/factor/mod.rs @@ -1,6 +1,9 @@ -use clap::{Arg, ArgMatches, Command, value_parser}; use crate::Cmd; -use std::{error::Error, io::{self, BufRead}}; +use clap::{value_parser, Arg, ArgMatches, Command}; +use std::{ + error::Error, + io::{self, BufRead}, +}; #[derive(Debug)] pub struct Factor { @@ -23,13 +26,13 @@ impl Cmd for Factor { .about("factor numbers") .after_help( "Print the prime factors of each specified integer NUMBER. If none are\n\ - specified on the command line, read them from standard input." + specified on the command line, read them from standard input.", ) .arg( Arg::new("number") .help("the numbers to factor") .num_args(0..) - .value_parser(value_parser!(u64)) + .value_parser(value_parser!(u64)), ) } @@ -38,15 +41,14 @@ impl Cmd for Factor { match matches.get_many::("number") { Some(numbers) => { numbers.for_each(|n| print_factors(*n)); - }, + } None => { for line in io::stdin().lock().lines() { - for value in line.unwrap().split_whitespace() { - let num: u64 = value.parse()?; - print_factors(num); + for num in line?.split_whitespace() { + print_factors(num.parse()?); } } - }, + } } } Ok(()) @@ -57,30 +59,28 @@ impl Cmd for Factor { } } -fn is_prime(num: u64) -> bool { - match num { - 0 | 1 => false, - 2 | 3 | 5 | 7 | 11 | 13 => true, - x if x % 2 == 0 => false, - _ => { - let mut x: u64 = 2; - while x < num / 2 { - if num % x == 0 { - return false; - } - x += 1; - } - true +fn first_factor(num: u64) -> u64 { + if crate::math::is_prime(num) { + return num; + } + for n in 2..=num { + if num % n == 0 { + return n; } } + num } fn print_factors(num: u64) { print!("{num}:"); - for n in 2..=num / 2 { - if num % n == 0 && is_prime(n) { - print!(" {n}"); + let mut n = num; + loop { + let f = first_factor(n); + print!(" {f}"); + if f == n { + break; } + n /= f; } println!(); } diff --git a/src/cmd/head/mod.rs b/src/cmd/head/mod.rs index ff1e41f..94aafee 100644 --- a/src/cmd/head/mod.rs +++ b/src/cmd/head/mod.rs @@ -80,19 +80,17 @@ impl Cmd for Head { if arg1.starts_with('-') && arg1.len() > 1 { let lines: usize = arg1[1..].parse()?; let files: Vec<_> = if args.len() > idx + 1 { - args[idx + 1..].iter().map(|x| x.as_str()).collect() + args[idx + 1..].iter().map(String::as_str).collect() } else { vec!["-"] }; for file in &files { - head(&file, lines, false, false); + head(file, lines, false, false); } return Ok(()); } } - let matches = if let Some(m) = matches { - m - } else { + let Some(matches) = matches else { return Err(io::Error::new(io::ErrorKind::Other, "No input").into()); }; if let Some(l) = matches.get_one("LINES") { diff --git a/src/cmd/shitbox/mod.rs b/src/cmd/shitbox/mod.rs index 2e36137..00d02c9 100644 --- a/src/cmd/shitbox/mod.rs +++ b/src/cmd/shitbox/mod.rs @@ -1,4 +1,6 @@ -use super::{Cmd, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FACTOR, FALSE, HEAD, HOSTNAME, NOLOGIN, SLEEP, TRUE}; +use super::{ + Cmd, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FACTOR, FALSE, HEAD, HOSTNAME, NOLOGIN, SLEEP, TRUE, +}; use clap::Command; use std::{ error::Error, @@ -45,9 +47,7 @@ impl Cmd for Shitbox { } fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { - let matches = if let Some(m) = matches { - m - } else { + let Some(matches) = matches else { return Err(Box::new(io::Error::new(ErrorKind::Other, "No input"))); }; match matches.subcommand() { diff --git a/src/lib.rs b/src/lib.rs index bc43576..e6e6548 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,10 @@ use std::{env, error::Error, path::PathBuf, string::ToString}; pub mod cmd; use cmd::{ - Cmd, Commands, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, FACTOR, HEAD, HOSTNAME, NOLOGIN, SHITBOX, SLEEP, TRUE, + Cmd, Commands, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FACTOR, FALSE, HEAD, HOSTNAME, NOLOGIN, + SHITBOX, SLEEP, TRUE, }; +pub mod math; #[derive(Debug, Clone, Copy)] pub enum Path { @@ -39,8 +41,8 @@ pub fn run() -> Result<(), Box> { cmd::COMMANDS .set(Commands { items: vec![ - &BASE_32, &BOOTSTRAP, &DIRNAME, &ECHO, &FALSE, &FACTOR, &HEAD, &HOSTNAME, &NOLOGIN, &TRUE, &SLEEP, - &SHITBOX, + &BASE_32, &BOOTSTRAP, &DIRNAME, &ECHO, &FALSE, &FACTOR, &HEAD, &HOSTNAME, + &NOLOGIN, &TRUE, &SLEEP, &SHITBOX, ], }) .expect("Cannot register commands"); diff --git a/src/math.rs b/src/math.rs new file mode 100644 index 0000000..0f3677c --- /dev/null +++ b/src/math.rs @@ -0,0 +1,18 @@ +#[must_use] +pub fn is_prime(num: u64) -> bool { + match num { + 0 | 1 => false, + 2 | 3 | 5 | 7 | 11 | 13 => true, + x if x % 2 == 0 => false, + _ => { + let mut x: u64 = 2; + while x < num / 2 { + if num % x == 0 { + return false; + } + x += 1; + } + true + } + } +}