Validation baseline.

This commit is contained in:
Andrés N. Robalino 2019-07-28 18:34:37 -05:00
parent 59dec999b8
commit 7c4706ee50
3 changed files with 68 additions and 4 deletions

View File

@ -4,6 +4,15 @@ use std::io;
pub trait Plugin { pub trait Plugin {
fn config(&mut self) -> Result<CommandConfig, ShellError>; fn config(&mut self) -> Result<CommandConfig, ShellError>;
#[allow(unused)]
fn is_valid(&self) -> bool {
true
}
#[allow(unused)]
fn log_error(&mut self, message: &str) { }
#[allow(unused)] #[allow(unused)]
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> { fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![]) Ok(vec![])

View File

@ -1,11 +1,12 @@
use indexmap::IndexMap; use indexmap::IndexMap;
use nu::{ use nu::{
serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive, serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive,
ReturnSuccess, ReturnValue, ShellError, Spanned, SpannedItem, Value, ReturnSuccess, ReturnValue, ShellError, Spanned, Value,
}; };
struct Str { struct Str {
field: Option<String>, field: Option<String>,
error: Option<String>,
downcase: bool, downcase: bool,
upcase: bool, upcase: bool,
} }
@ -14,11 +15,32 @@ impl Str {
fn new() -> Str { fn new() -> Str {
Str { Str {
field: None, field: None,
error: None,
downcase: false, downcase: false,
upcase: 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( fn strutils(
&self, &self,
value: Spanned<Value>, value: Spanned<Value>,
@ -84,11 +106,22 @@ impl Plugin for Str {
rest_positional: true, 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<Vec<ReturnValue>, ShellError> { fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
if call_info.args.has("downcase") { if call_info.args.has("downcase") {
self.downcase = true; self.to_downcase();
} else if call_info.args.has("upcase") { }
self.upcase = true;
if call_info.args.has("upcase") {
self.to_upcase();
} }
if let Some(args) = call_info.args.positional { 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![]) Ok(vec![])
} }

View File

@ -65,6 +65,17 @@ fn can_split_by_column() {
assert_eq!(output, "name"); 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] #[test]
fn str_downcases() { fn str_downcases() {
nu!( nu!(