mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 04:15:08 +02:00
Ensure stable plugins get installed. (#1373)
This commit is contained in:
committed by
GitHub
parent
20ab125861
commit
29ccb9f5cd
@ -6,6 +6,9 @@ edition = "2018"
|
||||
description = "A simple summation plugin for Nushell"
|
||||
license = "MIT"
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
nu-plugin = { path = "../nu-plugin", version = "0.9.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.9.0" }
|
||||
|
4
crates/nu_plugin_sum/src/lib.rs
Normal file
4
crates/nu_plugin_sum/src/lib.rs
Normal file
@ -0,0 +1,4 @@
|
||||
mod nu;
|
||||
mod sum;
|
||||
|
||||
pub use sum::Sum;
|
@ -1,94 +1,5 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::{serve_plugin, Plugin};
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, UntaggedValue, Value,
|
||||
};
|
||||
|
||||
struct Sum {
|
||||
total: Option<Value>,
|
||||
}
|
||||
impl Sum {
|
||||
fn new() -> Sum {
|
||||
Sum { total: None }
|
||||
}
|
||||
|
||||
fn sum(&mut self, value: Value) -> Result<(), ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => Ok(()),
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Int(j)),
|
||||
tag,
|
||||
}) => {
|
||||
//TODO: handle overflow
|
||||
self.total = Some(UntaggedValue::int(i + j).into_value(tag));
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value.clone());
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could not sum non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Bytes(b)) => {
|
||||
match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Bytes(j)),
|
||||
tag,
|
||||
}) => {
|
||||
//TODO: handle overflow
|
||||
self.total = Some(UntaggedValue::bytes(b + j).into_value(tag));
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could not sum non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
x => Err(ShellError::labeled_error(
|
||||
format!("Unrecognized type in stream: {:?}", x),
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Sum {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("sum")
|
||||
.desc("Sum a column of values.")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, _: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
self.sum(input)?;
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
match self.total {
|
||||
None => Ok(vec![]),
|
||||
Some(ref v) => Ok(vec![ReturnSuccess::value(v.clone())]),
|
||||
}
|
||||
}
|
||||
}
|
||||
use nu_plugin::serve_plugin;
|
||||
use nu_plugin_sum::Sum;
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Sum::new());
|
||||
|
29
crates/nu_plugin_sum/src/nu/mod.rs
Normal file
29
crates/nu_plugin_sum/src/nu/mod.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::Plugin;
|
||||
use nu_protocol::{CallInfo, ReturnSuccess, ReturnValue, Signature, Value};
|
||||
|
||||
use crate::Sum;
|
||||
|
||||
impl Plugin for Sum {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("sum")
|
||||
.desc("Sum a column of values.")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, _: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
self.sum(input)?;
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
match self.total {
|
||||
None => Ok(vec![]),
|
||||
Some(ref v) => Ok(vec![ReturnSuccess::value(v.clone())]),
|
||||
}
|
||||
}
|
||||
}
|
66
crates/nu_plugin_sum/src/sum.rs
Normal file
66
crates/nu_plugin_sum/src/sum.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, UntaggedValue, Value};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Sum {
|
||||
pub total: Option<Value>,
|
||||
}
|
||||
|
||||
impl Sum {
|
||||
pub fn new() -> Sum {
|
||||
Sum { total: None }
|
||||
}
|
||||
|
||||
pub fn sum(&mut self, value: Value) -> Result<(), ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => Ok(()),
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Int(j)),
|
||||
tag,
|
||||
}) => {
|
||||
//TODO: handle overflow
|
||||
self.total = Some(UntaggedValue::int(i + j).into_value(tag));
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value.clone());
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could not sum non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Bytes(b)) => {
|
||||
match &self.total {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Bytes(j)),
|
||||
tag,
|
||||
}) => {
|
||||
//TODO: handle overflow
|
||||
self.total = Some(UntaggedValue::bytes(b + j).into_value(tag));
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
self.total = Some(value);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Could not sum non-integer or unrelated types",
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
x => Err(ShellError::labeled_error(
|
||||
format!("Unrecognized type in stream: {:?}", x),
|
||||
"source",
|
||||
value.tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user