From d834708be8919196df28798ab39987ab8f316680 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 30 Apr 2020 14:23:40 +1200 Subject: [PATCH] Finish remove the last traces of per-item (#1685) --- crates/nu-cli/src/commands/command.rs | 134 ++++---------------------- crates/nu-cli/src/context.rs | 16 +-- crates/nu-cli/src/data/command.rs | 11 +-- 3 files changed, 31 insertions(+), 130 deletions(-) diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index dde8457034..48f4cba024 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -207,7 +207,7 @@ pub struct RunnableContext { } impl RunnableContext { - pub fn get_command(&self, name: &str) -> Option> { + pub fn get_command(&self, name: &str) -> Option { self.registry.get_command(name) } } @@ -370,146 +370,54 @@ pub trait WholeStreamCommand: Send + Sync { } } -pub trait PerItemCommand: Send + Sync { - fn name(&self) -> &str; - - fn signature(&self) -> Signature { - Signature::new(self.name()).desc(self.usage()).filter() - } - - fn usage(&self) -> &str; - - fn run( - &self, - call_info: &CallInfo, - registry: &CommandRegistry, - raw_args: &RawCommandArgs, - input: Value, - ) -> Result; - - fn is_binary(&self) -> bool { - false - } -} - -pub enum Command { - WholeStream(Arc), - PerItem(Arc), -} +#[derive(Clone)] +pub struct Command(Arc); impl PrettyDebugWithSource for Command { fn pretty_debug(&self, source: &str) -> DebugDocBuilder { - match self { - Command::WholeStream(command) => b::typed( - "whole stream command", - b::description(command.name()) - + b::space() - + b::equals() - + b::space() - + command.signature().pretty_debug(source), - ), - Command::PerItem(command) => b::typed( - "per item command", - b::description(command.name()) - + b::space() - + b::equals() - + b::space() - + command.signature().pretty_debug(source), - ), - } + b::typed( + "whole stream command", + b::description(self.name()) + + b::space() + + b::equals() + + b::space() + + self.signature().pretty_debug(source), + ) } } impl std::fmt::Debug for Command { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Command::WholeStream(command) => write!(f, "WholeStream({})", command.name()), - Command::PerItem(command) => write!(f, "PerItem({})", command.name()), - } + write!(f, "Command({})", self.name()) } } impl Command { pub fn name(&self) -> &str { - match self { - Command::WholeStream(command) => command.name(), - Command::PerItem(command) => command.name(), - } + self.0.name() } pub fn signature(&self) -> Signature { - match self { - Command::WholeStream(command) => command.signature(), - Command::PerItem(command) => command.signature(), - } + self.0.signature() } pub fn usage(&self) -> &str { - match self { - Command::WholeStream(command) => command.usage(), - Command::PerItem(command) => command.usage(), - } + self.0.usage() } pub fn run(&self, args: CommandArgs, registry: &CommandRegistry) -> OutputStream { if args.call_info.switch_present("help") { get_help(self.name(), self.usage(), self.signature()).into() } else { - match self { - Command::WholeStream(command) => match command.run(args, registry) { - Ok(stream) => stream, - Err(err) => OutputStream::one(Err(err)), - }, - Command::PerItem(command) => { - self.run_helper(command.clone(), args, registry.clone()) - } + match self.0.run(args, registry) { + Ok(stream) => stream, + Err(err) => OutputStream::one(Err(err)), } } } - fn run_helper( - &self, - command: Arc, - args: CommandArgs, - registry: CommandRegistry, - ) -> OutputStream { - let raw_args = RawCommandArgs { - host: args.host, - ctrl_c: args.ctrl_c, - shell_manager: args.shell_manager, - call_info: args.call_info, - }; - - let out = args - .input - .map(move |x| { - let call_info = UnevaluatedCallInfo { - args: raw_args.call_info.args.clone(), - name_tag: raw_args.call_info.name_tag.clone(), - scope: raw_args.call_info.scope.clone().set_it(x.clone()), - } - .evaluate(®istry); - - match call_info { - Ok(call_info) => match command.run(&call_info, ®istry, &raw_args, x) { - Ok(o) => o, - Err(e) => { - futures::stream::iter(vec![ReturnValue::Err(e)]).to_output_stream() - } - }, - Err(e) => futures::stream::iter(vec![ReturnValue::Err(e)]).to_output_stream(), - } - }) - .flatten(); - - out.to_output_stream() - } - pub fn is_binary(&self) -> bool { - match self { - Command::WholeStream(command) => command.is_binary(), - Command::PerItem(command) => command.is_binary(), - } + self.0.is_binary() } } @@ -571,6 +479,6 @@ impl WholeStreamCommand for FnFilterCommand { } } -pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Arc { - Arc::new(Command::WholeStream(Arc::new(command))) +pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Command { + Command(Arc::new(command)) } diff --git a/crates/nu-cli/src/context.rs b/crates/nu-cli/src/context.rs index 7a04885926..6314ec4a11 100644 --- a/crates/nu-cli/src/context.rs +++ b/crates/nu-cli/src/context.rs @@ -16,7 +16,7 @@ use std::sync::Arc; #[derive(Debug, Clone, Default)] pub struct CommandRegistry { - registry: Arc>>>, + registry: Arc>>, } impl SignatureRegistry for CommandRegistry { @@ -42,13 +42,13 @@ impl CommandRegistry { } impl CommandRegistry { - pub(crate) fn get_command(&self, name: &str) -> Option> { + pub(crate) fn get_command(&self, name: &str) -> Option { let registry = self.registry.lock(); registry.get(name).cloned() } - pub(crate) fn expect_command(&self, name: &str) -> Result, ShellError> { + pub(crate) fn expect_command(&self, name: &str) -> Result { self.get_command(name).ok_or_else(|| { ShellError::untagged_runtime_error(format!("Could not load command: {}", name)) }) @@ -60,7 +60,7 @@ impl CommandRegistry { registry.contains_key(name) } - pub(crate) fn insert(&mut self, name: impl Into, command: Arc) { + pub(crate) fn insert(&mut self, name: impl Into, command: Command) { let mut registry = self.registry.lock(); registry.insert(name.into(), command); } @@ -209,23 +209,23 @@ impl Context { block(&mut *errors) } - pub fn add_commands(&mut self, commands: Vec>) { + pub fn add_commands(&mut self, commands: Vec) { for command in commands { self.registry.insert(command.name().to_string(), command); } } - pub(crate) fn get_command(&self, name: &str) -> Option> { + pub(crate) fn get_command(&self, name: &str) -> Option { self.registry.get_command(name) } - pub(crate) fn expect_command(&self, name: &str) -> Result, ShellError> { + pub(crate) fn expect_command(&self, name: &str) -> Result { self.registry.expect_command(name) } pub(crate) fn run_command( &mut self, - command: Arc, + command: Command, name_tag: Tag, args: hir::Call, scope: &Scope, diff --git a/crates/nu-cli/src/data/command.rs b/crates/nu-cli/src/data/command.rs index 666008376f..947b5d04ba 100644 --- a/crates/nu-cli/src/data/command.rs +++ b/crates/nu-cli/src/data/command.rs @@ -2,22 +2,15 @@ use crate::commands::command::Command; use crate::data::TaggedListBuilder; use crate::prelude::*; use nu_protocol::{NamedType, PositionalType, Signature, TaggedDictBuilder, UntaggedValue, Value}; -use std::ops::Deref; -pub(crate) fn command_dict(command: Arc, tag: impl Into) -> Value { +pub(crate) fn command_dict(command: Command, tag: impl Into) -> Value { let tag = tag.into(); let mut cmd_dict = TaggedDictBuilder::new(&tag); cmd_dict.insert_untagged("name", UntaggedValue::string(command.name())); - cmd_dict.insert_untagged( - "type", - UntaggedValue::string(match command.deref() { - Command::WholeStream(_) => "Command", - Command::PerItem(_) => "Filter", - }), - ); + cmd_dict.insert_untagged("type", UntaggedValue::string("Command")); cmd_dict.insert_value("signature", signature_dict(command.signature(), tag)); cmd_dict.insert_untagged("usage", UntaggedValue::string(command.usage()));