nushell/src/plugins/inc.rs

143 lines
4.6 KiB
Rust
Raw Normal View History

2019-07-03 17:37:09 +00:00
use indexmap::IndexMap;
use nu::{
serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive,
2019-08-01 01:58:42 +00:00
ReturnSuccess, ReturnValue, ShellError, Tagged, TaggedItem, Value,
2019-07-03 17:37:09 +00:00
};
2019-06-27 04:56:48 +00:00
2019-07-02 07:56:20 +00:00
struct Inc {
2019-07-18 01:32:19 +00:00
field: Option<String>,
2019-07-18 01:49:12 +00:00
major: bool,
minor: bool,
patch: bool,
2019-06-27 04:56:48 +00:00
}
2019-07-02 07:56:20 +00:00
impl Inc {
fn new() -> Inc {
2019-07-18 01:49:12 +00:00
Inc {
field: None,
major: false,
minor: false,
patch: false,
}
2019-07-18 01:32:19 +00:00
}
2019-07-18 01:49:12 +00:00
fn inc(
&self,
2019-08-01 01:58:42 +00:00
value: Tagged<Value>,
2019-07-18 01:49:12 +00:00
field: &Option<String>,
2019-08-01 01:58:42 +00:00
) -> Result<Tagged<Value>, ShellError> {
2019-07-18 01:32:19 +00:00
match value.item {
2019-08-01 01:58:42 +00:00
Value::Primitive(Primitive::Int(i)) => Ok(Value::int(i + 1).tagged(value.span())),
2019-07-18 01:32:19 +00:00
Value::Primitive(Primitive::Bytes(b)) => {
2019-08-01 01:58:42 +00:00
Ok(Value::bytes(b + 1 as u64).tagged(value.span()))
2019-07-18 01:32:19 +00:00
}
2019-08-01 01:58:42 +00:00
Value::Primitive(Primitive::String(ref s)) => {
2019-07-18 01:32:19 +00:00
if let Ok(i) = s.parse::<u64>() {
2019-08-01 01:58:42 +00:00
Ok(Tagged::from_item(
Value::string(format!("{}", i + 1)),
value.span(),
))
2019-07-18 01:49:12 +00:00
} 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();
}
2019-08-01 01:58:42 +00:00
Ok(Tagged::from_item(
Value::string(ver.to_string()),
value.span(),
))
2019-07-18 01:32:19 +00:00
} else {
Err(ShellError::string("string could not be incremented"))
}
}
Value::Object(_) => match field {
Some(f) => {
2019-08-01 01:58:42 +00:00
let replacement = match value.item.get_data_by_path(value.span(), f) {
2019-07-18 01:49:12 +00:00
Some(result) => self.inc(result.map(|x| x.clone()), &None)?,
2019-07-18 01:32:19 +00:00
None => {
return Err(ShellError::string("inc could not find field to replace"))
}
};
match value
.item
2019-08-01 01:58:42 +00:00
.replace_data_at_path(value.span(), f, replacement.item.clone())
2019-07-18 01:32:19 +00:00
{
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
))),
}
2019-06-27 04:56:48 +00:00
}
}
2019-07-13 02:07:06 +00:00
impl Plugin for Inc {
fn config(&mut self) -> Result<CommandConfig, ShellError> {
2019-07-18 01:49:12 +00:00
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);
2019-07-13 02:07:06 +00:00
Ok(CommandConfig {
name: "inc".to_string(),
2019-07-18 01:32:19 +00:00
positional: vec![PositionalType::optional_any("Field")],
2019-07-13 02:07:06 +00:00
is_filter: true,
is_sink: false,
2019-07-18 01:49:12 +00:00
named,
2019-07-13 02:07:06 +00:00
rest_positional: true,
})
}
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
if call_info.args.has("major") {
2019-07-18 01:49:12 +00:00
self.major = true;
}
if call_info.args.has("minor") {
2019-07-18 01:49:12 +00:00
self.minor = true;
}
if call_info.args.has("patch") {
2019-07-18 01:49:12 +00:00
self.patch = true;
}
if let Some(args) = call_info.args.positional {
2019-07-13 02:07:06 +00:00
for arg in args {
match arg {
2019-08-01 01:58:42 +00:00
Tagged {
2019-07-18 01:32:19 +00:00
item: Value::Primitive(Primitive::String(s)),
2019-07-13 02:07:06 +00:00
..
} => {
2019-07-18 01:32:19 +00:00
self.field = Some(s);
}
_ => {
return Err(ShellError::string(format!(
"Unrecognized type in params: {:?}",
arg
)))
2019-06-27 04:56:48 +00:00
}
}
}
2019-07-13 02:07:06 +00:00
}
Ok(vec![])
2019-07-13 02:07:06 +00:00
}
2019-08-01 01:58:42 +00:00
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
2019-07-18 01:49:12 +00:00
Ok(vec![ReturnSuccess::value(self.inc(input, &self.field)?)])
2019-06-27 04:56:48 +00:00
}
2019-07-02 07:56:20 +00:00
}
2019-06-27 04:56:48 +00:00
2019-07-02 07:56:20 +00:00
fn main() {
serve_plugin(&mut Inc::new());
2019-06-27 04:56:48 +00:00
}