From 3ebb6ba99119e146281647dc5269b8e480778d72 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 16 Jul 2019 19:08:35 +1200 Subject: [PATCH] Fix plugin's commandconfig --- src/cli.rs | 18 ++++++-------- src/commands/macros.rs | 1 + src/commands/plugin.rs | 41 +++++++++++++++++++++++++++++++- src/lib.rs | 2 +- src/object/types.rs | 5 +--- src/parser/hir/baseline_parse.rs | 2 +- src/parser/parse/token_tree.rs | 16 ++++++------- src/parser/registry.rs | 5 ++-- src/plugins/binaryview.rs | 4 ++-- src/prelude.rs | 2 +- 10 files changed, 65 insertions(+), 31 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 03025ebec3..6dc62aa182 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,6 +6,7 @@ use crate::commands::classified::{ }; use crate::commands::command::sink; use crate::commands::plugin::JsonRpc; +use crate::commands::plugin::{PluginCommand, PluginSink}; use crate::context::Context; crate use crate::errors::ShellError; use crate::evaluate::Scope; @@ -43,8 +44,6 @@ impl MaybeOwned<'a, T> { } fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), ShellError> { - use crate::commands::{command, plugin}; - let mut child = std::process::Command::new(path) .stdin(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped()) @@ -70,20 +69,17 @@ fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), Shel Ok(jrpc) => match jrpc.params { Ok(params) => { let fname = path.to_string_lossy(); - //println!("Loaded: {} from {}", params.name, fname); if params.is_filter { let fname = fname.to_string(); - context.add_commands(vec![command( - ¶ms.name, - Box::new(move |x| plugin::filter_plugin(fname.clone(), x)), - )]); + let name = params.name.clone(); + context.add_commands(vec![Arc::new(PluginCommand::new( + name, fname, params, + ))]); Ok(()) } else if params.is_sink { let fname = fname.to_string(); - context.add_sinks(vec![sink( - ¶ms.name, - Box::new(move |x| plugin::sink_plugin(fname.clone(), x)), - )]); + let name = params.name.clone(); + context.add_sinks(vec![Arc::new(PluginSink::new(name, fname, params))]); Ok(()) } else { Ok(()) diff --git a/src/commands/macros.rs b/src/commands/macros.rs index 97818df542..44a88c189f 100644 --- a/src/commands/macros.rs +++ b/src/commands/macros.rs @@ -1,4 +1,5 @@ #[doc(hidden)] +#[allow(unused)] macro_rules! named_type { ($name:ident) => { $crate::parser::registry::NamedType::$($name)* diff --git a/src/commands/plugin.rs b/src/commands/plugin.rs index 3d9ecb5c54..a27f1944af 100644 --- a/src/commands/plugin.rs +++ b/src/commands/plugin.rs @@ -1,6 +1,7 @@ -use crate::commands::command::SinkCommandArgs; use crate::errors::ShellError; +use crate::parser::registry; use crate::prelude::*; +use derive_new::new; use serde::{self, Deserialize, Serialize}; use std::io::prelude::*; use std::io::BufReader; @@ -32,6 +33,44 @@ pub enum NuResult { }, } +#[derive(new)] +pub struct PluginCommand { + name: String, + path: String, + config: registry::CommandConfig, +} + +impl Command for PluginCommand { + fn run(&self, args: CommandArgs) -> Result { + filter_plugin(self.path.clone(), args) + } + fn name(&self) -> &str { + &self.name + } + fn config(&self) -> registry::CommandConfig { + self.config.clone() + } +} + +#[derive(new)] +pub struct PluginSink { + name: String, + path: String, + config: registry::CommandConfig, +} + +impl Sink for PluginSink { + fn run(&self, args: SinkCommandArgs) -> Result<(), ShellError> { + sink_plugin(self.path.clone(), args) + } + fn name(&self) -> &str { + &self.name + } + fn config(&self) -> registry::CommandConfig { + self.config.clone() + } +} + pub fn filter_plugin(path: String, args: CommandArgs) -> Result { let mut child = std::process::Command::new(path) .stdin(std::process::Stdio::piped()) diff --git a/src/lib.rs b/src/lib.rs index 05b39f57a6..4528be06b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,4 +32,4 @@ pub use cli::cli; pub use errors::ShellError; pub use object::base::{Primitive, Value}; pub use parser::parse::text::Text; -pub use parser::registry::{Args, CommandConfig, NamedType, NamedValue, PositionalType}; +pub use parser::registry::{Args, CommandConfig, NamedType, PositionalType}; diff --git a/src/object/types.rs b/src/object/types.rs index abc4b7e8fa..0264a33769 100644 --- a/src/object/types.rs +++ b/src/object/types.rs @@ -54,9 +54,6 @@ impl ExtractType for Spanned { } } -#[derive(Debug)] -pub struct FilePath; - impl ExtractType for std::path::PathBuf { fn syntax_type() -> hir::SyntaxType { hir::SyntaxType::Path @@ -66,7 +63,7 @@ impl ExtractType for std::path::PathBuf { match &value { Spanned { item: Value::Primitive(Primitive::String(p)), - span, + .. } => Ok(PathBuf::from(p)), other => Err(ShellError::type_error("Path", other.spanned_type_name())), } diff --git a/src/parser/hir/baseline_parse.rs b/src/parser/hir/baseline_parse.rs index b4dc38458e..02fa63f54d 100644 --- a/src/parser/hir/baseline_parse.rs +++ b/src/parser/hir/baseline_parse.rs @@ -21,7 +21,7 @@ pub fn baseline_parse_token_as_string(token: &Token, source: &Text) -> hir::Expr } RawToken::Variable(span) => hir::Expression::variable(span, token.span), RawToken::Integer(_) => hir::Expression::bare(token.span), - RawToken::Size(int, unit) => hir::Expression::bare(token.span), + RawToken::Size(_, _) => hir::Expression::bare(token.span), RawToken::Bare => hir::Expression::bare(token.span), RawToken::String(span) => hir::Expression::string(span, token.span), } diff --git a/src/parser/parse/token_tree.rs b/src/parser/parse/token_tree.rs index 573b04c5ad..b12bc3e338 100644 --- a/src/parser/parse/token_tree.rs +++ b/src/parser/parse/token_tree.rs @@ -102,15 +102,15 @@ impl TokenNode { pub fn type_name(&self) -> String { match self { TokenNode::Token(t) => t.type_name(), - TokenNode::Call(s) => "command", + TokenNode::Call(_) => "command", TokenNode::Delimited(d) => d.type_name(), - TokenNode::Pipeline(s) => "pipeline", - TokenNode::Operator(s) => "operator", - TokenNode::Flag(s) => "flag", - TokenNode::Member(s) => "member", - TokenNode::Whitespace(s) => "whitespace", - TokenNode::Error(s) => "error", - TokenNode::Path(s) => "path", + TokenNode::Pipeline(_) => "pipeline", + TokenNode::Operator(_) => "operator", + TokenNode::Flag(_) => "flag", + TokenNode::Member(_) => "member", + TokenNode::Whitespace(_) => "whitespace", + TokenNode::Error(_) => "error", + TokenNode::Path(_) => "path", } .to_string() } diff --git a/src/parser/registry.rs b/src/parser/registry.rs index 0d54f0429e..7a15071cbc 100644 --- a/src/parser/registry.rs +++ b/src/parser/registry.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use std::fmt; #[allow(unused)] -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub enum NamedType { Switch, Mandatory(SyntaxType), @@ -36,6 +36,7 @@ impl PositionalType { PositionalType::Mandatory(name.to_string(), SyntaxType::Block) } + #[allow(unused)] crate fn to_coerce_hint(&self) -> Option { match self { PositionalType::Mandatory(_, SyntaxType::Block) @@ -59,7 +60,7 @@ impl PositionalType { } } -#[derive(Debug, Getters, Serialize, Deserialize)] +#[derive(Debug, Getters, Serialize, Deserialize, Clone)] #[get = "crate"] pub struct CommandConfig { pub name: String, diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index ea600d62c5..8c525f3886 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -14,14 +14,14 @@ impl BinaryView { impl Plugin for BinaryView { fn config(&mut self) -> Result { let mut named = IndexMap::new(); - named.insert("--lores".to_string(), NamedType::Switch); + named.insert("lores".to_string(), NamedType::Switch); Ok(CommandConfig { name: "binaryview".to_string(), positional: vec![], is_filter: false, is_sink: true, named, - rest_positional: true, + rest_positional: false, }) } diff --git a/src/prelude.rs b/src/prelude.rs index 7316efada3..509a0866ac 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -34,7 +34,7 @@ macro_rules! trace_stream { crate use crate::cli::MaybeOwned; crate use crate::commands::command::{ - Command, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, + Command, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, Sink, SinkCommandArgs, }; crate use crate::context::Context; crate use crate::env::host::handle_unexpected;