2019-07-03 17:37:09 +00:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu::{
|
2019-07-13 02:07:06 +00:00
|
|
|
serve_plugin, Args, CommandConfig, Plugin, PositionalType, Primitive, ReturnSuccess,
|
|
|
|
ReturnValue, ShellError, Spanned, SpannedItem, 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 {
|
|
|
|
inc_by: i64,
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
2019-07-02 07:56:20 +00:00
|
|
|
impl Inc {
|
|
|
|
fn new() -> Inc {
|
|
|
|
Inc { inc_by: 1 }
|
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> {
|
|
|
|
Ok(CommandConfig {
|
|
|
|
name: "inc".to_string(),
|
2019-07-16 07:25:48 +00:00
|
|
|
positional: vec![PositionalType::optional_any("Increment")],
|
2019-07-13 02:07:06 +00:00
|
|
|
is_filter: true,
|
|
|
|
is_sink: false,
|
|
|
|
named: IndexMap::new(),
|
|
|
|
rest_positional: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn begin_filter(&mut self, args: Args) -> Result<(), ShellError> {
|
|
|
|
if let Some(args) = args.positional {
|
|
|
|
for arg in args {
|
|
|
|
match arg {
|
|
|
|
Spanned {
|
|
|
|
item: Value::Primitive(Primitive::Int(i)),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
self.inc_by = i;
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
2019-07-02 07:56:20 +00:00
|
|
|
_ => return Err(ShellError::string("Unrecognized type in params")),
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-13 02:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
|
|
|
let span = input.span;
|
|
|
|
|
|
|
|
match input.item {
|
|
|
|
Value::Primitive(Primitive::Int(i)) => Ok(vec![ReturnSuccess::value(
|
|
|
|
Value::int(i + self.inc_by).spanned(span),
|
|
|
|
)]),
|
|
|
|
Value::Primitive(Primitive::Bytes(b)) => Ok(vec![ReturnSuccess::value(
|
|
|
|
Value::bytes(b + self.inc_by as u64).spanned(span),
|
|
|
|
)]),
|
2019-07-02 07:56:20 +00:00
|
|
|
x => Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in stream: {:?}",
|
|
|
|
x
|
|
|
|
))),
|
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
|
|
|
}
|