mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
Remove as_i64
and as_f64
(#14258)
# Description Turns out there are duplicate conversion functions: `as_i64` and `as_f64`. In most cases, these can be replaced with `as_int` and `as_float`, respectively.
This commit is contained in:
parent
1e051e573d
commit
e87a35104a
@ -214,7 +214,7 @@ fn item_from_record(mut rec: Record, span: Span) -> Result<HistoryItem, ShellErr
|
||||
start_timestamp: get(rec, fields::START_TIMESTAMP, |v| Ok(v.as_date()?.to_utc()))?,
|
||||
hostname: get(rec, fields::HOSTNAME, |v| Ok(v.as_str()?.to_owned()))?,
|
||||
cwd: get(rec, fields::CWD, |v| Ok(v.as_str()?.to_owned()))?,
|
||||
exit_status: get(rec, fields::EXIT_STATUS, |v| v.as_i64())?,
|
||||
exit_status: get(rec, fields::EXIT_STATUS, |v| v.as_int())?,
|
||||
duration: get(rec, fields::DURATION, duration_from_value)?,
|
||||
more_info: None,
|
||||
// TODO: Currently reedline doesn't let you create session IDs.
|
||||
|
@ -760,7 +760,7 @@ fn fill_in_result_related_history_metadata(
|
||||
c.duration = Some(cmd_duration);
|
||||
c.exit_status = stack
|
||||
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
||||
.and_then(|e| e.as_i64().ok());
|
||||
.and_then(|e| e.as_int().ok());
|
||||
c
|
||||
})
|
||||
.into_diagnostic()?; // todo: don't stop repl if error here?
|
||||
@ -1247,7 +1247,7 @@ fn get_command_finished_marker(
|
||||
) -> 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
|
||||
|
@ -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(
|
||||
|
@ -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<Value, ShellError> {
|
||||
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,
|
||||
};
|
||||
|
||||
|
@ -1,29 +0,0 @@
|
||||
use crate::{ShellError, Value};
|
||||
|
||||
impl Value {
|
||||
pub fn as_f64(&self) -> Result<f64, ShellError> {
|
||||
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<i64, ShellError> {
|
||||
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,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
mod custom_value;
|
||||
mod duration;
|
||||
mod filesize;
|
||||
mod from;
|
||||
mod from_value;
|
||||
mod glob;
|
||||
mod into_value;
|
||||
|
@ -47,9 +47,9 @@ impl PluginCommand for Sum {
|
||||
) -> Result<PipelineData, LabeledError> {
|
||||
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")
|
||||
|
@ -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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
let series_values: Result<Vec<_>, _> = 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<Serie
|
||||
.iter()
|
||||
.map(|v| {
|
||||
value_to_option(v, |v| {
|
||||
v.as_i64().map(|v| nanos_from_timeunit(v, *time_unit))
|
||||
v.as_duration().map(|v| nanos_from_timeunit(v, *time_unit))
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
@ -27,7 +27,7 @@ fn find_id(
|
||||
let block = parse(working_set, Some(file_path), file, false);
|
||||
let flattened = flatten_block(working_set, &block);
|
||||
|
||||
if let Ok(location) = location.as_i64() {
|
||||
if let Ok(location) = location.as_int() {
|
||||
let location = location as usize + offset;
|
||||
for item in flattened {
|
||||
if location >= 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<EngineState>, 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,
|
||||
|
Loading…
Reference in New Issue
Block a user