use itertools::Itertools; use nu::{ serve_plugin, CallInfo, Plugin, ReturnSuccess, ReturnValue, ShellError, Signature, SyntaxShape, Tagged, TaggedItem, Value, }; pub type ColumnPath = Vec>; struct Insert { field: Option, value: Option, } impl Insert { fn new() -> Insert { Insert { field: None, value: None, } } fn insert(&self, value: Tagged) -> Result, ShellError> { let value_tag = value.tag(); match (value.item, self.value.clone()) { (obj @ Value::Row(_), Some(v)) => match &self.field { Some(f) => match obj.insert_data_at_column_path(value_tag.clone(), f, v) { Some(v) => return Ok(v), None => { return Err(ShellError::labeled_error( format!( "add could not find place to insert field {:?} {}", obj, f.iter() .map(|i| { match &i.item { Value::Primitive(primitive) => primitive.format(None), _ => String::from(""), } }) .join(".") ), "column name", &value_tag, )) } }, None => Err(ShellError::labeled_error( "add needs a column name when adding a value to a table", "column name", value_tag, )), }, (value, _) => Err(ShellError::type_error( "row", value.type_name().tagged(value_tag), )), } } } impl Plugin for Insert { fn config(&mut self) -> Result { Ok(Signature::build("insert") .desc("Insert a new column to the table.") .required( "column", SyntaxShape::ColumnPath, "the column name to insert", ) .required( "value", SyntaxShape::String, "the value to give the cell(s)", ) .filter()) } fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { match &args[0] { table @ Tagged { item: Value::Table(_), .. } => { self.field = Some(table.as_column_path()?.item); } value => return Err(ShellError::type_error("table", value.tagged_type_name())), } 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.insert(input)?)]) } } fn main() { serve_plugin(&mut Insert::new()); }