Skip to content

Commit

Permalink
Small syntax and performance improvements for field selection
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzrehde committed Aug 22, 2023
1 parent ff70580 commit c825443
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/config/fields/field_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl FromStr for FieldSelections {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let ranges = s
.split(",")
.split(',')
.map(|s| {
let field: FieldSelection = s.parse()?;
let range: RangeInclusive<usize> = field.into();
Expand Down Expand Up @@ -52,13 +52,13 @@ impl FieldSelections {
impl FromStr for FieldSelection {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (start, end) = match s.split('-').collect::<Vec<_>>().as_slice() {
&[x] => {
let (start, end) = match *s.splitn(2, '-').collect::<Vec<_>>().as_slice() {
[x] => {
let x_parsed = x.parse::<usize>()?;
(x_parsed, Some(x_parsed))
}
&[x, ""] => (x.parse::<usize>()?, None),
&[x, y] => (x.parse::<usize>()?, Some(y.parse::<usize>()?)),
[x, ""] => (x.parse::<usize>()?, None),
[x, y] => (x.parse::<usize>()?, Some(y.parse::<usize>()?)),
_ => bail!(
"Failed to parse \"{}\" as field selection, expected format is a|a-b|a-",
s
Expand Down
5 changes: 3 additions & 2 deletions src/config/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ impl TableFormatter for &str {
line.split(separator)
.enumerate()
// TODO: seems inefficient, try applying selection to whole line at a time
.filter(|(idx, _)| selections.contains(*idx))
.map(|(_, field)| field)
.filter_map(|(idx, field)| {
selections.contains(idx).then_some(field)
})
.join("\t")
})
.join("\n"),
Expand Down

0 comments on commit c825443

Please sign in to comment.