2019-07-28 07:01:32 +00:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu::{
|
|
|
|
serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive,
|
2019-07-28 23:34:37 +00:00
|
|
|
ReturnSuccess, ReturnValue, ShellError, Spanned, Value,
|
2019-07-28 07:01:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Str {
|
|
|
|
field: Option<String>,
|
2019-07-28 23:34:37 +00:00
|
|
|
error: Option<String>,
|
2019-07-28 07:01:32 +00:00
|
|
|
downcase: bool,
|
|
|
|
upcase: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Str {
|
|
|
|
fn new() -> Str {
|
|
|
|
Str {
|
|
|
|
field: None,
|
2019-07-28 23:34:37 +00:00
|
|
|
error: None,
|
2019-07-28 07:01:32 +00:00
|
|
|
downcase: false,
|
|
|
|
upcase: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:13:06 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-07-29 02:30:47 +00:00
|
|
|
fn for_input(&mut self, field: String) {
|
|
|
|
self.field = Some(field);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn for_downcase(&mut self) {
|
2019-07-28 23:34:37 +00:00
|
|
|
self.downcase = true;
|
|
|
|
|
|
|
|
if !self.is_valid() {
|
|
|
|
self.log_error("can only apply one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 02:30:47 +00:00
|
|
|
fn for_upcase(&mut self) {
|
2019-07-28 23:34:37 +00:00
|
|
|
self.upcase = true;
|
|
|
|
|
|
|
|
if !self.is_valid() {
|
|
|
|
self.log_error("can only apply one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 02:30:47 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-07-28 23:34:37 +00:00
|
|
|
fn usage(&self) -> &'static str {
|
|
|
|
"Usage: str [--downcase, --upcase]"
|
|
|
|
}
|
2019-07-29 02:30:47 +00:00
|
|
|
}
|
2019-07-28 23:34:37 +00:00
|
|
|
|
2019-07-29 02:30:47 +00:00
|
|
|
impl Str {
|
2019-07-28 07:01:32 +00:00
|
|
|
fn strutils(
|
|
|
|
&self,
|
|
|
|
value: Spanned<Value>,
|
|
|
|
field: &Option<String>,
|
|
|
|
) -> Result<Spanned<Value>, ShellError> {
|
|
|
|
match value.item {
|
2019-07-29 02:30:47 +00:00
|
|
|
Value::Primitive(Primitive::String(s)) => Ok(Spanned {
|
|
|
|
item: Value::string(self.apply(&s)),
|
|
|
|
span: value.span,
|
|
|
|
}),
|
2019-07-28 07:01:32 +00:00
|
|
|
Value::Object(_) => match field {
|
|
|
|
Some(f) => {
|
|
|
|
let replacement = match value.item.get_data_by_path(value.span, f) {
|
|
|
|
Some(result) => self.strutils(result.map(|x| x.clone()), &None)?,
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::string("str could not find field to replace"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match value
|
|
|
|
.item
|
|
|
|
.replace_data_at_path(value.span, f, replacement.item.clone())
|
|
|
|
{
|
|
|
|
Some(v) => return Ok(v),
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::string("str could not find field to replace"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(ShellError::string(
|
|
|
|
"str needs a field when applying it to a value in an object",
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
x => Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in stream: {:?}",
|
|
|
|
x
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Str {
|
|
|
|
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
|
|
|
let mut named = IndexMap::new();
|
|
|
|
named.insert("downcase".to_string(), NamedType::Switch);
|
|
|
|
named.insert("upcase".to_string(), NamedType::Switch);
|
|
|
|
|
|
|
|
Ok(CommandConfig {
|
|
|
|
name: "str".to_string(),
|
|
|
|
positional: vec![PositionalType::optional_any("Field")],
|
|
|
|
is_filter: true,
|
|
|
|
is_sink: false,
|
|
|
|
named,
|
|
|
|
rest_positional: true,
|
|
|
|
})
|
|
|
|
}
|
2019-07-28 23:34:37 +00:00
|
|
|
|
2019-07-28 07:01:32 +00:00
|
|
|
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
|
|
|
|
if call_info.args.has("downcase") {
|
2019-07-29 02:30:47 +00:00
|
|
|
self.for_downcase();
|
2019-07-28 23:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if call_info.args.has("upcase") {
|
2019-07-29 02:30:47 +00:00
|
|
|
self.for_upcase();
|
2019-07-28 07:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(args) = call_info.args.positional {
|
|
|
|
for arg in args {
|
|
|
|
match arg {
|
|
|
|
Spanned {
|
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
|
|
|
..
|
|
|
|
} => {
|
2019-07-29 02:30:47 +00:00
|
|
|
self.for_input(s);
|
2019-07-28 07:01:32 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in params: {:?}",
|
|
|
|
arg
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 23:34:37 +00:00
|
|
|
match &self.error {
|
|
|
|
Some(reason) => {
|
2019-07-29 02:30:47 +00:00
|
|
|
return Err(ShellError::string(format!("{}: {}", reason, self.usage())))
|
2019-07-28 23:34:37 +00:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
2019-07-28 07:01:32 +00:00
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
|
|
|
Ok(vec![ReturnSuccess::value(
|
|
|
|
self.strutils(input, &self.field)?,
|
|
|
|
)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
serve_plugin(&mut Str::new());
|
|
|
|
}
|