diff --git a/crates/nu-cli/src/commands/history/history_import.rs b/crates/nu-cli/src/commands/history/history_import.rs index c59d501f2f..d56bc1da03 100644 --- a/crates/nu-cli/src/commands/history/history_import.rs +++ b/crates/nu-cli/src/commands/history/history_import.rs @@ -214,7 +214,7 @@ fn item_from_record(mut rec: Record, span: Span) -> Result String { let exit_code = stack .get_env_var(engine_state, "LAST_EXIT_CODE") - .and_then(|e| e.as_i64().ok()); + .and_then(|e| e.as_int().ok()); if shell_integration_osc633 { if stack diff --git a/crates/nu-command/src/conversions/into/int.rs b/crates/nu-command/src/conversions/into/int.rs index 0792719fef..f55770e7ff 100644 --- a/crates/nu-command/src/conversions/into/int.rs +++ b/crates/nu-command/src/conversions/into/int.rs @@ -259,7 +259,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { if radix == 10 { *val as i64 } else { - match convert_int(&Value::int(*val as i64, span), span, radix).as_i64() { + match convert_int(&Value::int(*val as i64, span), span, radix).as_int() { Ok(v) => v, _ => { return Value::error( diff --git a/crates/nu-command/src/generators/seq_date.rs b/crates/nu-command/src/generators/seq_date.rs index 955de5a507..7369878d1d 100644 --- a/crates/nu-command/src/generators/seq_date.rs +++ b/crates/nu-command/src/generators/seq_date.rs @@ -1,5 +1,6 @@ use chrono::{Duration, Local, NaiveDate}; use nu_engine::command_prelude::*; +use nu_protocol::FromValue; use std::fmt::Write; @@ -187,13 +188,14 @@ pub fn run_seq_dates( ) -> Result { let today = Local::now().date_naive(); // if cannot convert , it will return error - let mut step_size: i64 = increment.as_i64()?; + let increment_span = increment.span(); + let mut step_size: i64 = i64::from_value(increment)?; if step_size == 0 { return Err(ShellError::GenericError { error: "increment cannot be 0".into(), msg: "increment cannot be 0".into(), - span: Some(increment.span()), + span: Some(increment_span), help: None, inner: vec![], }); @@ -264,7 +266,7 @@ pub fn run_seq_dates( }; let mut days_to_output = match day_count { - Some(d) => d.as_i64()?, + Some(d) => i64::from_value(d)?, None => 0i64, }; diff --git a/crates/nu-protocol/src/value/from.rs b/crates/nu-protocol/src/value/from.rs deleted file mode 100644 index 9519f58156..0000000000 --- a/crates/nu-protocol/src/value/from.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::{ShellError, Value}; - -impl Value { - pub fn as_f64(&self) -> Result { - match self { - Value::Float { val, .. } => Ok(*val), - x => Err(ShellError::CantConvert { - to_type: "f64".into(), - from_type: x.get_type().to_string(), - span: self.span(), - help: None, - }), - } - } - - pub fn as_i64(&self) -> Result { - match self { - Value::Int { val, .. } => Ok(*val), - Value::Filesize { val, .. } => Ok(*val), - Value::Duration { val, .. } => Ok(*val), - x => Err(ShellError::CantConvert { - to_type: "i64".into(), - from_type: x.get_type().to_string(), - span: self.span(), - help: None, - }), - } - } -} diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 29f75d157d..26d8384cee 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -1,7 +1,6 @@ mod custom_value; mod duration; mod filesize; -mod from; mod from_value; mod glob; mod into_value; diff --git a/crates/nu_plugin_example/src/commands/sum.rs b/crates/nu_plugin_example/src/commands/sum.rs index e58f27420c..afa46e1f62 100644 --- a/crates/nu_plugin_example/src/commands/sum.rs +++ b/crates/nu_plugin_example/src/commands/sum.rs @@ -47,9 +47,9 @@ impl PluginCommand for Sum { ) -> Result { let mut acc = IntOrFloat::Int(0); for value in input { - if let Ok(n) = value.as_i64() { + if let Ok(n) = value.as_int() { acc.add_i64(n); - } else if let Ok(n) = value.as_f64() { + } else if let Ok(n) = value.as_float() { acc.add_f64(n); } else { return Err(LabeledError::new("Sum only accepts ints and floats") 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 54bcbde843..a920aac0a3 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,34 +34,34 @@ const VALUES_CAPACITY: usize = 10; macro_rules! value_to_primitive { ($value:ident, u8) => { - $value.as_i64().map(|v| v as u8) + $value.as_int().map(|v| v as u8) }; ($value:ident, u16) => { - $value.as_i64().map(|v| v as u16) + $value.as_int().map(|v| v as u16) }; ($value:ident, u32) => { - $value.as_i64().map(|v| v as u32) + $value.as_int().map(|v| v as u32) }; ($value:ident, u64) => { - $value.as_i64().map(|v| v as u64) + $value.as_int().map(|v| v as u64) }; ($value:ident, i8) => { - $value.as_i64().map(|v| v as i8) + $value.as_int().map(|v| v as i8) }; ($value:ident, i16) => { - $value.as_i64().map(|v| v as i16) + $value.as_int().map(|v| v as i16) }; ($value:ident, i32) => { - $value.as_i64().map(|v| v as i32) + $value.as_int().map(|v| v as i32) }; ($value:ident, i64) => { - $value.as_i64() + $value.as_int() }; ($value:ident, f32) => { - $value.as_f64().map(|v| v as f32) + $value.as_float().map(|v| v as f32) }; ($value:ident, f64) => { - $value.as_f64() + $value.as_float() }; } @@ -331,7 +331,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as u8))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u8))) .collect(); Ok(Series::new(name, series_values?)) } @@ -339,7 +339,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as u16))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u16))) .collect(); Ok(Series::new(name, series_values?)) } @@ -347,7 +347,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as u32))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u32))) .collect(); Ok(Series::new(name, series_values?)) } @@ -355,7 +355,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as u64))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as u64))) .collect(); Ok(Series::new(name, series_values?)) } @@ -363,7 +363,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as i8))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as i8))) .collect(); Ok(Series::new(name, series_values?)) } @@ -371,7 +371,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as i16))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as i16))) .collect(); Ok(Series::new(name, series_values?)) } @@ -379,7 +379,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64().map(|v| v as i32))) + .map(|v| value_to_option(v, |v| v.as_int().map(|v| v as i32))) .collect(); Ok(Series::new(name, series_values?)) } @@ -387,7 +387,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| value_to_option(v, |v| v.as_i64())) + .map(|v| value_to_option(v, |v| v.as_int())) .collect(); Ok(Series::new(name, series_values?)) } @@ -419,7 +419,7 @@ fn typed_column_to_series(name: PlSmallStr, column: TypedColumn) -> Result= item.0.start && location < item.0.end { @@ -80,7 +80,7 @@ pub fn check(engine_state: &mut EngineState, file_path: &str, max_errors: &Value let mut working_set = StateWorkingSet::new(engine_state); let file = std::fs::read(file_path); - let max_errors = if let Ok(max_errors) = max_errors.as_i64() { + let max_errors = if let Ok(max_errors) = max_errors.as_int() { max_errors as usize } else { 100 @@ -603,7 +603,7 @@ pub fn complete(engine_reference: Arc, file_path: &str, location: & std::process::exit(1); }); - if let Ok(location) = location.as_i64() { + if let Ok(location) = location.as_int() { let results = completer.complete( &String::from_utf8_lossy(&file)[..location as usize], location as usize,