From bd4d6fcac5e7d817c1bddf0ddc89e486c25f6d70 Mon Sep 17 00:00:00 2001 From: nicoo Date: Wed, 24 Jun 2020 13:37:21 +0200 Subject: [PATCH] factor: Split the CLI and I/O code off the factoring logic --- src/uu/factor/Cargo.toml | 6 ++-- src/uu/factor/src/cli.rs | 63 +++++++++++++++++++++++++++++++++++ src/uu/factor/src/factor.rs | 65 ++++--------------------------------- 3 files changed, 73 insertions(+), 61 deletions(-) create mode 100644 src/uu/factor/src/cli.rs diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index 7589fe1bb..5631a52da 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -11,9 +11,6 @@ keywords = ["coreutils", "uutils", "cross-platform", "cli", "utility"] categories = ["command-line-utilities"] edition = "2018" -[lib] -path = "src/factor.rs" - [dependencies] rand = "0.5" uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary" } @@ -22,3 +19,6 @@ uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.co [[bin]] name = "factor" path = "src/main.rs" + +[lib] +path = "src/cli.rs" diff --git a/src/uu/factor/src/cli.rs b/src/uu/factor/src/cli.rs new file mode 100644 index 000000000..628448964 --- /dev/null +++ b/src/uu/factor/src/cli.rs @@ -0,0 +1,63 @@ +// * This file is part of the uutils coreutils package. +// * +// * (c) 2014 T. Jameson Little +// * (c) 2020 nicoo +// * +// * For the full copyright and license information, please view the LICENSE file +// * that was distributed with this source code. + +#[macro_use] +extern crate uucore; + +use std::error::Error; +use std::io::{self, stdin, stdout, BufRead, Write}; + +mod factor; +pub(crate) use factor::*; + +mod miller_rabin; +mod numeric; +mod rho; +mod table; + +static SYNTAX: &str = "[OPTION] [NUMBER]..."; +static SUMMARY: &str = "Print the prime factors of the given number(s). + If none are specified, read from standard input."; +static LONG_HELP: &str = ""; + +fn print_factors_str(num_str: &str, w: &mut impl io::Write) -> Result<(), Box> { + num_str + .parse::() + .map_err(|e| e.into()) + .and_then(|x| writeln!(w, "{}:{}", x, factor(x)).map_err(|e| e.into())) +} + +pub fn uumain(args: impl uucore::Args) -> i32 { + let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); + let stdout = stdout(); + let mut w = io::BufWriter::new(stdout.lock()); + + if matches.free.is_empty() { + let stdin = stdin(); + + for line in stdin.lock().lines() { + for number in line.unwrap().split_whitespace() { + if let Err(e) = print_factors_str(number, &mut w) { + show_warning!("{}: {}", number, e); + } + } + } + } else { + for number in &matches.free { + if let Err(e) = print_factors_str(number, &mut w) { + show_warning!("{}: {}", number, e); + } + } + } + + if let Err(e) = w.flush() { + show_error!("{}", e); + } + + 0 +} diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index d56488793..f825c03ab 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -1,6 +1,5 @@ // * This file is part of the uutils coreutils package. // * -// * (c) 2014 T. Jameson Little // * (c) 2020 nicoo // * // * For the full copyright and license information, please view the LICENSE file @@ -8,48 +7,35 @@ extern crate rand; -#[macro_use] -extern crate uucore; - use std::collections::BTreeMap; -use std::error::Error; use std::fmt; -use std::io::{self, stdin, stdout, BufRead, Write}; use std::ops; -mod miller_rabin; -mod numeric; -mod rho; -mod table; +use crate::{miller_rabin, rho, table}; -static SYNTAX: &str = "[OPTION] [NUMBER]..."; -static SUMMARY: &str = "Print the prime factors of the given number(s). - If none are specified, read from standard input."; -static LONG_HELP: &str = ""; - -struct Factors { +pub struct Factors { f: BTreeMap, } impl Factors { - fn one() -> Factors { + pub fn one() -> Factors { Factors { f: BTreeMap::new() } } - fn prime(p: u64) -> Factors { + pub fn prime(p: u64) -> Factors { debug_assert!(miller_rabin::is_prime(p)); let mut f = Factors::one(); f.push(p); f } - fn add(&mut self, prime: u64, exp: u8) { + pub fn add(&mut self, prime: u64, exp: u8) { debug_assert!(exp > 0); let n = *self.f.get(&prime).unwrap_or(&0); self.f.insert(prime, exp + n); } - fn push(&mut self, prime: u64) { + pub fn push(&mut self, prime: u64) { self.add(prime, 1) } @@ -81,7 +67,7 @@ impl fmt::Display for Factors { } } -fn factor(mut n: u64) -> Factors { +pub fn factor(mut n: u64) -> Factors { let mut factors = Factors::one(); if n < 2 { @@ -109,43 +95,6 @@ fn factor(mut n: u64) -> Factors { factors } -fn print_factors_str(num_str: &str, w: &mut impl io::Write) -> Result<(), Box> { - num_str - .parse::() - .map_err(|e| e.into()) - .and_then(|x| writeln!(w, "{}:{}", x, factor(x)).map_err(|e| e.into())) -} - -pub fn uumain(args: impl uucore::Args) -> i32 { - let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); - let stdout = stdout(); - let mut w = io::BufWriter::new(stdout.lock()); - - if matches.free.is_empty() { - let stdin = stdin(); - - for line in stdin.lock().lines() { - for number in line.unwrap().split_whitespace() { - if let Err(e) = print_factors_str(number, &mut w) { - show_warning!("{}: {}", number, e); - } - } - } - } else { - for number in &matches.free { - if let Err(e) = print_factors_str(number, &mut w) { - show_warning!("{}: {}", number, e); - } - } - } - - if let Err(e) = w.flush() { - show_error!("{}", e); - } - - 0 -} - #[cfg(test)] mod tests { use super::factor;