From 28fe31d5651c77efe57ff369459a8d0e2a9143b2 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 7 Sep 2019 19:32:07 +1200 Subject: [PATCH] Protect autoview against missing plugins --- src/commands/autoview.rs | 58 ++++++++++++++++++++-------------------- src/commands/command.rs | 4 +++ 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index 844a093c4..f970d23e4 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -43,17 +43,36 @@ pub fn autoview( .. } = input[0usize] { - let binary = context.expect_command("binaryview"); - let result = binary.run(raw.with_input(input), &context.commands); - result.collect::>().await; + let binary = context.get_command("binaryview"); + if let Some(binary) = binary { + let result = binary.run(raw.with_input(input), &context.commands); + result.collect::>().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) { - let text = context.expect_command("textview"); - let result = text.run(raw.with_input(input), &context.commands); - result.collect::>().await; - } else if equal_shapes(&input) { - let table = context.expect_command("table"); - let result = table.run(raw.with_input(input), &context.commands); - result.collect::>().await; + let text = context.get_command("textview"); + if let Some(text) = text { + let result = text.run(raw.with_input(input), &context.commands); + result.collect::>().await; + } else { + for i in input { + match i.item { + Value::Primitive(Primitive::String(s)) => { + println!("{}", s); + } + _ => {} + } + } + } } else { let table = context.expect_command("table"); let result = table.run(raw.with_input(input), &context.commands); @@ -63,25 +82,6 @@ pub fn autoview( })) } -fn equal_shapes(input: &Vec>) -> 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>) -> bool { if input.len() != 1 { return false; diff --git a/src/commands/command.rs b/src/commands/command.rs index a79e797fd..a719f33b7 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -236,6 +236,10 @@ impl RunnableContext { .get_command(name) .expect(&format!("Expected command {}", name)) } + + pub fn get_command(&self, name: &str) -> Option> { + self.commands.get_command(name) + } } pub struct RunnablePerItemArgs {