forked from extern/nushell
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 regex match 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_match/src/lib.rs
Normal file
4
crates/nu_plugin_match/src/lib.rs
Normal file
@ -0,0 +1,4 @@
|
||||
mod match_;
|
||||
mod nu;
|
||||
|
||||
pub use match_::Match;
|
@ -1,112 +1,5 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::{serve_plugin, Plugin};
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
struct Match {
|
||||
column: String,
|
||||
regex: Regex,
|
||||
}
|
||||
|
||||
impl Match {
|
||||
#[allow(clippy::trivial_regex)]
|
||||
fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
Ok(Match {
|
||||
column: String::new(),
|
||||
regex: Regex::new("")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Match {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("match")
|
||||
.desc("filter rows by regex")
|
||||
.required("member", SyntaxShape::Member, "the column name to match")
|
||||
.required("regex", SyntaxShape::String, "the regex to match with")
|
||||
.filter())
|
||||
}
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
match &args[0] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.column = s.clone();
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"value",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
match &args[1] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
tag,
|
||||
} => {
|
||||
self.regex = Regex::new(s).map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Internal error while creating regex",
|
||||
"internal error created by pattern",
|
||||
tag,
|
||||
)
|
||||
})?;
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"unexpected value",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let flag: bool;
|
||||
match &input {
|
||||
Value {
|
||||
value: UntaggedValue::Row(dict),
|
||||
tag,
|
||||
} => {
|
||||
if let Some(val) = dict.entries.get(&self.column) {
|
||||
if let Ok(s) = val.as_string() {
|
||||
flag = self.regex.is_match(&s);
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
"expected string",
|
||||
"value",
|
||||
val.tag(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!("column not in row! {:?} {:?}", &self.column, dict),
|
||||
"row",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error("Expected row", "value", tag));
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
Ok(vec![Ok(ReturnSuccess::Value(input))])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
}
|
||||
use nu_plugin::serve_plugin;
|
||||
use nu_plugin_match::Match;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
serve_plugin(&mut Match::new()?);
|
||||
|
16
crates/nu_plugin_match/src/match_.rs
Normal file
16
crates/nu_plugin_match/src/match_.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use regex::Regex;
|
||||
|
||||
pub struct Match {
|
||||
pub column: String,
|
||||
pub regex: Regex,
|
||||
}
|
||||
|
||||
impl Match {
|
||||
#[allow(clippy::trivial_regex)]
|
||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
Ok(Match {
|
||||
column: String::new(),
|
||||
regex: Regex::new("")?,
|
||||
})
|
||||
}
|
||||
}
|
96
crates/nu_plugin_match/src/nu/mod.rs
Normal file
96
crates/nu_plugin_match/src/nu/mod.rs
Normal file
@ -0,0 +1,96 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::Plugin;
|
||||
use nu_protocol::{
|
||||
CallInfo, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
|
||||
use crate::Match;
|
||||
use regex::Regex;
|
||||
|
||||
impl Plugin for Match {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("match")
|
||||
.desc("filter rows by regex")
|
||||
.required("member", SyntaxShape::Member, "the column name to match")
|
||||
.required("regex", SyntaxShape::String, "the regex to match with")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
if let Some(args) = call_info.args.positional {
|
||||
match &args[0] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
} => {
|
||||
self.column = s.clone();
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"value",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
match &args[1] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
tag,
|
||||
} => {
|
||||
self.regex = Regex::new(s).map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Internal error while creating regex",
|
||||
"internal error created by pattern",
|
||||
tag,
|
||||
)
|
||||
})?;
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unrecognized type in params",
|
||||
"unexpected value",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let flag: bool;
|
||||
match &input {
|
||||
Value {
|
||||
value: UntaggedValue::Row(dict),
|
||||
tag,
|
||||
} => {
|
||||
if let Some(val) = dict.entries.get(&self.column) {
|
||||
if let Ok(s) = val.as_string() {
|
||||
flag = self.regex.is_match(&s);
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
"expected string",
|
||||
"value",
|
||||
val.tag(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!("column not in row! {:?} {:?}", &self.column, dict),
|
||||
"row",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
Value { tag, .. } => {
|
||||
return Err(ShellError::labeled_error("Expected row", "value", tag));
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
Ok(vec![Ok(ReturnSuccess::Value(input))])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user