From d71206ed9e99b701a04cf2e3dd6d21e51dd55deb Mon Sep 17 00:00:00 2001 From: Odin Dutton Date: Mon, 19 Aug 2019 15:16:39 +1000 Subject: [PATCH] Implement WholeStreamCommand for all remaining commands --- src/cli.rs | 63 ++++++++++++++++++------------------ src/commands.rs | 35 ++++++++++++++++++-- src/commands/cd.rs | 23 ++++++++++++- src/commands/command.rs | 37 --------------------- src/commands/debug.rs | 22 +++++++++++++ src/commands/first.rs | 29 ++++++++++++----- src/commands/from_array.rs | 26 ++++++++++++--- src/commands/from_csv.rs | 23 ++++++++++++- src/commands/from_ini.rs | 23 ++++++++++++- src/commands/from_json.rs | 26 ++++++++++++--- src/commands/from_toml.rs | 21 ++++++++++++ src/commands/from_xml.rs | 23 ++++++++++++- src/commands/from_yaml.rs | 26 ++++++++++++--- src/commands/lines.rs | 23 ++++++++++++- src/commands/ls.rs | 23 ++++++++++++- src/commands/next.rs | 23 ++++++++++++- src/commands/nth.rs | 29 ++++++++++++----- src/commands/pick.rs | 33 ++++++++++++------- src/commands/prev.rs | 24 +++++++++++++- src/commands/ps.rs | 24 +++++++++++++- src/commands/reject.rs | 33 ++++++++++++------- src/commands/shells.rs | 24 +++++++++++++- src/commands/size.rs | 23 ++++++++++++- src/commands/sort_by.rs | 24 +++++++++++++- src/commands/split_column.rs | 38 +++++++++++++++------- src/commands/split_row.rs | 39 ++++++++++++++-------- src/commands/tags.rs | 23 ++++++++++++- src/commands/to_array.rs | 26 ++++++++++++--- src/commands/to_csv.rs | 23 ++++++++++++- src/commands/to_json.rs | 23 ++++++++++++- src/commands/to_toml.rs | 23 ++++++++++++- src/commands/to_yaml.rs | 23 ++++++++++++- src/commands/trim.rs | 23 ++++++++++++- 33 files changed, 732 insertions(+), 169 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 02cd99e07..816fc3a93 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -145,38 +145,37 @@ pub async fn cli() -> Result<(), Box> { use crate::commands::*; context.add_commands(vec![ - command("first", Box::new(first::first)), - command("pick", Box::new(pick::pick)), - command("from-array", Box::new(from_array::from_array)), - command("from-ini", Box::new(from_ini::from_ini)), - command("from-csv", Box::new(from_csv::from_csv)), - command("from-json", Box::new(from_json::from_json)), - command("from-toml", Box::new(from_toml::from_toml)), - command("from-xml", Box::new(from_xml::from_xml)), - command("ps", Box::new(ps::ps)), - command("ls", Box::new(ls::ls)), - command("cd", Box::new(cd::cd)), - command("size", Box::new(size::size)), - command("from-yaml", Box::new(from_yaml::from_yaml)), - command("nth", Box::new(nth::nth)), - command("n", Box::new(next::next)), - command("p", Box::new(prev::prev)), - command("debug", Box::new(debug::debug)), - command("lines", Box::new(lines::lines)), - command("pick", Box::new(pick::pick)), - command("shells", Box::new(shells::shells)), - command("split-column", Box::new(split_column::split_column)), - command("split-row", Box::new(split_row::split_row)), - command("lines", Box::new(lines::lines)), - command("reject", Box::new(reject::reject)), - command("trim", Box::new(trim::trim)), - command("to-array", Box::new(to_array::to_array)), - command("to-csv", Box::new(to_csv::to_csv)), - command("to-json", Box::new(to_json::to_json)), - command("to-toml", Box::new(to_toml::to_toml)), - command("to-yaml", Box::new(to_yaml::to_yaml)), - command("sort-by", Box::new(sort_by::sort_by)), - command("tags", Box::new(tags::tags)), + whole_stream_command(PS), + whole_stream_command(LS), + whole_stream_command(CD), + whole_stream_command(Size), + whole_stream_command(Nth), + whole_stream_command(Next), + whole_stream_command(Previous), + whole_stream_command(Debug), + whole_stream_command(Lines), + whole_stream_command(Shells), + whole_stream_command(SplitColumn), + whole_stream_command(SplitRow), + whole_stream_command(Lines), + whole_stream_command(Reject), + whole_stream_command(Trim), + whole_stream_command(ToArray), + whole_stream_command(ToCSV), + whole_stream_command(ToJSON), + whole_stream_command(ToTOML), + whole_stream_command(ToYAML), + whole_stream_command(SortBy), + whole_stream_command(Tags), + whole_stream_command(First), + whole_stream_command(FromArray), + whole_stream_command(FromCSV), + whole_stream_command(FromINI), + whole_stream_command(FromJSON), + whole_stream_command(FromTOML), + whole_stream_command(FromXML), + whole_stream_command(FromYAML), + whole_stream_command(Pick), whole_stream_command(Get), per_item_command(Remove), per_item_command(Open), diff --git a/src/commands.rs b/src/commands.rs index 26dfb4fc3..1591747f8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -56,25 +56,54 @@ crate mod where_; crate mod which_; crate use autoview::Autoview; -//crate use cd::Cd; +crate use cd::CD; crate use clip::Clip; crate use command::{ - command, per_item_command, whole_stream_command, Command, CommandArgs, PerItemCommand, - RawCommandArgs, UnevaluatedCallInfo, WholeStreamCommand, + per_item_command, whole_stream_command, Command, CommandArgs, PerItemCommand, RawCommandArgs, + UnevaluatedCallInfo, WholeStreamCommand, }; crate use config::Config; crate use cp::Cpy; crate use date::Date; +crate use debug::Debug; crate use enter::Enter; crate use exit::Exit; +crate use first::First; +crate use from_array::FromArray; +crate use from_csv::FromCSV; +crate use from_ini::FromINI; +crate use from_json::FromJSON; +crate use from_toml::FromTOML; +crate use from_xml::FromXML; +crate use from_yaml::FromYAML; crate use get::Get; +crate use lines::Lines; +crate use ls::LS; crate use mkdir::Mkdir; crate use mv::Move; +crate use next::Next; +crate use nth::Nth; crate use open::Open; +crate use pick::Pick; +crate use prev::Previous; +crate use ps::PS; +crate use reject::Reject; crate use rm::Remove; crate use save::Save; +crate use shells::Shells; +crate use size::Size; crate use skip_while::SkipWhile; +crate use sort_by::SortBy; +crate use split_column::SplitColumn; +crate use split_row::SplitRow; crate use table::Table; +crate use tags::Tags; +crate use to_array::ToArray; +crate use to_csv::ToCSV; +crate use to_json::ToJSON; +crate use to_toml::ToTOML; +crate use to_yaml::ToYAML; +crate use trim::Trim; crate use version::Version; crate use vtable::VTable; crate use where_::Where; diff --git a/src/commands/cd.rs b/src/commands/cd.rs index 0fe24add8..dab6f3548 100644 --- a/src/commands/cd.rs +++ b/src/commands/cd.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::prelude::*; -pub fn cd(args: CommandArgs, registry: &CommandRegistry) -> Result { +pub struct CD; + +impl WholeStreamCommand for CD { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + cd(args, registry) + } + + fn name(&self) -> &str { + "cd" + } + + fn signature(&self) -> Signature { + Signature::build("cd").required("directory", SyntaxType::Path) + } +} + +fn cd(args: CommandArgs, registry: &CommandRegistry) -> Result { let shell_manager = args.shell_manager.clone(); let args = args.evaluate_once(registry)?; shell_manager.cd(args) diff --git a/src/commands/command.rs b/src/commands/command.rs index 377e71dcc..4b16c1c37 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -635,43 +635,6 @@ impl WholeStreamCommand for FnFilterCommand { } } -pub struct FnRawCommand { - name: String, - func: Box< - dyn Fn(CommandArgs, ®istry::CommandRegistry) -> Result - + Send - + Sync, - >, -} - -impl WholeStreamCommand for FnRawCommand { - fn name(&self) -> &str { - &self.name - } - - fn run( - &self, - args: CommandArgs, - registry: ®istry::CommandRegistry, - ) -> Result { - (self.func)(args, registry) - } -} - -pub fn command( - name: &str, - func: Box< - dyn Fn(CommandArgs, ®istry::CommandRegistry) -> Result - + Send - + Sync, - >, -) -> Arc { - Arc::new(Command::WholeStream(Arc::new(FnRawCommand { - name: name.to_string(), - func, - }))) -} - pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Arc { Arc::new(Command::WholeStream(Arc::new(command))) } diff --git a/src/commands/debug.rs b/src/commands/debug.rs index ac3ab3c1c..d5f84afa4 100644 --- a/src/commands/debug.rs +++ b/src/commands/debug.rs @@ -1,6 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::prelude::*; +pub struct Debug; + +impl WholeStreamCommand for Debug { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + debug(args, registry) + } + + fn name(&self) -> &str { + "debug" + } + + fn signature(&self) -> Signature { + // TODO: Signature? + Signature::build("debug") + } +} + pub fn debug(args: CommandArgs, _registry: &CommandRegistry) -> Result { let input = args.input; diff --git a/src/commands/first.rs b/src/commands/first.rs index a48e55593..48f1871fd 100644 --- a/src/commands/first.rs +++ b/src/commands/first.rs @@ -1,18 +1,31 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::parser::CommandRegistry; use crate::prelude::*; -pub fn first(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; +pub struct First; - if args.len() == 0 { - return Err(ShellError::labeled_error( - "First requires an amount", - "needs parameter", - args.name_span(), - )); +impl WholeStreamCommand for First { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + first(args, registry) } + fn name(&self) -> &str { + "first" + } + + fn signature(&self) -> Signature { + Signature::build("first").required("amount", SyntaxType::Literal) + } +} + +fn first(args: CommandArgs, registry: &CommandRegistry) -> Result { + let args = args.evaluate_once(registry)?; + let amount = args.expect_nth(0)?.as_i64(); let amount = match amount { diff --git a/src/commands/from_array.rs b/src/commands/from_array.rs index 10bef6786..3fb04c9bf 100644 --- a/src/commands/from_array.rs +++ b/src/commands/from_array.rs @@ -1,10 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::Value; use crate::prelude::*; -pub fn from_array( - args: CommandArgs, - _registry: &CommandRegistry, -) -> Result { +pub struct FromArray; + +impl WholeStreamCommand for FromArray { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_array(args, registry) + } + + fn name(&self) -> &str { + "from-array" + } + + fn signature(&self) -> Signature { + Signature::build("from-array") + } +} + +fn from_array(args: CommandArgs, _registry: &CommandRegistry) -> Result { let stream = args .input .values diff --git a/src/commands/from_csv.rs b/src/commands/from_csv.rs index 1485646f9..ac5399096 100644 --- a/src/commands/from_csv.rs +++ b/src/commands/from_csv.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use csv::ReaderBuilder; +pub struct FromCSV; + +impl WholeStreamCommand for FromCSV { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_csv(args, registry) + } + + fn name(&self) -> &str { + "from-csv" + } + + fn signature(&self) -> Signature { + Signature::build("from-csv") + } +} + pub fn from_csv_string_to_value( s: String, tag: impl Into, @@ -45,7 +66,7 @@ pub fn from_csv_string_to_value( Ok(Tagged::from_item(Value::List(rows), tag)) } -pub fn from_csv(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn from_csv(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); let out = args.input; diff --git a/src/commands/from_ini.rs b/src/commands/from_ini.rs index e6b7e569a..229d4fa45 100644 --- a/src/commands/from_ini.rs +++ b/src/commands/from_ini.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use std::collections::HashMap; +pub struct FromINI; + +impl WholeStreamCommand for FromINI { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_ini(args, registry) + } + + fn name(&self) -> &str { + "from-ini" + } + + fn signature(&self) -> Signature { + Signature::build("from-ini") + } +} + fn convert_ini_second_to_nu_value( v: &HashMap, tag: impl Into, @@ -37,7 +58,7 @@ pub fn from_ini_string_to_value( Ok(convert_ini_top_to_nu_value(&v, tag)) } -pub fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); let out = args.input; diff --git a/src/commands/from_json.rs b/src/commands/from_json.rs index 2939f7f38..e43dc693b 100644 --- a/src/commands/from_json.rs +++ b/src/commands/from_json.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::base::OF64; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; +pub struct FromJSON; + +impl WholeStreamCommand for FromJSON { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_json(args, registry) + } + + fn name(&self) -> &str { + "from-json" + } + + fn signature(&self) -> Signature { + Signature::build("from-json") + } +} + fn convert_json_value_to_nu_value(v: &serde_hjson::Value, tag: impl Into) -> Tagged { let tag = tag.into(); @@ -43,10 +64,7 @@ pub fn from_json_string_to_value( Ok(convert_json_value_to_nu_value(&v, tag)) } -pub fn from_json( - args: CommandArgs, - registry: &CommandRegistry, -) -> Result { +fn from_json(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); let out = args.input; diff --git a/src/commands/from_toml.rs b/src/commands/from_toml.rs index 9594aece1..ae224f90c 100644 --- a/src/commands/from_toml.rs +++ b/src/commands/from_toml.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::base::OF64; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; +pub struct FromTOML; + +impl WholeStreamCommand for FromTOML { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_toml(args, registry) + } + + fn name(&self) -> &str { + "from-toml" + } + + fn signature(&self) -> Signature { + Signature::build("from-toml") + } +} + pub fn convert_toml_value_to_nu_value(v: &toml::Value, tag: impl Into) -> Tagged { let tag = tag.into(); diff --git a/src/commands/from_xml.rs b/src/commands/from_xml.rs index a2eebad62..42fe4d9df 100644 --- a/src/commands/from_xml.rs +++ b/src/commands/from_xml.rs @@ -1,6 +1,27 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; +pub struct FromXML; + +impl WholeStreamCommand for FromXML { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_xml(args, registry) + } + + fn name(&self) -> &str { + "from-xml" + } + + fn signature(&self) -> Signature { + Signature::build("from-xml") + } +} + fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, tag: impl Into) -> Tagged { let tag = tag.into(); @@ -56,7 +77,7 @@ pub fn from_xml_string_to_value( Ok(from_document_to_value(&parsed, tag)) } -pub fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); let out = args.input; diff --git a/src/commands/from_yaml.rs b/src/commands/from_yaml.rs index 7f3b0f13d..f5547e8d0 100644 --- a/src/commands/from_yaml.rs +++ b/src/commands/from_yaml.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::base::OF64; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; +pub struct FromYAML; + +impl WholeStreamCommand for FromYAML { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + from_yaml(args, registry) + } + + fn name(&self) -> &str { + "from-yaml" + } + + fn signature(&self) -> Signature { + Signature::build("from-yaml") + } +} + fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, tag: impl Into) -> Tagged { let tag = tag.into(); @@ -47,10 +68,7 @@ pub fn from_yaml_string_to_value( Ok(convert_yaml_value_to_nu_value(&v, tag)) } -pub fn from_yaml( - args: CommandArgs, - _registry: &CommandRegistry, -) -> Result { +fn from_yaml(args: CommandArgs, _registry: &CommandRegistry) -> Result { let span = args.name_span(); let out = args.input; diff --git a/src/commands/lines.rs b/src/commands/lines.rs index 65cf7e06a..e6ce32b3f 100644 --- a/src/commands/lines.rs +++ b/src/commands/lines.rs @@ -1,11 +1,32 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::{Primitive, Value}; use crate::prelude::*; use log::trace; +pub struct Lines; + +impl WholeStreamCommand for Lines { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + lines(args, registry) + } + + fn name(&self) -> &str { + "lines" + } + + fn signature(&self) -> Signature { + Signature::build("lines") + } +} + // TODO: "Amount remaining" wrapper -pub fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); let input = args.input; diff --git a/src/commands/ls.rs b/src/commands/ls.rs index 7a101d3d5..bc020c17d 100644 --- a/src/commands/ls.rs +++ b/src/commands/ls.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::prelude::*; -pub fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result { +pub struct LS; + +impl WholeStreamCommand for LS { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + ls(args, registry) + } + + fn name(&self) -> &str { + "ls" + } + + fn signature(&self) -> Signature { + Signature::build("ls").optional("path", SyntaxType::Path) + } +} + +fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result { let shell_manager = args.shell_manager.clone(); let args = args.evaluate_once(registry)?; shell_manager.ls(args) diff --git a/src/commands/next.rs b/src/commands/next.rs index 17e952f63..de0d3dbe2 100644 --- a/src/commands/next.rs +++ b/src/commands/next.rs @@ -1,7 +1,28 @@ use crate::commands::command::CommandAction; +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::prelude::*; -pub fn next(_args: CommandArgs, _registry: &CommandRegistry) -> Result { +pub struct Next; + +impl WholeStreamCommand for Next { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + next(args, registry) + } + + fn name(&self) -> &str { + "n" + } + + fn signature(&self) -> Signature { + Signature::build("n") + } +} + +fn next(_args: CommandArgs, _registry: &CommandRegistry) -> Result { Ok(vec![Ok(ReturnSuccess::Action(CommandAction::NextShell))].into()) } diff --git a/src/commands/nth.rs b/src/commands/nth.rs index 96e29f009..46e78eb2c 100644 --- a/src/commands/nth.rs +++ b/src/commands/nth.rs @@ -1,18 +1,31 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::parser::CommandRegistry; use crate::prelude::*; -pub fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; +pub struct Nth; - if args.len() == 0 { - return Err(ShellError::labeled_error( - "Nth requires an amount", - "needs amount", - args.name_span(), - )); +impl WholeStreamCommand for Nth { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + nth(args, registry) } + fn name(&self) -> &str { + "nth" + } + + fn signature(&self) -> Signature { + Signature::build("nth").required("amount", SyntaxType::Literal) + } +} + +fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result { + let args = args.evaluate_once(registry)?; + let amount = args.expect_nth(0)?.as_i64(); let amount = match amount { diff --git a/src/commands/pick.rs b/src/commands/pick.rs index 71c019339..3c7cf99be 100644 --- a/src/commands/pick.rs +++ b/src/commands/pick.rs @@ -1,22 +1,33 @@ +use crate::commands::WholeStreamCommand; use crate::context::CommandRegistry; use crate::errors::ShellError; use crate::object::base::select_fields; use crate::prelude::*; -pub fn pick(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - let len = args.len(); - let span = args.name_span(); - let (input, args) = args.parts(); +pub struct Pick; - if len == 0 { - return Err(ShellError::labeled_error( - "Pick requires fields", - "needs parameter", - span, - )); +impl WholeStreamCommand for Pick { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + pick(args, registry) } + fn name(&self) -> &str { + "pick" + } + + fn signature(&self) -> Signature { + Signature::build("pick").required("fields", SyntaxType::Any) + } +} + +fn pick(args: CommandArgs, registry: &CommandRegistry) -> Result { + let args = args.evaluate_once(registry)?; + let (input, args) = args.parts(); + let fields: Result, _> = args .positional .iter() diff --git a/src/commands/prev.rs b/src/commands/prev.rs index ea6e3c8d5..4291e9380 100644 --- a/src/commands/prev.rs +++ b/src/commands/prev.rs @@ -2,6 +2,28 @@ use crate::commands::command::CommandAction; use crate::errors::ShellError; use crate::prelude::*; -pub fn prev(_args: CommandArgs, _registry: &CommandRegistry) -> Result { +use crate::commands::WholeStreamCommand; + +pub struct Previous; + +impl WholeStreamCommand for Previous { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + previous(args, registry) + } + + fn name(&self) -> &str { + "p" + } + + fn signature(&self) -> Signature { + Signature::build("p") + } +} + +fn previous(_args: CommandArgs, _registry: &CommandRegistry) -> Result { Ok(vec![Ok(ReturnSuccess::Action(CommandAction::PreviousShell))].into()) } diff --git a/src/commands/ps.rs b/src/commands/ps.rs index 3bac04688..4d4cfb93d 100644 --- a/src/commands/ps.rs +++ b/src/commands/ps.rs @@ -1,9 +1,31 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::process::process_dict; use crate::prelude::*; use sysinfo::{RefreshKind, SystemExt}; -pub fn ps(args: CommandArgs, _registry: &CommandRegistry) -> Result { +pub struct PS; + +impl WholeStreamCommand for PS { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + ps(args, registry) + } + + fn name(&self) -> &str { + "ps" + } + + fn signature(&self) -> Signature { + // TODO: Signature? + Signature::build("ps") + } +} + +fn ps(args: CommandArgs, _registry: &CommandRegistry) -> Result { let mut system = sysinfo::System::new_with_specifics(RefreshKind::new().with_processes()); system.refresh_processes(); let list = system.get_process_list(); diff --git a/src/commands/reject.rs b/src/commands/reject.rs index 443e6e372..dbe305b14 100644 --- a/src/commands/reject.rs +++ b/src/commands/reject.rs @@ -1,21 +1,32 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::base::reject_fields; use crate::prelude::*; -pub fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - let len = args.len(); - let span = args.name_span(); - let (input, args) = args.parts(); +pub struct Reject; - if len == 0 { - return Err(ShellError::labeled_error( - "Reject requires fields", - "needs parameter", - span, - )); +impl WholeStreamCommand for Reject { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + reject(args, registry) } + fn name(&self) -> &str { + "reject" + } + + fn signature(&self) -> Signature { + Signature::build("reject").required("fields", SyntaxType::Any) + } +} + +fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result { + let args = args.evaluate_once(registry)?; + let (input, args) = args.parts(); + let fields: Result, _> = args .positional .iter() diff --git a/src/commands/shells.rs b/src/commands/shells.rs index 0c5982296..5b7528bc4 100644 --- a/src/commands/shells.rs +++ b/src/commands/shells.rs @@ -1,8 +1,30 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::TaggedDictBuilder; use crate::prelude::*; -pub fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result { +pub struct Shells; + +impl WholeStreamCommand for Shells { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + shells(args, registry) + } + + fn name(&self) -> &str { + "shells" + } + + fn signature(&self) -> Signature { + // TODO: Signature? + Signature::build("shells") + } +} + +fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result { let mut shells_out = VecDeque::new(); let span = args.call_info.name_span; diff --git a/src/commands/size.rs b/src/commands/size.rs index 538662696..2af7d47e8 100644 --- a/src/commands/size.rs +++ b/src/commands/size.rs @@ -1,8 +1,29 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::{TaggedDictBuilder, Value}; use crate::prelude::*; -pub fn size(args: CommandArgs, _registry: &CommandRegistry) -> Result { +pub struct Size; + +impl WholeStreamCommand for Size { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + size(args, registry) + } + + fn name(&self) -> &str { + "size" + } + + fn signature(&self) -> Signature { + Signature::build("size") + } +} + +fn size(args: CommandArgs, _registry: &CommandRegistry) -> Result { let input = args.input; let span = args.call_info.name_span; Ok(input diff --git a/src/commands/sort_by.rs b/src/commands/sort_by.rs index 38e240d28..2c2272812 100644 --- a/src/commands/sort_by.rs +++ b/src/commands/sort_by.rs @@ -1,7 +1,29 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::prelude::*; -pub fn sort_by(args: CommandArgs, registry: &CommandRegistry) -> Result { +pub struct SortBy; + +impl WholeStreamCommand for SortBy { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + sort_by(args, registry) + } + + fn name(&self) -> &str { + "sort-by" + } + + fn signature(&self) -> Signature { + // TODO: Signature? + Signature::build("sort-by") + } +} + +fn sort_by(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let (input, args) = args.parts(); diff --git a/src/commands/split_column.rs b/src/commands/split_column.rs index 68a9ba04e..bcc717b37 100644 --- a/src/commands/split_column.rs +++ b/src/commands/split_column.rs @@ -1,26 +1,40 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use log::trace; -pub fn split_column( - args: CommandArgs, - registry: &CommandRegistry, -) -> Result { +pub struct SplitColumn; + +impl WholeStreamCommand for SplitColumn { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + split_column(args, registry) + } + + fn name(&self) -> &str { + "split-column" + } + + fn signature(&self) -> Signature { + // TODO: Signature? + // TODO: Improve error. Old error had extra info: + // + // needs parameter (e.g. split-column ",") + Signature::build("split-column").required("delimeter", SyntaxType::Any) + } +} + +fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); let (input, args) = args.parts(); let positional: Vec<_> = args.positional.iter().flatten().cloned().collect(); - if positional.len() == 0 { - return Err(ShellError::labeled_error( - "Split-column needs more information", - "needs parameter (eg split-column \",\")", - span, - )); - } - Ok(input .values .map(move |v| match v.item { diff --git a/src/commands/split_row.rs b/src/commands/split_row.rs index 3240cc5d5..4781bd35e 100644 --- a/src/commands/split_row.rs +++ b/src/commands/split_row.rs @@ -1,27 +1,40 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::{Primitive, Value}; use crate::prelude::*; use log::trace; -pub fn split_row( - args: CommandArgs, - registry: &CommandRegistry, -) -> Result { +pub struct SplitRow; + +impl WholeStreamCommand for SplitRow { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + split_row(args, registry) + } + + fn name(&self) -> &str { + "split-row" + } + + fn signature(&self) -> Signature { + // TODO: Signature? + // TODO: Improve error. Old error had extra info: + // + // needs parameter (e.g. split-row ",") + Signature::build("split-row").required("delimeter", SyntaxType::Any) + } +} + +fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let span = args.name_span(); - let len = args.len(); let (input, args) = args.parts(); let positional: Vec> = args.positional.iter().flatten().cloned().collect(); - if len == 0 { - return Err(ShellError::labeled_error( - "Split-row needs more information", - "needs parameter (eg split-row \"\\n\")", - span, - )); - } - let stream = input .values .map(move |v| match v.item { diff --git a/src/commands/tags.rs b/src/commands/tags.rs index 0180238eb..b916c03f8 100644 --- a/src/commands/tags.rs +++ b/src/commands/tags.rs @@ -1,8 +1,29 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::{TaggedDictBuilder, Value}; use crate::prelude::*; -pub fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result { +pub struct Tags; + +impl WholeStreamCommand for Tags { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + tags(args, registry) + } + + fn name(&self) -> &str { + "tags" + } + + fn signature(&self) -> Signature { + Signature::build("tags") + } +} + +fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result { let source_map = args.call_info.source_map.clone(); Ok(args .input diff --git a/src/commands/to_array.rs b/src/commands/to_array.rs index af031c62c..6d044d6d8 100644 --- a/src/commands/to_array.rs +++ b/src/commands/to_array.rs @@ -1,10 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::Value; use crate::prelude::*; -pub fn to_array( - args: CommandArgs, - _registry: &CommandRegistry, -) -> Result { +pub struct ToArray; + +impl WholeStreamCommand for ToArray { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + to_array(args, registry) + } + + fn name(&self) -> &str { + "to-array" + } + + fn signature(&self) -> Signature { + Signature::build("to-array") + } +} + +fn to_array(args: CommandArgs, _registry: &CommandRegistry) -> Result { let out = args.input.values.collect(); Ok(out diff --git a/src/commands/to_csv.rs b/src/commands/to_csv.rs index 04a99c5de..d4f4e03c6 100644 --- a/src/commands/to_csv.rs +++ b/src/commands/to_csv.rs @@ -1,7 +1,28 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, Value}; use crate::prelude::*; use csv::WriterBuilder; +pub struct ToCSV; + +impl WholeStreamCommand for ToCSV { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + to_csv(args, registry) + } + + fn name(&self) -> &str { + "to-csv" + } + + fn signature(&self) -> Signature { + Signature::build("to-csv") + } +} + pub fn value_to_csv_value(v: &Value) -> Value { match v { Value::Primitive(Primitive::String(s)) => Value::Primitive(Primitive::String(s.clone())), @@ -36,7 +57,7 @@ pub fn to_string(v: &Value) -> Result> { } } -pub fn to_csv(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn to_csv(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let name_span = args.name_span(); let out = args.input; diff --git a/src/commands/to_json.rs b/src/commands/to_json.rs index b52fa4cfd..d61ce909b 100644 --- a/src/commands/to_json.rs +++ b/src/commands/to_json.rs @@ -1,6 +1,27 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, Value}; use crate::prelude::*; +pub struct ToJSON; + +impl WholeStreamCommand for ToJSON { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + to_json(args, registry) + } + + fn name(&self) -> &str { + "to-json" + } + + fn signature(&self) -> Signature { + Signature::build("to-json") + } +} + pub fn value_to_json_value(v: &Value) -> serde_json::Value { match v { Value::Primitive(Primitive::Boolean(b)) => serde_json::Value::Bool(*b), @@ -41,7 +62,7 @@ pub fn value_to_json_value(v: &Value) -> serde_json::Value { } } -pub fn to_json(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn to_json(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let name_span = args.name_span(); let out = args.input; diff --git a/src/commands/to_toml.rs b/src/commands/to_toml.rs index 5020a9556..074a74a51 100644 --- a/src/commands/to_toml.rs +++ b/src/commands/to_toml.rs @@ -1,6 +1,27 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, Value}; use crate::prelude::*; +pub struct ToTOML; + +impl WholeStreamCommand for ToTOML { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + to_toml(args, registry) + } + + fn name(&self) -> &str { + "to-toml" + } + + fn signature(&self) -> Signature { + Signature::build("to-toml") + } +} + pub fn value_to_toml_value(v: &Value) -> toml::Value { match v { Value::Primitive(Primitive::Boolean(b)) => toml::Value::Boolean(*b), @@ -33,7 +54,7 @@ pub fn value_to_toml_value(v: &Value) -> toml::Value { } } -pub fn to_toml(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn to_toml(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let name_span = args.name_span(); let out = args.input; diff --git a/src/commands/to_yaml.rs b/src/commands/to_yaml.rs index aa1fba4fc..e490954f2 100644 --- a/src/commands/to_yaml.rs +++ b/src/commands/to_yaml.rs @@ -1,6 +1,27 @@ +use crate::commands::WholeStreamCommand; use crate::object::{Primitive, Value}; use crate::prelude::*; +pub struct ToYAML; + +impl WholeStreamCommand for ToYAML { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + to_yaml(args, registry) + } + + fn name(&self) -> &str { + "to-yaml" + } + + fn signature(&self) -> Signature { + Signature::build("to-yaml") + } +} + pub fn value_to_yaml_value(v: &Value) -> serde_yaml::Value { match v { Value::Primitive(Primitive::Boolean(b)) => serde_yaml::Value::Bool(*b), @@ -39,7 +60,7 @@ pub fn value_to_yaml_value(v: &Value) -> serde_yaml::Value { } } -pub fn to_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result { +fn to_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result { let args = args.evaluate_once(registry)?; let name_span = args.name_span(); let out = args.input; diff --git a/src/commands/trim.rs b/src/commands/trim.rs index 1d3c76e90..22a29a36b 100644 --- a/src/commands/trim.rs +++ b/src/commands/trim.rs @@ -1,8 +1,29 @@ +use crate::commands::WholeStreamCommand; use crate::errors::ShellError; use crate::object::Value; use crate::prelude::*; -pub fn trim(args: CommandArgs, _registry: &CommandRegistry) -> Result { +pub struct Trim; + +impl WholeStreamCommand for Trim { + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + trim(args, registry) + } + + fn name(&self) -> &str { + "trim" + } + + fn signature(&self) -> Signature { + Signature::build("trim") + } +} + +fn trim(args: CommandArgs, _registry: &CommandRegistry) -> Result { let input = args.input; Ok(input