nushell/crates/nu-cli/src/commands/update.rs

148 lines
5.0 KiB
Rust
Raw Normal View History

use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
2019-12-05 21:15:41 +01:00
use crate::context::CommandRegistry;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
2019-12-09 19:52:01 +01:00
use nu_value_ext::ValueExt;
2019-12-05 21:15:41 +01:00
use futures::stream::once;
pub struct Update;
2019-12-05 21:15:41 +01:00
#[derive(Deserialize)]
pub struct UpdateArgs {
field: ColumnPath,
replacement: Value,
}
impl WholeStreamCommand for Update {
2019-12-05 21:15:41 +01:00
fn name(&self) -> &str {
"update"
2019-12-05 21:15:41 +01:00
}
fn signature(&self) -> Signature {
Signature::build("update")
2019-12-05 21:15:41 +01:00
.required(
"field",
2019-12-05 21:15:41 +01:00
SyntaxShape::ColumnPath,
"the name of the column to update",
2019-12-05 21:15:41 +01:00
)
.required(
"replacement value",
SyntaxShape::Any,
2019-12-05 21:15:41 +01:00
"the new value to give the cell(s)",
)
}
fn usage(&self) -> &str {
"Update an existing column to have a new value."
2019-12-05 21:15:41 +01:00
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
2019-12-05 21:15:41 +01:00
) -> Result<OutputStream, ShellError> {
Ok(args.process_raw(registry, update)?.run())
}
}
fn update(
UpdateArgs { field, replacement }: UpdateArgs,
context: RunnableContext,
raw_args: RawCommandArgs,
) -> Result<OutputStream, ShellError> {
let scope = raw_args.call_info.scope.clone();
let registry = context.registry.clone();
let mut input_stream = context.input;
2019-12-05 21:15:41 +01:00
let stream = async_stream! {
while let Some(input) = input_stream.next().await {
let replacement = replacement.clone();
match replacement {
Value {
value: UntaggedValue::Block(block),
tag,
} => {
let mut context = Context::from_raw(&raw_args, &registry);
let for_block = input.clone();
let input_clone = input.clone();
let input_stream = once(async { Ok(for_block) }).to_input_stream();
let result = run_block(
&block,
&mut context,
input_stream,
&scope.clone().set_it(input_clone),
).await;
match result {
Ok(mut stream) => {
let errors = context.get_errors();
if let Some(error) = errors.first() {
yield Err(error.clone());
}
2019-12-05 21:15:41 +01:00
match input {
obj @ Value {
value: UntaggedValue::Row(_),
..
} => {
if let Some(result) = stream.next().await {
match obj.replace_data_at_column_path(&field, result.clone()) {
Some(v) => yield Ok(ReturnSuccess::Value(v)),
None => {
yield Err(ShellError::labeled_error(
"update could not find place to insert column",
"column name",
obj.tag,
))
}
}
}
}
Value { tag, ..} => {
yield Err(ShellError::labeled_error(
"Unrecognized type in stream",
"original value",
tag,
))
}
}
}
Err(e) => {
yield Err(e);
}
}
}
_ => {
match input {
obj @ Value {
value: UntaggedValue::Row(_),
..
} => match obj.replace_data_at_column_path(&field, replacement.clone()) {
Some(v) => yield Ok(ReturnSuccess::Value(v)),
None => {
yield Err(ShellError::labeled_error(
"update could not find place to insert column",
"column name",
obj.tag,
))
}
},
Value { tag, ..} => {
yield Err(ShellError::labeled_error(
"Unrecognized type in stream",
"original value",
tag,
))
}
_ => {}
}
2019-12-05 21:15:41 +01:00
}
}}
};
2019-12-05 21:15:41 +01:00
Ok(stream.to_output_stream())
2019-12-05 21:15:41 +01:00
}