From c63bb81c3e6af38e4b528aac7eb6d491f0132758 Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Tue, 3 Dec 2024 04:08:41 -0800 Subject: [PATCH 1/2] Convert Filesize to Int (#14491) # Description Fixes the conversion of Value::Filesize to Value::Int allowing things like `ps | polars into-df` to work correctly. --- .../values/nu_dataframe/conversion.rs | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs index 77457dee0c..7d5a4ace3f 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs @@ -34,28 +34,28 @@ const VALUES_CAPACITY: usize = 10; macro_rules! value_to_primitive { ($value:ident, u8) => { - $value.as_int().map(|v| v as u8) + value_to_int($value).map(|v| v as u8) }; ($value:ident, u16) => { - $value.as_int().map(|v| v as u16) + value_to_int($value).map(|v| v as u16) }; ($value:ident, u32) => { - $value.as_int().map(|v| v as u32) + value_to_int($value).map(|v| v as u32) }; ($value:ident, u64) => { - $value.as_int().map(|v| v as u64) + value_to_int($value).map(|v| v as u64) }; ($value:ident, i8) => { - $value.as_int().map(|v| v as i8) + value_to_int($value).map(|v| v as i8) }; ($value:ident, i16) => { - $value.as_int().map(|v| v as i16) + value_to_int($value).map(|v| v as i16) }; ($value:ident, i32) => { - $value.as_int().map(|v| v as i32) + value_to_int($value).map(|v| v as i32) }; ($value:ident, i64) => { - $value.as_int() + value_to_int($value) }; ($value:ident, f32) => { $value.as_float().map(|v| v as f32) @@ -341,7 +341,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u8))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as u8))) .collect(); Ok(Series::new(name, series_values?)) } @@ -349,7 +349,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u16))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as u16))) .collect(); Ok(Series::new(name, series_values?)) } @@ -357,7 +357,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u32))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as u32))) .collect(); Ok(Series::new(name, series_values?)) } @@ -365,7 +365,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u64))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as u64))) .collect(); Ok(Series::new(name, series_values?)) } @@ -373,7 +373,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as i8))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as i8))) .collect(); Ok(Series::new(name, series_values?)) } @@ -381,7 +381,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as i16))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as i16))) .collect(); Ok(Series::new(name, series_values?)) } @@ -389,7 +389,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as i32))) + .map(|v| value_to_option(v, |v| value_to_int(v).map(|v| v as i32))) .collect(); Ok(Series::new(name, series_values?)) } @@ -397,7 +397,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_int())) + .map(|v| value_to_option(v, value_to_int)) .collect(); Ok(Series::new(name, series_values?)) } @@ -1364,6 +1364,20 @@ fn time_from_midnight(nanos: i64, span: Span) -> Result { }) } +// this takes into non-int types that we should represent as int like filesize +fn value_to_int(value: &Value) -> Result { + match value { + Value::Int { val, .. } => Ok(*val), + Value::Filesize { val, .. } => Ok((*val).into()), + _ => Err(ShellError::CantConvert { + to_type: "int".into(), + from_type: value.get_type().to_string(), + span: value.span(), + help: None, + }), + } +} + fn value_to_option(value: &Value, func: F) -> Result, ShellError> where F: FnOnce(&Value) -> Result, From a65a7df209300ed1ac6eac359d2aed795508b60a Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Tue, 3 Dec 2024 16:59:37 +0100 Subject: [PATCH 2/2] Add `remove` as a search term on `drop` commands (#14493) # Description Better discoverability of `drop` subcommands "I want to remove items by index" -> `drop nth` h/t @amtoine # User-Facing Changes More search terms --- crates/nu-command/src/filters/drop/column.rs | 2 +- crates/nu-command/src/filters/drop/drop_.rs | 2 +- crates/nu-command/src/filters/drop/nth.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/filters/drop/column.rs b/crates/nu-command/src/filters/drop/column.rs index f39e0e2276..b69a15b2d7 100644 --- a/crates/nu-command/src/filters/drop/column.rs +++ b/crates/nu-command/src/filters/drop/column.rs @@ -29,7 +29,7 @@ impl Command for DropColumn { } fn search_terms(&self) -> Vec<&str> { - vec!["delete"] + vec!["delete", "remove"] } fn run( diff --git a/crates/nu-command/src/filters/drop/drop_.rs b/crates/nu-command/src/filters/drop/drop_.rs index 67abea625d..d35e191a19 100644 --- a/crates/nu-command/src/filters/drop/drop_.rs +++ b/crates/nu-command/src/filters/drop/drop_.rs @@ -26,7 +26,7 @@ impl Command for Drop { } fn search_terms(&self) -> Vec<&str> { - vec!["delete"] + vec!["delete", "remove"] } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/filters/drop/nth.rs b/crates/nu-command/src/filters/drop/nth.rs index d2aed50332..282aa41c1d 100644 --- a/crates/nu-command/src/filters/drop/nth.rs +++ b/crates/nu-command/src/filters/drop/nth.rs @@ -32,7 +32,7 @@ impl Command for DropNth { } fn search_terms(&self) -> Vec<&str> { - vec!["delete"] + vec!["delete", "remove", "index"] } fn examples(&self) -> Vec {