use nu::{ serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature, SyntaxType, Tagged, Value, }; struct Edit { field: Option, value: Option, } impl Edit { fn new() -> Edit { Edit { field: None, value: None, } } fn edit(&self, value: Tagged) -> Result, ShellError> { let value_tag = value.tag(); match (value.item, self.value.clone()) { (obj @ Value::Object(_), Some(v)) => match &self.field { Some(f) => match obj.replace_data_at_path(value_tag, &f, v) { 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 { fn config(&mut self) -> Result { Ok(Signature::build("edit") .desc("Edit an existing field to have a new value.") .required("Field", SyntaxType::String) .required("Value", SyntaxType::String) .filter()) } fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { match &args[0] { Tagged { 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] { Tagged { item: v, .. } => { self.value = Some(v.clone()); } } } Ok(vec![]) } fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value(self.edit(input)?)]) } } fn main() { serve_plugin(&mut Edit::new()); }