diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_dtype/mod.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_dtype/mod.rs index 58bda94719..aacc2aee58 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_dtype/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_dtype/mod.rs @@ -2,12 +2,12 @@ pub mod custom_value; use custom_value::NuDataTypeCustomValue; use nu_protocol::{record, ShellError, Span, Value}; -use polars::prelude::{DataType, PlSmallStr, TimeUnit, UnknownKind}; +use polars::prelude::{DataType, Field, PlSmallStr, TimeUnit, UnknownKind}; use uuid::Uuid; use crate::{Cacheable, PolarsPlugin}; -use super::{nu_schema::dtype_to_value, CustomValueSupport, PolarsPluginObject, PolarsPluginType}; +use super::{CustomValueSupport, PolarsPluginObject, PolarsPluginType}; #[derive(Debug, Clone)] pub struct NuDataType { @@ -300,6 +300,18 @@ pub fn str_to_dtype(dtype: &str, span: Span) -> Result { } } +pub(crate) fn fields_to_value(fields: impl Iterator, span: Span) -> Value { + let record = fields + .map(|field| { + let col = field.name().to_string(); + let val = dtype_to_value(field.dtype(), span); + (col, val) + }) + .collect(); + + Value::record(record, Span::unknown()) +} + fn str_to_time_unit(ts_string: &str, span: Span) -> Result { match ts_string { "ms" => Ok(TimeUnit::Milliseconds), @@ -314,3 +326,10 @@ fn str_to_time_unit(ts_string: &str, span: Span) -> Result }), } } + +pub(crate) fn dtype_to_value(dtype: &DataType, span: Span) -> Value { + match dtype { + DataType::Struct(fields) => fields_to_value(fields.iter().cloned(), span), + _ => Value::string(dtype.to_string().replace('[', "<").replace(']', ">"), span), + } +} diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_schema/mod.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_schema/mod.rs index 0dbd8cdd53..87a90f7ac7 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_schema/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_schema/mod.rs @@ -9,7 +9,10 @@ use uuid::Uuid; use crate::{Cacheable, PolarsPlugin}; -use super::{str_to_dtype, CustomValueSupport, NuDataType, PolarsPluginObject, PolarsPluginType}; +use super::{ + nu_dtype::fields_to_value, str_to_dtype, CustomValueSupport, NuDataType, PolarsPluginObject, + PolarsPluginType, +}; #[derive(Debug, Clone)] pub struct NuSchema { @@ -98,25 +101,6 @@ impl CustomValueSupport for NuSchema { } } -fn fields_to_value(fields: impl Iterator, span: Span) -> Value { - let record = fields - .map(|field| { - let col = field.name().to_string(); - let val = dtype_to_value(field.dtype(), span); - (col, val) - }) - .collect(); - - Value::record(record, Span::unknown()) -} - -pub fn dtype_to_value(dtype: &DataType, span: Span) -> Value { - match dtype { - DataType::Struct(fields) => fields_to_value(fields.iter().cloned(), span), - _ => Value::string(dtype.to_string().replace('[', "<").replace(']', ">"), span), - } -} - fn value_to_schema(plugin: &PolarsPlugin, value: &Value, span: Span) -> Result { let fields = value_to_fields(plugin, value, span)?; let schema = Schema::from_iter(fields);