|
| 1 | +use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs}; |
| 2 | +use nu_engine::CallExt; |
| 3 | +use nu_protocol::{ |
| 4 | + ast::{Call, CellPath}, |
| 5 | + engine::{Command, EngineState, Stack}, |
| 6 | + Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value, |
| 7 | +}; |
| 8 | + |
| 9 | +#[derive(Clone)] |
| 10 | +pub struct SubCommand; |
| 11 | + |
| 12 | +impl Command for SubCommand { |
| 13 | + fn name(&self) -> &str { |
| 14 | + "into float" |
| 15 | + } |
| 16 | + |
| 17 | + fn signature(&self) -> Signature { |
| 18 | + Signature::build("into float") |
| 19 | + .input_output_types(vec![ |
| 20 | + (Type::Int, Type::Float), |
| 21 | + (Type::String, Type::Float), |
| 22 | + (Type::Bool, Type::Float), |
| 23 | + (Type::Float, Type::Float), |
| 24 | + (Type::Table(vec![]), Type::Table(vec![])), |
| 25 | + (Type::Record(vec![]), Type::Record(vec![])), |
| 26 | + ( |
| 27 | + Type::List(Box::new(Type::Any)), |
| 28 | + Type::List(Box::new(Type::Float)), |
| 29 | + ), |
| 30 | + ]) |
| 31 | + .rest( |
| 32 | + "rest", |
| 33 | + SyntaxShape::CellPath, |
| 34 | + "for a data structure input, convert data at the given cell paths", |
| 35 | + ) |
| 36 | + .allow_variants_without_examples(true) |
| 37 | + .category(Category::Conversions) |
| 38 | + } |
| 39 | + |
| 40 | + fn usage(&self) -> &str { |
| 41 | + "Convert data into floating point number." |
| 42 | + } |
| 43 | + |
| 44 | + fn search_terms(&self) -> Vec<&str> { |
| 45 | + vec!["convert", "number", "floating", "decimal"] |
| 46 | + } |
| 47 | + |
| 48 | + fn run( |
| 49 | + &self, |
| 50 | + engine_state: &EngineState, |
| 51 | + stack: &mut Stack, |
| 52 | + call: &Call, |
| 53 | + input: PipelineData, |
| 54 | + ) -> Result<PipelineData, ShellError> { |
| 55 | + let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?; |
| 56 | + let args = CellPathOnlyArgs::from(cell_paths); |
| 57 | + operate(action, args, input, call.head, engine_state.ctrlc.clone()) |
| 58 | + } |
| 59 | + |
| 60 | + fn examples(&self) -> Vec<Example> { |
| 61 | + vec![ |
| 62 | + Example { |
| 63 | + description: "Convert string to float in table", |
| 64 | + example: "[[num]; ['5.01']] | into float num", |
| 65 | + result: Some(Value::test_list(vec![Value::test_record(Record { |
| 66 | + cols: vec!["num".to_string()], |
| 67 | + vals: vec![Value::test_float(5.01)], |
| 68 | + })])), |
| 69 | + }, |
| 70 | + Example { |
| 71 | + description: "Convert string to floating point number", |
| 72 | + example: "'1.345' | into float", |
| 73 | + result: Some(Value::test_float(1.345)), |
| 74 | + }, |
| 75 | + Example { |
| 76 | + description: "Coerce list of ints and floats to float", |
| 77 | + example: "[4 -5.9] | into float", |
| 78 | + result: Some(Value::test_list(vec![ |
| 79 | + Value::test_float(4.0), |
| 80 | + Value::test_float(-5.9), |
| 81 | + ])), |
| 82 | + }, |
| 83 | + Example { |
| 84 | + description: "Convert boolean to float", |
| 85 | + example: "true | into float", |
| 86 | + result: Some(Value::test_float(1.0)), |
| 87 | + }, |
| 88 | + ] |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value { |
| 93 | + let span = input.span(); |
| 94 | + match input { |
| 95 | + Value::Float { .. } => input.clone(), |
| 96 | + Value::String { val: s, .. } => { |
| 97 | + let other = s.trim(); |
| 98 | + |
| 99 | + match other.parse::<f64>() { |
| 100 | + Ok(x) => Value::float(x, head), |
| 101 | + Err(reason) => Value::error( |
| 102 | + ShellError::CantConvert { |
| 103 | + to_type: "float".to_string(), |
| 104 | + from_type: reason.to_string(), |
| 105 | + span, |
| 106 | + help: None, |
| 107 | + }, |
| 108 | + span, |
| 109 | + ), |
| 110 | + } |
| 111 | + } |
| 112 | + Value::Int { val: v, .. } => Value::float(*v as f64, span), |
| 113 | + Value::Bool { val: b, .. } => Value::float( |
| 114 | + match b { |
| 115 | + true => 1.0, |
| 116 | + false => 0.0, |
| 117 | + }, |
| 118 | + span, |
| 119 | + ), |
| 120 | + // Propagate errors by explicitly matching them before the final case. |
| 121 | + Value::Error { .. } => input.clone(), |
| 122 | + other => Value::error( |
| 123 | + ShellError::OnlySupportsThisInputType { |
| 124 | + exp_input_type: "string, integer or bool".into(), |
| 125 | + wrong_type: other.get_type().to_string(), |
| 126 | + dst_span: head, |
| 127 | + src_span: other.span(), |
| 128 | + }, |
| 129 | + head, |
| 130 | + ), |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#[cfg(test)] |
| 135 | +mod tests { |
| 136 | + use super::*; |
| 137 | + use nu_protocol::Type::Error; |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_examples() { |
| 141 | + use crate::test_examples; |
| 142 | + |
| 143 | + test_examples(SubCommand {}) |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + #[allow(clippy::approx_constant)] |
| 148 | + fn string_to_decimal() { |
| 149 | + let word = Value::test_string("3.1415"); |
| 150 | + let expected = Value::test_float(3.1415); |
| 151 | + |
| 152 | + let actual = action(&word, &CellPathOnlyArgs::from(vec![]), Span::test_data()); |
| 153 | + assert_eq!(actual, expected); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn communicates_parsing_error_given_an_invalid_decimallike_string() { |
| 158 | + let invalid_str = Value::test_string("11.6anra"); |
| 159 | + |
| 160 | + let actual = action( |
| 161 | + &invalid_str, |
| 162 | + &CellPathOnlyArgs::from(vec![]), |
| 163 | + Span::test_data(), |
| 164 | + ); |
| 165 | + |
| 166 | + assert_eq!(actual.get_type(), Error); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn int_to_float() { |
| 171 | + let input_int = Value::test_int(10); |
| 172 | + let expected = Value::test_float(10.0); |
| 173 | + let actual = action( |
| 174 | + &input_int, |
| 175 | + &CellPathOnlyArgs::from(vec![]), |
| 176 | + Span::test_data(), |
| 177 | + ); |
| 178 | + |
| 179 | + assert_eq!(actual, expected); |
| 180 | + } |
| 181 | +} |
0 commit comments