nushell/crates/nu-command/src/conversions/into/int.rs

160 lines
4.4 KiB
Rust
Raw Normal View History

2021-11-05 04:59:12 +01:00
use nu_engine::CallExt;
2021-10-11 03:56:19 +02:00
use nu_protocol::{
2021-11-05 04:59:12 +01:00
ast::{Call, CellPath},
2021-10-25 18:58:58 +02:00
engine::{Command, EngineState, Stack},
2021-10-25 23:14:21 +02:00
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
2021-10-11 03:56:19 +02:00
};
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-11 03:56:19 +02:00
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"into int"
}
fn signature(&self) -> Signature {
Signature::build("into int").rest(
"rest",
SyntaxShape::CellPath,
"column paths to convert to int (for table input)",
)
}
fn usage(&self) -> &str {
"Convert value to integer"
}
fn run(
&self,
2021-10-28 06:13:10 +02:00
engine_state: &EngineState,
2021-11-05 04:59:12 +01:00
stack: &mut Stack,
2021-10-11 03:56:19 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-11-05 04:59:12 +01:00
into_int(engine_state, stack, call, input)
2021-10-11 03:56:19 +02:00
}
fn examples(&self) -> Vec<Example> {
vec![
2021-11-05 04:59:12 +01:00
Example {
description: "Convert string to integer in table",
example: "echo [[num]; ['-5'] [4] [1.5]] | into int num",
result: None,
},
2021-10-11 03:56:19 +02:00
Example {
description: "Convert string to integer",
example: "'2' | into int",
2021-10-20 07:58:25 +02:00
result: Some(Value::test_int(2)),
2021-10-11 03:56:19 +02:00
},
Example {
description: "Convert decimal to integer",
example: "5.9 | into int",
2021-10-20 07:58:25 +02:00
result: Some(Value::test_int(5)),
2021-10-11 03:56:19 +02:00
},
Example {
description: "Convert decimal string to integer",
example: "'5.9' | into int",
2021-10-20 07:58:25 +02:00
result: Some(Value::test_int(5)),
2021-10-11 03:56:19 +02:00
},
Example {
description: "Convert file size to integer",
example: "4KB | into int",
result: Some(Value::Int {
val: 4000,
span: Span::unknown(),
}),
},
Example {
description: "Convert bool to integer",
example: "[$false, $true] | into int",
2021-10-25 06:01:02 +02:00
result: Some(Value::List {
vals: vec![Value::test_int(0), Value::test_int(1)],
2021-10-11 03:56:19 +02:00
span: Span::unknown(),
}),
},
]
}
}
fn into_int(
2021-10-28 06:13:10 +02:00
engine_state: &EngineState,
2021-11-05 04:59:12 +01:00
stack: &mut Stack,
2021-10-11 03:56:19 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-10-11 03:56:19 +02:00
let head = call.head;
2021-11-05 04:59:12 +01:00
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
2021-10-11 03:56:19 +02:00
2021-10-28 06:13:10 +02:00
input.map(
move |v| {
2021-11-05 04:59:12 +01:00
if column_paths.is_empty() {
action(&v, head)
} else {
let mut ret = v;
for path in &column_paths {
let r =
ret.update_cell_path(&path.members, Box::new(move |old| action(old, head)));
if let Err(error) = r {
return Value::Error { error };
}
}
2021-10-11 03:56:19 +02:00
2021-11-05 04:59:12 +01:00
ret
}
2021-10-28 06:13:10 +02:00
},
engine_state.ctrlc.clone(),
)
2021-10-11 03:56:19 +02:00
}
2021-11-05 04:59:12 +01:00
pub fn action(input: &Value, span: Span) -> Value {
2021-10-11 03:56:19 +02:00
match input {
2021-11-05 04:59:12 +01:00
Value::Int { .. } => input.clone(),
Value::Filesize { val, .. } => Value::Int { val: *val, span },
2021-10-11 03:56:19 +02:00
Value::Float { val, .. } => Value::Int {
2021-11-05 04:59:12 +01:00
val: *val as i64,
2021-10-11 03:56:19 +02:00
span,
},
2021-11-05 04:59:12 +01:00
Value::String { val, .. } => match int_from_string(val, span) {
2021-10-11 03:56:19 +02:00
Ok(val) => Value::Int { val, span },
Err(error) => Value::Error { error },
},
Value::Bool { val, .. } => {
2021-11-05 04:59:12 +01:00
if *val {
2021-10-11 03:56:19 +02:00
Value::Int { val: 1, span }
} else {
Value::Int { val: 0, span }
}
}
_ => Value::Error {
error: ShellError::UnsupportedInput("'into int' for unsupported type".into(), span),
},
}
}
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
match a_string.parse::<i64>() {
Ok(n) => Ok(n),
Err(_) => match a_string.parse::<f64>() {
Ok(f) => Ok(f as i64),
_ => Err(ShellError::CantConvert(
"into int".into(),
"string".into(),
span,
)),
2021-10-11 03:56:19 +02:00
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}