2020-05-26 22:19:18 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::ShellTypeName;
|
|
|
|
use nu_protocol::{
|
|
|
|
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
|
|
|
};
|
|
|
|
use nu_source::Tag;
|
|
|
|
use nu_value_ext::ValueExt;
|
|
|
|
|
|
|
|
use bigdecimal::BigDecimal;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Arguments {
|
|
|
|
rest: Vec<ColumnPath>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
#[async_trait]
|
2020-05-26 22:19:18 +00:00
|
|
|
impl WholeStreamCommand for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
2020-06-01 21:02:57 +00:00
|
|
|
"str to-decimal"
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-06-01 21:02:57 +00:00
|
|
|
Signature::build("str to-decimal").rest(
|
2020-05-26 22:19:18 +00:00
|
|
|
SyntaxShape::ColumnPath,
|
2020-06-01 21:02:57 +00:00
|
|
|
"optionally convert text into decimal by column paths",
|
2020-05-26 22:19:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-06-01 21:02:57 +00:00
|
|
|
"converts text into decimal"
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
async fn run(
|
2020-05-26 22:19:18 +00:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-06-12 23:40:23 +00:00
|
|
|
operate(args, registry).await
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2020-06-01 21:02:57 +00:00
|
|
|
description: "Convert to decimal",
|
|
|
|
example: "echo '3.1415' | str to-decimal",
|
2020-05-26 22:19:18 +00:00
|
|
|
result: None,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 23:40:23 +00:00
|
|
|
async fn operate(
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-26 22:19:18 +00:00
|
|
|
let registry = registry.clone();
|
|
|
|
|
2020-06-12 23:40:23 +00:00
|
|
|
let (Arguments { rest }, input) = args.process(®istry).await?;
|
2020-05-26 22:19:18 +00:00
|
|
|
|
2020-06-12 23:40:23 +00:00
|
|
|
let column_paths: Vec<_> = rest;
|
2020-05-26 22:19:18 +00:00
|
|
|
|
2020-06-12 23:40:23 +00:00
|
|
|
Ok(input
|
|
|
|
.map(move |v| {
|
2020-05-26 22:19:18 +00:00
|
|
|
if column_paths.is_empty() {
|
2020-07-15 07:51:41 +00:00
|
|
|
ReturnSuccess::value(action(&v, v.tag())?)
|
2020-05-26 22:19:18 +00:00
|
|
|
} else {
|
2020-06-12 23:40:23 +00:00
|
|
|
let mut ret = v;
|
2020-05-26 22:19:18 +00:00
|
|
|
|
|
|
|
for path in &column_paths {
|
2020-07-16 17:39:51 +00:00
|
|
|
ret = ret.swap_data_by_column_path(
|
2020-06-12 23:40:23 +00:00
|
|
|
path,
|
|
|
|
Box::new(move |old| action(old, old.tag())),
|
2020-07-16 17:39:51 +00:00
|
|
|
)?;
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:40:23 +00:00
|
|
|
ReturnSuccess::value(ret)
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|
2020-06-12 23:40:23 +00:00
|
|
|
})
|
|
|
|
.to_output_stream())
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
|
|
|
match &input.value {
|
|
|
|
UntaggedValue::Primitive(Primitive::Line(s))
|
|
|
|
| UntaggedValue::Primitive(Primitive::String(s)) => {
|
|
|
|
let other = s.trim();
|
|
|
|
let out = match BigDecimal::from_str(other) {
|
|
|
|
Ok(v) => UntaggedValue::decimal(v),
|
2020-07-14 15:04:00 +00:00
|
|
|
Err(reason) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"could not parse as decimal",
|
|
|
|
reason.to_string(),
|
|
|
|
tag.into().span,
|
|
|
|
))
|
|
|
|
}
|
2020-05-26 22:19:18 +00:00
|
|
|
};
|
|
|
|
Ok(out.into_value(tag))
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
let got = format!("got {}", other.type_name());
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
"value is not string",
|
|
|
|
got,
|
|
|
|
tag.into().span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::{action, SubCommand};
|
|
|
|
use nu_plugin::test_helpers::value::{decimal, string};
|
|
|
|
use nu_source::Tag;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[allow(clippy::approx_constant)]
|
|
|
|
fn turns_to_integer() {
|
|
|
|
let word = string("3.1415");
|
|
|
|
let expected = decimal(3.1415);
|
|
|
|
|
|
|
|
let actual = action(&word, Tag::unknown()).unwrap();
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
2020-07-14 15:04:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn communicates_parsing_error_given_an_invalid_decimallike_string() {
|
|
|
|
let decimal_str = string("11.6anra");
|
|
|
|
|
|
|
|
let actual = action(&decimal_str, Tag::unknown());
|
|
|
|
|
|
|
|
assert!(actual.is_err());
|
|
|
|
}
|
2020-05-26 22:19:18 +00:00
|
|
|
}
|