2019-07-28 07:01:32 +00:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu::{
|
|
|
|
serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, PositionalType, Primitive,
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess, ReturnValue, ShellError, Tagged, Value,
|
2019-07-28 07:01:32 +00:00
|
|
|
};
|
|
|
|
|
2019-07-31 22:26:04 +00:00
|
|
|
enum Action {
|
|
|
|
Downcase,
|
|
|
|
Upcase,
|
|
|
|
ToInteger,
|
|
|
|
}
|
|
|
|
|
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-31 22:26:04 +00:00
|
|
|
action: Option<Action>,
|
2019-07-28 07:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Str {
|
|
|
|
fn new() -> Str {
|
|
|
|
Str {
|
|
|
|
field: None,
|
2019-07-28 23:34:37 +00:00
|
|
|
error: None,
|
2019-07-31 22:26:04 +00:00
|
|
|
action: None,
|
2019-07-28 07:01:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:26:04 +00:00
|
|
|
fn apply(&self, input: &str) -> Value {
|
|
|
|
match self.action {
|
|
|
|
Some(Action::Downcase) => Value::string(input.to_ascii_lowercase()),
|
|
|
|
Some(Action::Upcase) => Value::string(input.to_ascii_uppercase()),
|
|
|
|
Some(Action::ToInteger) => match input.trim().parse::<i64>() {
|
|
|
|
Ok(v) => Value::int(v),
|
|
|
|
Err(_) => Value::string(input),
|
2019-07-31 22:29:40 +00:00
|
|
|
},
|
2019-07-31 22:26:04 +00:00
|
|
|
None => Value::string(input.to_string()),
|
|
|
|
}
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 22:26:04 +00:00
|
|
|
fn for_input(&mut self, field: String) {
|
|
|
|
self.field = Some(field);
|
2019-07-29 01:13:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 00:19:31 +00:00
|
|
|
fn permit(&mut self) -> bool {
|
|
|
|
self.action.is_none()
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 01:13:06 +00:00
|
|
|
fn log_error(&mut self, message: &str) {
|
|
|
|
self.error = Some(message.to_string());
|
|
|
|
}
|
|
|
|
|
2019-07-30 16:24:24 +00:00
|
|
|
fn for_to_int(&mut self) {
|
2019-08-02 00:19:31 +00:00
|
|
|
if self.permit() {
|
|
|
|
self.action = Some(Action::ToInteger);
|
|
|
|
} else {
|
|
|
|
self.log_error("can only apply one");
|
|
|
|
}
|
2019-07-30 16:24:24 +00:00
|
|
|
}
|
2019-07-29 02:30:47 +00:00
|
|
|
|
|
|
|
fn for_downcase(&mut self) {
|
2019-08-02 00:19:31 +00:00
|
|
|
if self.permit() {
|
|
|
|
self.action = Some(Action::Downcase);
|
|
|
|
} else {
|
|
|
|
self.log_error("can only apply one");
|
|
|
|
}
|
2019-07-28 23:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 02:30:47 +00:00
|
|
|
fn for_upcase(&mut self) {
|
2019-08-02 00:19:31 +00:00
|
|
|
if self.permit() {
|
|
|
|
self.action = Some(Action::Upcase);
|
|
|
|
} else {
|
|
|
|
self.log_error("can only apply one");
|
|
|
|
}
|
2019-07-29 02:30:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 23:34:37 +00:00
|
|
|
fn usage(&self) -> &'static str {
|
2019-07-30 16:24:24 +00:00
|
|
|
"Usage: str field [--downcase|--upcase|--to-int]"
|
2019-07-28 23:34:37 +00:00
|
|
|
}
|
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,
|
2019-08-01 01:58:42 +00:00
|
|
|
value: Tagged<Value>,
|
2019-07-28 07:01:32 +00:00
|
|
|
field: &Option<String>,
|
2019-08-01 01:58:42 +00:00
|
|
|
) -> Result<Tagged<Value>, ShellError> {
|
2019-07-28 07:01:32 +00:00
|
|
|
match value.item {
|
2019-08-01 05:32:36 +00:00
|
|
|
Value::Primitive(Primitive::String(ref s)) => {
|
|
|
|
Ok(Tagged::from_item(self.apply(&s), value.span()))
|
|
|
|
}
|
2019-07-28 07:01:32 +00:00
|
|
|
Value::Object(_) => match field {
|
|
|
|
Some(f) => {
|
2019-08-01 01:58:42 +00:00
|
|
|
let replacement = match value.item.get_data_by_path(value.span(), f) {
|
2019-07-28 07:01:32 +00:00
|
|
|
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
|
2019-08-01 01:58:42 +00:00
|
|
|
.replace_data_at_path(value.span(), f, replacement.item.clone())
|
2019-07-28 07:01:32 +00:00
|
|
|
{
|
|
|
|
Some(v) => return Ok(v),
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::string("str could not find field to replace"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 11:41:24 +00:00
|
|
|
None => Err(ShellError::string(format!(
|
|
|
|
"{}: {}",
|
2019-07-28 07:01:32 +00:00
|
|
|
"str needs a field when applying it to a value in an object",
|
2019-07-30 11:41:24 +00:00
|
|
|
self.usage()
|
|
|
|
))),
|
2019-07-28 07:01:32 +00:00
|
|
|
},
|
|
|
|
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);
|
2019-07-30 16:24:24 +00:00
|
|
|
named.insert("to-int".to_string(), NamedType::Switch);
|
2019-07-28 07:01:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-30 16:24:24 +00:00
|
|
|
if call_info.args.has("to-int") {
|
|
|
|
self.for_to_int();
|
|
|
|
}
|
|
|
|
|
2019-07-28 07:01:32 +00:00
|
|
|
if let Some(args) = call_info.args.positional {
|
|
|
|
for arg in args {
|
|
|
|
match arg {
|
2019-08-01 01:58:42 +00:00
|
|
|
Tagged {
|
2019-07-28 07:01:32 +00:00
|
|
|
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
|
|
|
}
|
2019-07-30 11:41:24 +00:00
|
|
|
None => Ok(vec![]),
|
2019-07-28 23:34:37 +00:00
|
|
|
}
|
2019-07-28 07:01:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
2019-07-28 07:01:32 +00:00
|
|
|
Ok(vec![ReturnSuccess::value(
|
|
|
|
self.strutils(input, &self.field)?,
|
|
|
|
)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
serve_plugin(&mut Str::new());
|
|
|
|
}
|
2019-07-30 11:41:24 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::Str;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu::{
|
2019-08-01 05:32:36 +00:00
|
|
|
Args, CallInfo, Plugin, ReturnSuccess, SourceMap, Span, Tagged, TaggedDictBuilder,
|
|
|
|
TaggedItem, Value,
|
2019-07-30 11:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CallStub {
|
2019-08-01 05:32:36 +00:00
|
|
|
positionals: Vec<Tagged<Value>>,
|
|
|
|
flags: IndexMap<String, Tagged<Value>>,
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CallStub {
|
|
|
|
fn new() -> CallStub {
|
|
|
|
CallStub {
|
|
|
|
positionals: vec![],
|
|
|
|
flags: indexmap::IndexMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_long_flag(&mut self, name: &str) -> &mut Self {
|
|
|
|
self.flags.insert(
|
|
|
|
name.to_string(),
|
2019-08-01 05:32:36 +00:00
|
|
|
Value::boolean(true).tagged(Span::unknown()),
|
2019-07-30 11:41:24 +00:00
|
|
|
);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_parameter(&mut self, name: &str) -> &mut Self {
|
|
|
|
self.positionals
|
2019-08-01 05:32:36 +00:00
|
|
|
.push(Value::string(name.to_string()).tagged(Span::unknown()));
|
2019-07-30 11:41:24 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&self) -> CallInfo {
|
|
|
|
CallInfo {
|
|
|
|
args: Args::new(Some(self.positionals.clone()), Some(self.flags.clone())),
|
|
|
|
source_map: SourceMap::new(),
|
|
|
|
name_span: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:32:36 +00:00
|
|
|
fn sample_record(key: &str, value: &str) -> Tagged<Value> {
|
|
|
|
let mut record = TaggedDictBuilder::new(Span::unknown());
|
2019-07-30 16:29:11 +00:00
|
|
|
record.insert(key.clone(), Value::string(value));
|
2019-08-01 05:32:36 +00:00
|
|
|
record.into_tagged_value()
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 17:17:33 +00:00
|
|
|
#[test]
|
|
|
|
fn str_plugin_configuration_flags_wired() {
|
2019-07-30 17:46:49 +00:00
|
|
|
let mut plugin = Str::new();
|
2019-07-30 17:17:33 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
let configured = plugin.config().unwrap();
|
2019-07-30 17:17:33 +00:00
|
|
|
|
|
|
|
for action_flag in &["downcase", "upcase", "to-int"] {
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(configured.named.get(*action_flag).is_some());
|
2019-07-30 17:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:41:24 +00:00
|
|
|
#[test]
|
2019-07-30 17:46:49 +00:00
|
|
|
fn str_plugin_accepts_downcase() {
|
|
|
|
let mut plugin = Str::new();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 11:41:24 +00:00
|
|
|
.begin_filter(CallStub::new().with_long_flag("downcase").create())
|
|
|
|
.is_ok());
|
2019-07-31 22:26:04 +00:00
|
|
|
assert!(plugin.action.is_some());
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-07-30 17:46:49 +00:00
|
|
|
fn str_plugin_accepts_upcase() {
|
|
|
|
let mut plugin = Str::new();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 11:41:24 +00:00
|
|
|
.begin_filter(CallStub::new().with_long_flag("upcase").create())
|
|
|
|
.is_ok());
|
2019-07-31 22:26:04 +00:00
|
|
|
assert!(plugin.action.is_some());
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 16:24:24 +00:00
|
|
|
#[test]
|
2019-07-30 17:46:49 +00:00
|
|
|
fn str_plugin_accepts_to_int() {
|
|
|
|
let mut plugin = Str::new();
|
2019-07-30 16:24:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 16:24:24 +00:00
|
|
|
.begin_filter(CallStub::new().with_long_flag("to-int").create())
|
|
|
|
.is_ok());
|
2019-07-31 22:26:04 +00:00
|
|
|
assert!(plugin.action.is_some());
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-07-30 17:46:49 +00:00
|
|
|
fn str_plugin_accepts_field() {
|
|
|
|
let mut plugin = Str::new();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 11:41:24 +00:00
|
|
|
.begin_filter(
|
|
|
|
CallStub::new()
|
|
|
|
.with_parameter("package.description")
|
|
|
|
.create()
|
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
|
2019-07-30 17:54:20 +00:00
|
|
|
assert_eq!(plugin.field, Some("package.description".to_string()));
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 22:29:40 +00:00
|
|
|
#[test]
|
|
|
|
fn str_plugin_accepts_only_one_action() {
|
|
|
|
let mut plugin = Str::new();
|
|
|
|
|
|
|
|
assert!(plugin
|
|
|
|
.begin_filter(
|
|
|
|
CallStub::new()
|
|
|
|
.with_long_flag("upcase")
|
|
|
|
.with_long_flag("downcase")
|
|
|
|
.with_long_flag("to-int")
|
|
|
|
.create(),
|
|
|
|
)
|
|
|
|
.is_err());
|
|
|
|
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:37:57 +00:00
|
|
|
#[test]
|
|
|
|
fn str_downcases() {
|
|
|
|
let mut strutils = Str::new();
|
|
|
|
strutils.for_downcase();
|
2019-07-30 17:54:20 +00:00
|
|
|
assert_eq!(strutils.apply("ANDRES"), Value::string("andres"));
|
2019-07-30 13:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_upcases() {
|
|
|
|
let mut strutils = Str::new();
|
|
|
|
strutils.for_upcase();
|
2019-07-30 17:54:20 +00:00
|
|
|
assert_eq!(strutils.apply("andres"), Value::string("ANDRES"));
|
2019-07-30 16:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn str_to_int() {
|
|
|
|
let mut strutils = Str::new();
|
|
|
|
strutils.for_to_int();
|
2019-07-30 17:54:20 +00:00
|
|
|
assert_eq!(strutils.apply("9999"), Value::int(9999 as i64));
|
2019-07-30 13:37:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:41:24 +00:00
|
|
|
#[test]
|
2019-07-30 16:53:31 +00:00
|
|
|
fn str_plugin_applies_upcase() {
|
2019-07-30 17:46:49 +00:00
|
|
|
let mut plugin = Str::new();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 11:41:24 +00:00
|
|
|
.begin_filter(
|
|
|
|
CallStub::new()
|
|
|
|
.with_long_flag("upcase")
|
|
|
|
.with_parameter("name")
|
|
|
|
.create()
|
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
|
2019-07-30 16:24:24 +00:00
|
|
|
let subject = sample_record("name", "jotandrehuda");
|
2019-07-30 17:46:49 +00:00
|
|
|
let output = plugin.filter(subject).unwrap();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
|
|
|
match output[0].as_ref().unwrap() {
|
2019-08-01 05:32:36 +00:00
|
|
|
ReturnSuccess::Value(Tagged {
|
2019-07-30 11:41:24 +00:00
|
|
|
item: Value::Object(o),
|
|
|
|
..
|
|
|
|
}) => assert_eq!(
|
|
|
|
*o.get_data(&String::from("name")).borrow(),
|
|
|
|
Value::string(String::from("JOTANDREHUDA"))
|
|
|
|
),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-07-30 16:53:31 +00:00
|
|
|
fn str_plugin_applies_downcase() {
|
2019-07-30 17:46:49 +00:00
|
|
|
let mut plugin = Str::new();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 11:41:24 +00:00
|
|
|
.begin_filter(
|
|
|
|
CallStub::new()
|
|
|
|
.with_long_flag("downcase")
|
|
|
|
.with_parameter("name")
|
|
|
|
.create()
|
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
|
2019-07-30 16:24:24 +00:00
|
|
|
let subject = sample_record("name", "JOTANDREHUDA");
|
2019-07-30 17:46:49 +00:00
|
|
|
let output = plugin.filter(subject).unwrap();
|
2019-07-30 11:41:24 +00:00
|
|
|
|
|
|
|
match output[0].as_ref().unwrap() {
|
2019-08-01 05:32:36 +00:00
|
|
|
ReturnSuccess::Value(Tagged {
|
2019-07-30 11:41:24 +00:00
|
|
|
item: Value::Object(o),
|
|
|
|
..
|
|
|
|
}) => assert_eq!(
|
|
|
|
*o.get_data(&String::from("name")).borrow(),
|
|
|
|
Value::string(String::from("jotandrehuda"))
|
|
|
|
),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 16:24:24 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-07-30 16:53:31 +00:00
|
|
|
fn str_plugin_applies_to_int() {
|
2019-07-30 17:46:49 +00:00
|
|
|
let mut plugin = Str::new();
|
2019-07-30 16:24:24 +00:00
|
|
|
|
2019-07-30 17:46:49 +00:00
|
|
|
assert!(plugin
|
2019-07-30 16:24:24 +00:00
|
|
|
.begin_filter(
|
|
|
|
CallStub::new()
|
|
|
|
.with_long_flag("to-int")
|
|
|
|
.with_parameter("Nu_birthday")
|
|
|
|
.create()
|
|
|
|
)
|
|
|
|
.is_ok());
|
|
|
|
|
|
|
|
let subject = sample_record("Nu_birthday", "10");
|
2019-07-30 17:46:49 +00:00
|
|
|
let output = plugin.filter(subject).unwrap();
|
2019-07-30 16:24:24 +00:00
|
|
|
|
|
|
|
match output[0].as_ref().unwrap() {
|
2019-08-01 05:32:36 +00:00
|
|
|
ReturnSuccess::Value(Tagged {
|
2019-07-30 16:24:24 +00:00
|
|
|
item: Value::Object(o),
|
|
|
|
..
|
|
|
|
}) => assert_eq!(
|
|
|
|
*o.get_data(&String::from("Nu_birthday")).borrow(),
|
|
|
|
Value::int(10)
|
|
|
|
),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 11:41:24 +00:00
|
|
|
}
|