Merge pull request #611 from jonathandturner/autoview_plugin

Protect autoview against missing plugins
This commit is contained in:
Jonathan Turner 2019-09-07 20:09:58 +12:00 committed by GitHub
commit 8be14a891d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 29 deletions

View File

@ -43,17 +43,36 @@ pub fn autoview(
.. ..
} = input[0usize] } = input[0usize]
{ {
let binary = context.expect_command("binaryview"); let binary = context.get_command("binaryview");
let result = binary.run(raw.with_input(input), &context.commands); if let Some(binary) = binary {
result.collect::<Vec<_>>().await; let result = binary.run(raw.with_input(input), &context.commands);
result.collect::<Vec<_>>().await;
} else {
for i in input {
match i.item {
Value::Binary(b) => {
use pretty_hex::*;
println!("{:?}", b.hex_dump());
}
_ => {}
}
}
};
} else if is_single_text_value(&input) { } else if is_single_text_value(&input) {
let text = context.expect_command("textview"); let text = context.get_command("textview");
let result = text.run(raw.with_input(input), &context.commands); if let Some(text) = text {
result.collect::<Vec<_>>().await; let result = text.run(raw.with_input(input), &context.commands);
} else if equal_shapes(&input) { result.collect::<Vec<_>>().await;
let table = context.expect_command("table"); } else {
let result = table.run(raw.with_input(input), &context.commands); for i in input {
result.collect::<Vec<_>>().await; match i.item {
Value::Primitive(Primitive::String(s)) => {
println!("{}", s);
}
_ => {}
}
}
}
} else { } else {
let table = context.expect_command("table"); let table = context.expect_command("table");
let result = table.run(raw.with_input(input), &context.commands); let result = table.run(raw.with_input(input), &context.commands);
@ -63,25 +82,6 @@ pub fn autoview(
})) }))
} }
fn equal_shapes(input: &Vec<Tagged<Value>>) -> bool {
let mut items = input.iter();
let item = match items.next() {
Some(item) => item,
None => return false,
};
let desc = item.data_descriptors();
for item in items {
if desc != item.data_descriptors() {
return false;
}
}
true
}
fn is_single_text_value(input: &Vec<Tagged<Value>>) -> bool { fn is_single_text_value(input: &Vec<Tagged<Value>>) -> bool {
if input.len() != 1 { if input.len() != 1 {
return false; return false;

View File

@ -236,6 +236,10 @@ impl RunnableContext {
.get_command(name) .get_command(name)
.expect(&format!("Expected command {}", name)) .expect(&format!("Expected command {}", name))
} }
pub fn get_command(&self, name: &str) -> Option<Arc<Command>> {
self.commands.get_command(name)
}
} }
pub struct RunnablePerItemArgs<T> { pub struct RunnablePerItemArgs<T> {