2019-07-02 07:56:20 +00:00
|
|
|
use nu::{serve_plugin, Args, Plugin, Primitive, ReturnValue, ShellError, Spanned, Value};
|
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-02 07:56:20 +00:00
|
|
|
impl Plugin for Inc {
|
|
|
|
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-02 07:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
|
|
|
match input {
|
|
|
|
Value::Primitive(Primitive::Int(i)) => {
|
|
|
|
Ok(vec![ReturnValue::Value(Value::int(i + self.inc_by))])
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
2019-07-02 07:56:20 +00:00
|
|
|
Value::Primitive(Primitive::Bytes(b)) => Ok(vec![ReturnValue::Value(Value::bytes(
|
|
|
|
b + self.inc_by as u64,
|
|
|
|
))]),
|
|
|
|
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
|
|
|
}
|