2019-07-22 05:52:57 +02:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu::{
|
2019-08-03 04:17:28 +02:00
|
|
|
serve_plugin, CallInfo, Plugin, PositionalType, Primitive, ReturnSuccess, ReturnValue,
|
2019-08-09 06:51:21 +02:00
|
|
|
ShellError, Signature, Tagged, Value,
|
2019-07-22 05:52:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Edit {
|
|
|
|
field: Option<String>,
|
|
|
|
value: Option<Value>,
|
|
|
|
}
|
|
|
|
impl Edit {
|
|
|
|
fn new() -> Edit {
|
|
|
|
Edit {
|
|
|
|
field: None,
|
|
|
|
value: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn edit(&self, value: Tagged<Value>) -> Result<Tagged<Value>, ShellError> {
|
2019-08-05 10:54:29 +02:00
|
|
|
let value_tag = value.tag();
|
2019-07-22 05:52:57 +02:00
|
|
|
match (value.item, self.value.clone()) {
|
|
|
|
(obj @ Value::Object(_), Some(v)) => match &self.field {
|
2019-08-05 10:54:29 +02:00
|
|
|
Some(f) => match obj.replace_data_at_path(value_tag, &f, v) {
|
2019-07-22 05:52:57 +02:00
|
|
|
Some(v) => return Ok(v),
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::string(
|
|
|
|
"edit could not find place to insert field",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => Err(ShellError::string(
|
|
|
|
"edit needs a field when adding a value to an object",
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
x => Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in stream: {:?}",
|
|
|
|
x
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Edit {
|
2019-08-02 21:15:07 +02:00
|
|
|
fn config(&mut self) -> Result<Signature, ShellError> {
|
|
|
|
Ok(Signature {
|
2019-07-22 05:52:57 +02:00
|
|
|
name: "edit".to_string(),
|
|
|
|
positional: vec![
|
|
|
|
PositionalType::mandatory_any("Field"),
|
|
|
|
PositionalType::mandatory_any("Value"),
|
|
|
|
],
|
|
|
|
is_filter: true,
|
|
|
|
named: IndexMap::new(),
|
|
|
|
rest_positional: true,
|
|
|
|
})
|
|
|
|
}
|
2019-07-27 09:45:00 +02:00
|
|
|
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
2019-07-22 05:52:57 +02:00
|
|
|
if let Some(args) = call_info.args.positional {
|
|
|
|
match &args[0] {
|
2019-08-01 03:58:42 +02:00
|
|
|
Tagged {
|
2019-07-22 05:52:57 +02:00
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
self.field = Some(s.clone());
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in params: {:?}",
|
|
|
|
args[0]
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match &args[1] {
|
2019-08-01 03:58:42 +02:00
|
|
|
Tagged { item: v, .. } => {
|
2019-07-22 05:52:57 +02:00
|
|
|
self.value = Some(v.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 09:45:00 +02:00
|
|
|
Ok(vec![])
|
2019-07-22 05:52:57 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
2019-07-22 05:52:57 +02:00
|
|
|
Ok(vec![ReturnSuccess::value(self.edit(input)?)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
serve_plugin(&mut Edit::new());
|
|
|
|
}
|