diff --git a/crates/nu-cmd-extra/src/extra/filters/update_cells.rs b/crates/nu-cmd-extra/src/extra/filters/update_cells.rs index d0b0eab194..a27cafa78b 100644 --- a/crates/nu-cmd-extra/src/extra/filters/update_cells.rs +++ b/crates/nu-cmd-extra/src/extra/filters/update_cells.rs @@ -158,7 +158,7 @@ impl Iterator for UpdateCellIterator { match self.input.next() { Some(val) => { if let Some(ref cols) = self.columns { - if !val.columns().iter().any(|c| cols.contains(c)) { + if !val.columns().any(|c| cols.contains(c)) { return Some(val); } } diff --git a/crates/nu-command/src/conversions/into/value.rs b/crates/nu-command/src/conversions/into/value.rs index e209b0ad9f..f35e56f5f4 100644 --- a/crates/nu-command/src/conversions/into/value.rs +++ b/crates/nu-command/src/conversions/into/value.rs @@ -99,7 +99,7 @@ impl Iterator for UpdateCellIterator { match self.input.next() { Some(val) => { if let Some(ref cols) = self.columns { - if !val.columns().iter().any(|c| cols.contains(c)) { + if !val.columns().any(|c| cols.contains(c)) { return Some(val); } } diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 1b5728acd7..034e6c5b33 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -190,7 +190,7 @@ pub fn value_to_string( Value::Int { val, .. } => Ok(format!("{}", *val)), Value::List { vals, .. } => { let headers = get_columns(vals); - if !headers.is_empty() && vals.iter().all(|x| x.columns() == headers) { + if !headers.is_empty() && vals.iter().all(|x| x.columns().eq(headers.iter())) { // Table output let headers: Vec = headers .iter() diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 6475c2111c..447eba1976 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -257,7 +257,7 @@ fn convert_to_list( let mut iter = iter.into_iter().peekable(); if let Some(first) = iter.peek() { - let mut headers = first.columns().to_vec(); + let mut headers: Vec = first.columns().cloned().collect(); if !headers.is_empty() { headers.insert(0, "#".into()); diff --git a/crates/nu-protocol/src/did_you_mean.rs b/crates/nu-protocol/src/did_you_mean.rs index 4996c83282..1e97d1dc13 100644 --- a/crates/nu-protocol/src/did_you_mean.rs +++ b/crates/nu-protocol/src/did_you_mean.rs @@ -1,5 +1,9 @@ -pub fn did_you_mean>(possibilities: &[S], input: &str) -> Option { - let possibilities: Vec<&str> = possibilities.iter().map(|s| s.as_ref()).collect(); +pub fn did_you_mean<'a, 'b, I, S>(possibilities: I, input: &'b str) -> Option +where + I: IntoIterator, + S: AsRef + 'a + ?Sized, +{ + let possibilities: Vec<&str> = possibilities.into_iter().map(|s| s.as_ref()).collect(); let suggestion = crate::lev_distance::find_best_match_for_name_with_substrings(&possibilities, input, None) .map(|s| s.to_string()); diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index bea42a16b5..a271a63c92 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -1035,7 +1035,9 @@ impl Value { return Ok(Value::nothing(*origin_span)); // short-circuit } else { if from_user_input { - if let Some(suggestion) = did_you_mean(&val.cols, column_name) { + if let Some(suggestion) = + did_you_mean(val.columns(), column_name) + { return Err(ShellError::DidYouMean( suggestion, *origin_span, @@ -1420,19 +1422,7 @@ impl Value { match val { Value::Record { val: record, .. } => { - let mut found = false; - let mut index = 0; - record.cols.retain_mut(|col| { - if col == col_name { - found = true; - record.vals.remove(index); - false - } else { - index += 1; - true - } - }); - if !found && !optional { + if record.remove(col_name).is_none() && !optional { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, @@ -1452,19 +1442,7 @@ impl Value { Ok(()) } Value::Record { val: record, .. } => { - let mut found = false; - let mut index = 0; - record.cols.retain_mut(|col| { - if col == col_name { - found = true; - record.vals.remove(index); - false - } else { - index += 1; - true - } - }); - if !found && !optional { + if record.remove(col_name).is_none() && !optional { return Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, @@ -1741,11 +1719,13 @@ impl Value { matches!(self, Value::Bool { val: false, .. }) } - pub fn columns(&self) -> &[String] { - match self { - Value::Record { val, .. } => &val.cols, - _ => &[], - } + pub fn columns(&self) -> impl Iterator { + let opt = match self { + Value::Record { val, .. } => Some(val.columns()), + _ => None, + }; + + opt.into_iter().flatten() } pub fn bool(val: bool, span: Span) -> Value {