2020-04-27 02:04:54 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-12-05 20:15:41 +00:00
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
2020-04-27 02:04:54 +00:00
|
|
|
use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
2019-12-09 18:52:01 +00:00
|
|
|
use nu_value_ext::ValueExt;
|
2019-12-05 20:15:41 +00:00
|
|
|
|
|
|
|
pub struct Edit;
|
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct EditArgs {
|
|
|
|
field: ColumnPath,
|
|
|
|
replacement: Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Edit {
|
2019-12-05 20:15:41 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"edit"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("edit")
|
|
|
|
.required(
|
2020-04-27 02:04:54 +00:00
|
|
|
"field",
|
2019-12-05 20:15:41 +00:00
|
|
|
SyntaxShape::ColumnPath,
|
|
|
|
"the name of the column to edit",
|
|
|
|
)
|
|
|
|
.required(
|
2020-04-27 02:04:54 +00:00
|
|
|
"replacement value",
|
|
|
|
SyntaxShape::Any,
|
2019-12-05 20:15:41 +00:00
|
|
|
"the new value to give the cell(s)",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Edit an existing column to have a new value."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2020-04-27 02:04:54 +00:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
2019-12-05 20:15:41 +00:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-04-27 02:04:54 +00:00
|
|
|
args.process(registry, edit)?.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn edit(
|
|
|
|
EditArgs { field, replacement }: EditArgs,
|
|
|
|
RunnableContext { input, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let mut input = input;
|
2019-12-05 20:15:41 +00:00
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
let stream = async_stream! {
|
|
|
|
match input.next().await {
|
|
|
|
Some(obj @ Value {
|
2019-12-05 20:15:41 +00:00
|
|
|
value: UntaggedValue::Row(_),
|
|
|
|
..
|
2020-04-27 02:04:54 +00:00
|
|
|
}) => match obj.replace_data_at_column_path(&field, replacement.clone()) {
|
|
|
|
Some(v) => yield Ok(ReturnSuccess::Value(v)),
|
2019-12-05 20:15:41 +00:00
|
|
|
None => {
|
2020-04-27 02:04:54 +00:00
|
|
|
yield Err(ShellError::labeled_error(
|
2019-12-05 20:15:41 +00:00
|
|
|
"edit could not find place to insert column",
|
|
|
|
"column name",
|
2020-04-27 02:04:54 +00:00
|
|
|
obj.tag,
|
2019-12-05 20:15:41 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
Some(Value { tag, ..}) => {
|
|
|
|
yield Err(ShellError::labeled_error(
|
2019-12-05 20:15:41 +00:00
|
|
|
"Unrecognized type in stream",
|
|
|
|
"original value",
|
2020-04-27 02:04:54 +00:00
|
|
|
tag,
|
2019-12-05 20:15:41 +00:00
|
|
|
))
|
|
|
|
}
|
2020-04-27 02:04:54 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
};
|
2019-12-05 20:15:41 +00:00
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
Ok(stream.to_output_stream())
|
2019-12-05 20:15:41 +00:00
|
|
|
}
|