From c810f5e3daf7902898d38a0d168ef27301e81145 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Wed, 20 Mar 2024 02:22:25 -0700 Subject: [PATCH] Refactor `PluginCustomValue::render_to_base_value_in` (#12244) # Description Changed to use `Value::recurse_mut` like the other functions. # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting --- .../src/protocol/plugin_custom_value.rs | 56 +++++-------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/crates/nu-plugin/src/protocol/plugin_custom_value.rs b/crates/nu-plugin/src/protocol/plugin_custom_value.rs index 60a54a1077..a0d1a2f127 100644 --- a/crates/nu-plugin/src/protocol/plugin_custom_value.rs +++ b/crates/nu-plugin/src/protocol/plugin_custom_value.rs @@ -370,49 +370,21 @@ impl PluginCustomValue { /// Render any custom values in the `Value` using `to_base_value()` pub(crate) fn render_to_base_value_in(value: &mut Value) -> Result<(), ShellError> { - let span = value.span(); - match value { - Value::CustomValue { ref val, .. } => { - *value = val.to_base_value(span)?; - Ok(()) + value.recurse_mut(&mut |value| { + let span = value.span(); + match value { + Value::CustomValue { ref val, .. } => { + *value = val.to_base_value(span)?; + Ok(()) + } + // Collect LazyRecord before proceeding + Value::LazyRecord { ref val, .. } => { + *value = val.collect()?; + Ok(()) + } + _ => Ok(()), } - // Any values that can contain other values need to be handled recursively - Value::Range { ref mut val, .. } => { - Self::render_to_base_value_in(&mut val.from)?; - Self::render_to_base_value_in(&mut val.to)?; - Self::render_to_base_value_in(&mut val.incr) - } - Value::Record { ref mut val, .. } => val - .iter_mut() - .try_for_each(|(_, rec_value)| Self::render_to_base_value_in(rec_value)), - Value::List { ref mut vals, .. } => { - vals.iter_mut().try_for_each(Self::render_to_base_value_in) - } - Value::Closure { ref mut val, .. } => val - .captures - .iter_mut() - .map(|(_, captured_value)| captured_value) - .try_for_each(Self::render_to_base_value_in), - // All of these don't contain other values - Value::Bool { .. } - | Value::Int { .. } - | Value::Float { .. } - | Value::Filesize { .. } - | Value::Duration { .. } - | Value::Date { .. } - | Value::String { .. } - | Value::Glob { .. } - | Value::Block { .. } - | Value::Nothing { .. } - | Value::Error { .. } - | Value::Binary { .. } - | Value::CellPath { .. } => Ok(()), - // Collect any lazy records that exist and try again - Value::LazyRecord { val, .. } => { - *value = val.collect()?; - Self::render_to_base_value_in(value) - } - } + }) } }