From 7c4706ee503ef69d40e8002fdf4161e619730a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Sun, 28 Jul 2019 18:34:37 -0500 Subject: [PATCH] Validation baseline. --- src/plugin.rs | 9 ++++++++ src/plugins/str.rs | 52 +++++++++++++++++++++++++++++++++++++++---- tests/filters_test.rs | 11 +++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/plugin.rs b/src/plugin.rs index 51366b3237..bbac5d6595 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -4,6 +4,15 @@ use std::io; pub trait Plugin { fn config(&mut self) -> Result; + + #[allow(unused)] + fn is_valid(&self) -> bool { + true + } + + #[allow(unused)] + fn log_error(&mut self, message: &str) { } + #[allow(unused)] fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { Ok(vec![]) diff --git a/src/plugins/str.rs b/src/plugins/str.rs index 7fdfcb21e5..a929def8dd 100644 --- a/src/plugins/str.rs +++ b/src/plugins/str.rs @@ -1,11 +1,12 @@ use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive, - ReturnSuccess, ReturnValue, ShellError, Spanned, SpannedItem, Value, + ReturnSuccess, ReturnValue, ShellError, Spanned, Value, }; struct Str { field: Option, + error: Option, downcase: bool, upcase: bool, } @@ -14,11 +15,32 @@ impl Str { fn new() -> Str { Str { field: None, + error: None, downcase: false, upcase: false, } } + fn to_downcase(&mut self) { + self.downcase = true; + + if !self.is_valid() { + self.log_error("can only apply one") + } + } + + fn to_upcase(&mut self) { + self.upcase = true; + + if !self.is_valid() { + self.log_error("can only apply one") + } + } + + fn usage(&self) -> &'static str { + "Usage: str [--downcase, --upcase]" + } + fn strutils( &self, value: Spanned, @@ -84,11 +106,22 @@ impl Plugin for Str { rest_positional: true, }) } + + fn is_valid(&self) -> bool { + (self.downcase && !self.upcase) || (!self.downcase && self.upcase) + } + + fn log_error(&mut self, message: &str) { + self.error = Some(message.to_string()); + } + fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if call_info.args.has("downcase") { - self.downcase = true; - } else if call_info.args.has("upcase") { - self.upcase = true; + self.to_downcase(); + } + + if call_info.args.has("upcase") { + self.to_upcase(); } if let Some(args) = call_info.args.positional { @@ -110,6 +143,17 @@ impl Plugin for Str { } } + match &self.error { + Some(reason) => { + return Err(ShellError::string(format!( + "{}: {}", + reason, + self.usage() + ))) + } + None => {} + } + Ok(vec![]) } diff --git a/tests/filters_test.rs b/tests/filters_test.rs index 5660fbaee6..6c542b4424 100644 --- a/tests/filters_test.rs +++ b/tests/filters_test.rs @@ -65,6 +65,17 @@ fn can_split_by_column() { assert_eq!(output, "name"); } +#[test] +fn str_can_only_apply_one() { + nu_error!( + output, + cwd("tests/fixtures/formats"), + "open caco3_plastics.csv | first 1 | str origin --downcase --upcase" + ); + + assert!(output.contains("Usage: str [--downcase, --upcase]")); +} + #[test] fn str_downcases() { nu!(