From 6f3324e624dbbdea7e37c8e9d912037c54be191e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Wed, 3 Mar 2021 11:41:52 +0100 Subject: [PATCH] Add feature for imports. --- TODO | 3 --- src/main.rs | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/TODO b/TODO index 9a61e12..db6dd5d 100644 --- a/TODO +++ b/TODO @@ -8,6 +8,3 @@ Technical: Documentation: - Write man page - Create `help` option - -Interface: - - Option to import numbers from a file (exports with piping) diff --git a/src/main.rs b/src/main.rs index 1c82b57..19a064d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,17 +16,18 @@ * along with this program. If not, see . */ -//use std::env; use structopt::StructOpt; use std::path::PathBuf; +use std::fs::File; +use std::io::{BufRead, BufReader}; use std::collections::VecDeque; #[derive(StructOpt)] struct Opt { #[structopt(short, long)] verbose:bool, - //#[structopt(short, long)] - //import:Option, + #[structopt(short, long)] + import:Option, n:usize, } @@ -36,14 +37,31 @@ fn main() { // get the first `n` primes let n = opts.n; let mut primes:VecDeque = VecDeque::with_capacity(n); - // first prime - if opts.verbose - { - println!("{}", 2); - } - primes.push_back(2); + let mut candidate:u64; - let mut candidate:u64 = 3; + if opts.import.is_some() + { + let in_file = File::open(opts.import.unwrap()).unwrap(); + let reader = BufReader::new(in_file); + for (index, line) in reader.lines().enumerate() + { + let line = line.unwrap(); + let aux:u64 = line.parse().unwrap(); + primes.push_back(aux); + } + + candidate = *primes.back().unwrap() + 2; + } + else + { + // first prime + if opts.verbose + { + println!("{}", 2); + } + primes.push_back(2); + candidate = 3; + } while primes.len() < n { @@ -71,6 +89,6 @@ fn main() { if !opts.verbose { - println!("{}", primes.back().unwrap()); + println!("{}", primes.get(n-1).unwrap()); } }