From 478ccbb5297919a58b15e8618b0998c4303a9408 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 28 Aug 2023 17:45:55 -0400 Subject: [PATCH] Add help tool --- src/bin/dust.rs | 2 +- src/tools/general.rs | 32 +++++++++++++++++++++++++++++++- src/tools/mod.rs | 3 ++- src/value/value_type.rs | 8 +++----- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/bin/dust.rs b/src/bin/dust.rs index b692f94..b727502 100644 --- a/src/bin/dust.rs +++ b/src/bin/dust.rs @@ -150,7 +150,7 @@ fn run_cli_shell() { let eval_result = eval_with_context(line, &mut context); match eval_result { - Ok(_) => {} + Ok(value) => println!("{value}"), Err(error) => eprintln!("{error}"), } } diff --git a/src/tools/general.rs b/src/tools/general.rs index 886a7c6..016cf1e 100644 --- a/src/tools/general.rs +++ b/src/tools/general.rs @@ -2,7 +2,37 @@ use std::{thread::sleep, time::Duration}; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; -use crate::{Result, Tool, ToolInfo, Value, ValueType}; +use crate::{Result, Table, Tool, ToolInfo, Value, ValueType, TOOL_LIST}; + +pub struct Help; + +impl Tool for Help { + fn info(&self) -> ToolInfo<'static> { + ToolInfo { + identifier: "help", + description: "Get help using dust.", + group: "general", + inputs: vec![ValueType::Empty, ValueType::String], + } + } + + fn run(&self, argument: &Value) -> Result { + self.check_type(argument)?; + + let mut table = Table::new(vec!["tool".to_string(), "description".to_string()]); + + for tool in TOOL_LIST { + let row = vec![ + Value::String(tool.info().identifier.to_string()), + Value::String(tool.info().description.to_string()), + ]; + + table.insert(row)?; + } + + Ok(Value::Table(table)) + } +} pub struct Output; diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 9ee5135..17ee210 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -50,7 +50,7 @@ pub mod time; /// Master list of all tools. /// /// This list is used to match identifiers with tools and to provide info to the shell. -pub const TOOL_LIST: [&'static dyn Tool; 51] = [ +pub const TOOL_LIST: [&'static dyn Tool; 52] = [ &collections::Count, &collections::CreateTable, &collections::Insert, @@ -82,6 +82,7 @@ pub const TOOL_LIST: [&'static dyn Tool; 51] = [ &filesystem::Trash, &filesystem::Watch, &filesystem::Write, + &general::Help, &general::Run, &general::Output, &general::Repeat, diff --git a/src/value/value_type.rs b/src/value/value_type.rs index dee3760..3b0a53d 100644 --- a/src/value/value_type.rs +++ b/src/value/value_type.rs @@ -38,11 +38,9 @@ impl PartialEq for ValueType { (ValueType::ListOf(_), ValueType::List) => true, (ValueType::List, ValueType::ListOf(_)) => true, (ValueType::ListOf(value_type), ValueType::ListExact(exact_list)) - | (ValueType::ListExact(exact_list), ValueType::ListOf(value_type)) => { - exact_list - .iter() - .all(|exact_type| exact_type == value_type.as_ref()) - } + | (ValueType::ListExact(exact_list), ValueType::ListOf(value_type)) => exact_list + .iter() + .all(|exact_type| exact_type == value_type.as_ref()), (ValueType::List, ValueType::List) => true, (ValueType::Empty, ValueType::Empty) => true, (ValueType::Map, ValueType::Map) => true,