use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive, ReturnSuccess, ReturnValue, ShellError, Tagged, TaggedItem, Value, }; struct Inc { field: Option, major: bool, minor: bool, patch: bool, } impl Inc { fn new() -> Inc { Inc { field: None, major: false, minor: false, patch: false, } } fn inc( &self, value: Tagged, field: &Option, ) -> Result, ShellError> { match value.item { Value::Primitive(Primitive::Int(i)) => Ok(Value::int(i + 1).tagged(value.span())), Value::Primitive(Primitive::Bytes(b)) => { Ok(Value::bytes(b + 1 as u64).tagged(value.span())) } Value::Primitive(Primitive::String(ref s)) => { if let Ok(i) = s.parse::() { Ok(Tagged::from_item( Value::string(format!("{}", i + 1)), value.span(), )) } else if let Ok(mut ver) = semver::Version::parse(&s) { if self.major { ver.increment_major(); } else if self.minor { ver.increment_minor(); } else { self.patch; ver.increment_patch(); } Ok(Tagged::from_item( Value::string(ver.to_string()), value.span(), )) } else { Err(ShellError::string("string could not be incremented")) } } Value::Object(_) => match field { Some(f) => { let replacement = match value.item.get_data_by_path(value.span(), f) { Some(result) => self.inc(result.map(|x| x.clone()), &None)?, None => { return Err(ShellError::string("inc could not find field to replace")) } }; match value .item .replace_data_at_path(value.span(), f, replacement.item.clone()) { Some(v) => return Ok(v), None => { return Err(ShellError::string("inc could not find field to replace")) } } } None => Err(ShellError::string( "inc needs a field when incrementing a value in an object", )), }, x => Err(ShellError::string(format!( "Unrecognized type in stream: {:?}", x ))), } } } impl Plugin for Inc { fn config(&mut self) -> Result { let mut named = IndexMap::new(); named.insert("major".to_string(), NamedType::Switch); named.insert("minor".to_string(), NamedType::Switch); named.insert("patch".to_string(), NamedType::Switch); Ok(CommandConfig { name: "inc".to_string(), positional: vec![PositionalType::optional_any("Field")], is_filter: true, is_sink: false, named, rest_positional: true, }) } fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if call_info.args.has("major") { self.major = true; } if call_info.args.has("minor") { self.minor = true; } if call_info.args.has("patch") { self.patch = true; } if let Some(args) = call_info.args.positional { for arg in args { match arg { Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { self.field = Some(s); } _ => { return Err(ShellError::string(format!( "Unrecognized type in params: {:?}", arg ))) } } } } Ok(vec![]) } fn filter(&mut self, input: Tagged) -> Result, ShellError> { Ok(vec![ReturnSuccess::value(self.inc(input, &self.field)?)]) } } fn main() { serve_plugin(&mut Inc::new()); }