Separate Nu plugin logic.

This commit is contained in:
Andrés N. Robalino 2019-07-28 21:30:47 -05:00
parent 87b299739c
commit be4262e96a

View file

@ -29,7 +29,11 @@ impl Str {
self.error = Some(message.to_string()); self.error = Some(message.to_string());
} }
fn to_downcase(&mut self) { fn for_input(&mut self, field: String) {
self.field = Some(field);
}
fn for_downcase(&mut self) {
self.downcase = true; self.downcase = true;
if !self.is_valid() { if !self.is_valid() {
@ -37,7 +41,7 @@ impl Str {
} }
} }
fn to_upcase(&mut self) { fn for_upcase(&mut self) {
self.upcase = true; self.upcase = true;
if !self.is_valid() { if !self.is_valid() {
@ -45,30 +49,34 @@ impl Str {
} }
} }
fn apply(&self, input: &str) -> String {
if self.downcase {
return input.to_ascii_lowercase();
}
if self.upcase {
return input.to_ascii_uppercase();
}
input.to_string()
}
fn usage(&self) -> &'static str { fn usage(&self) -> &'static str {
"Usage: str [--downcase, --upcase]" "Usage: str [--downcase, --upcase]"
} }
}
impl Str {
fn strutils( fn strutils(
&self, &self,
value: Spanned<Value>, value: Spanned<Value>,
field: &Option<String>, field: &Option<String>,
) -> Result<Spanned<Value>, ShellError> { ) -> Result<Spanned<Value>, ShellError> {
match value.item { match value.item {
Value::Primitive(Primitive::String(s)) => { Value::Primitive(Primitive::String(s)) => Ok(Spanned {
let applied = if self.downcase { item: Value::string(self.apply(&s)),
Value::string(s.to_ascii_lowercase()) span: value.span,
} else if self.upcase { }),
Value::string(s.to_ascii_uppercase())
} else {
Value::string(s)
};
Ok(Spanned {
item: applied,
span: value.span,
})
}
Value::Object(_) => match field { Value::Object(_) => match field {
Some(f) => { Some(f) => {
let replacement = match value.item.get_data_by_path(value.span, f) { let replacement = match value.item.get_data_by_path(value.span, f) {
@ -117,11 +125,11 @@ impl Plugin for Str {
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.to_downcase(); self.for_downcase();
} }
if call_info.args.has("upcase") { if call_info.args.has("upcase") {
self.to_upcase(); self.for_upcase();
} }
if let Some(args) = call_info.args.positional { if let Some(args) = call_info.args.positional {
@ -131,7 +139,7 @@ impl Plugin for Str {
item: Value::Primitive(Primitive::String(s)), item: Value::Primitive(Primitive::String(s)),
.. ..
} => { } => {
self.field = Some(s); self.for_input(s);
} }
_ => { _ => {
return Err(ShellError::string(format!( return Err(ShellError::string(format!(
@ -145,11 +153,7 @@ impl Plugin for Str {
match &self.error { match &self.error {
Some(reason) => { Some(reason) => {
return Err(ShellError::string(format!( return Err(ShellError::string(format!("{}: {}", reason, self.usage())))
"{}: {}",
reason,
self.usage()
)))
} }
None => {} None => {}
} }