diff options
author | Ryan Geary <rtgnj42@gmail.com> | 2019-09-17 22:30:10 -0400 |
---|---|---|
committer | Ryan Geary <rtgnj42@gmail.com> | 2019-09-17 23:40:35 -0400 |
commit | 4afdc50158ff090fa0b3387b3565cf0a2efc7f11 (patch) | |
tree | d67b66ccd2b2d225ecb5fb933c9c6ce8ac7cc1cc | |
parent | 5703ed02a134ca131cdf212d74431c41bfbddfb6 (diff) |
Separate slice finding from printing
-rw-r--r-- | src/choice.rs | 59 |
1 files changed, 22 insertions, 37 deletions
diff --git a/src/choice.rs b/src/choice.rs index 7cf0980..e6dcf59 100644 --- a/src/choice.rs +++ b/src/choice.rs @@ -78,8 +78,6 @@ mod tests { //} } - - } use regex::Regex; @@ -125,6 +123,10 @@ pub enum Choice { impl Choice { pub fn print_choice(&self, line: &String, opt: &Opt) { + print!("{}", self.get_choice_slice(line, opt).join(" ")); + } + + fn get_choice_slice<'a>(&self, line: &'a String, opt: &Opt) -> Vec<&'a str> { let re = Regex::new(match &opt.field_separator { Some(s) => s, None => "[[:space:]]", @@ -142,39 +144,26 @@ impl Choice { .enumerate(); match self { - Choice::Field(i) => { - print!( - "{} ", - words - .filter(|x| x.0 == *i as usize) - .map(|x| x.1) - .collect::<String>() - ); - } + Choice::Field(i) => words + .filter(|x| x.0 == *i as usize) + .map(|x| x.1) + .collect::<Vec<&str>>(), Choice::FieldRange(r) => match r { - (None, None) => print!("{}", words.map(|x| x.1).collect::<String>()), - (Some(start), None) => print!( - "{} ", - words - .filter(|x| x.0 >= (*start).try_into().unwrap()) - .map(|x| x.1) - .collect::<Vec<&str>>() - .join(" ") - ), + (None, None) => words.map(|x| x.1).collect::<Vec<&str>>(), + (Some(start), None) => words + .filter(|x| x.0 >= (*start).try_into().unwrap()) + .map(|x| x.1) + .collect::<Vec<&str>>(), (None, Some(end)) => { let e: usize = if opt.inclusive { (end + 1).try_into().unwrap() } else { (*end).try_into().unwrap() }; - print!( - "{} ", - words - .filter(|x| x.0 < e) - .map(|x| x.1) - .collect::<Vec<&str>>() - .join(" ") - ) + words + .filter(|x| x.0 < e) + .map(|x| x.1) + .collect::<Vec<&str>>() } (Some(start), Some(end)) => { let e: usize = if opt.inclusive { @@ -182,17 +171,13 @@ impl Choice { } else { (*end).try_into().unwrap() }; - print!( - "{} ", - words - .filter(|x| x.0 < e && x.0 >= (*start).try_into().unwrap()) - .map(|x| x.1) - .collect::<Vec<&str>>() - .join(" ") - ) + words + .filter(|x| x.0 < e && x.0 >= (*start).try_into().unwrap()) + .map(|x| x.1) + .collect::<Vec<&str>>() } }, - }; + } } pub fn parse_choice(src: &str) -> Result<Choice, ParseIntError> { |