1
0

Add type checks

This commit is contained in:
Jeff 2023-08-24 16:26:48 -04:00
parent 8d04e0bb04
commit 8721ad41b1
6 changed files with 35 additions and 41 deletions

View File

@ -1,4 +1,6 @@
//! Macros for collection values: strings, lists, maps and tables. //! Macros for collection values: strings, lists, maps and tables.
//!
//! Tests for this module are written in Dust and can be found at tests/collections.ds.
use crate::{Error, Result, Table, Tool, ToolInfo, Value, ValueType, VariableMap}; use crate::{Error, Result, Table, Tool, ToolInfo, Value, ValueType, VariableMap};

View File

@ -1,6 +1,6 @@
use std::process::Command; use std::process::Command;
use crate::{Result, Tool, ToolInfo, Value}; use crate::{Result, Tool, ToolInfo, Value, ValueType};
pub struct Sh; pub struct Sh;
@ -10,7 +10,7 @@ impl Tool for Sh {
identifier: "sh", identifier: "sh",
description: "Pass input to the Bourne Shell.", description: "Pass input to the Bourne Shell.",
group: "command", group: "command",
inputs: vec![], inputs: vec![ValueType::String],
} }
} }
@ -31,7 +31,7 @@ impl Tool for Bash {
identifier: "bash", identifier: "bash",
description: "Pass input to the Bourne Again Shell.", description: "Pass input to the Bourne Again Shell.",
group: "command", group: "command",
inputs: vec![], inputs: vec![ValueType::String],
} }
} }
@ -55,7 +55,7 @@ impl Tool for Fish {
identifier: "fish", identifier: "fish",
description: "Pass input to the fish shell.", description: "Pass input to the fish shell.",
group: "command", group: "command",
inputs: vec![], inputs: vec![ValueType::String],
} }
} }
@ -80,7 +80,7 @@ impl Tool for Zsh {
identifier: "zsh", identifier: "zsh",
description: "Pass input to the Z shell.", description: "Pass input to the Z shell.",
group: "command", group: "command",
inputs: vec![], inputs: vec![ValueType::String],
} }
} }
@ -105,7 +105,7 @@ impl Tool for Raw {
identifier: "raw", identifier: "raw",
description: "Run input as a command without a shell", description: "Run input as a command without a shell",
group: "command", group: "command",
inputs: vec![], inputs: vec![ValueType::String],
} }
} }

View File

@ -30,7 +30,7 @@ impl Tool for ToJson {
identifier: "to_json", identifier: "to_json",
description: "Create a JSON string from a whale value.", description: "Create a JSON string from a whale value.",
group: "data", group: "data",
inputs: vec![], inputs: vec![ValueType::Any],
} }
} }
@ -96,7 +96,7 @@ impl Tool for ToCsv {
identifier: "to_csv", identifier: "to_csv",
description: "Convert a value to a string of comma-separated values.", description: "Convert a value to a string of comma-separated values.",
group: "data", group: "data",
inputs: vec![], inputs: vec![ValueType::Any],
} }
} }

View File

@ -2,7 +2,7 @@ use std::process::Command;
use sysinfo::{DiskExt, System, SystemExt}; use sysinfo::{DiskExt, System, SystemExt};
use crate::{Result, Table, Tool, ToolInfo, Value}; use crate::{Result, Table, Tool, ToolInfo, Value, ValueType};
pub struct ListDisks; pub struct ListDisks;
@ -12,7 +12,7 @@ impl Tool for ListDisks {
identifier: "list_disks", identifier: "list_disks",
description: "List all block devices.", description: "List all block devices.",
group: "disks", group: "disks",
inputs: vec![], inputs: vec![ValueType::Empty],
} }
} }
@ -66,7 +66,7 @@ impl Tool for Partition {
identifier: "partition", identifier: "partition",
description: "Partition a disk, clearing its content.", description: "Partition a disk, clearing its content.",
group: "disks", group: "disks",
inputs: vec![], inputs: vec![ValueType::Map],
} }
} }

View File

@ -1,8 +1,8 @@
use std::{fs, thread::sleep, time::Duration}; use std::{thread::sleep, time::Duration};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use crate::{Function, Result, Tool, ToolInfo, Value}; use crate::{Result, Tool, ToolInfo, Value, ValueType};
pub struct Output; pub struct Output;
@ -12,7 +12,7 @@ impl Tool for Output {
identifier: "output", identifier: "output",
description: "Print a value.", description: "Print a value.",
group: "general", group: "general",
inputs: vec![], inputs: vec![ValueType::Any],
} }
} }
@ -30,7 +30,10 @@ impl Tool for Repeat {
identifier: "repeat", identifier: "repeat",
description: "Run a function the given number of times.", description: "Run a function the given number of times.",
group: "general", group: "general",
inputs: vec![], inputs: vec![ValueType::ListExact(vec![
ValueType::Function,
ValueType::Integer,
])],
} }
} }
@ -50,35 +53,15 @@ impl Tool for Repeat {
} }
} }
pub struct Run;
impl Tool for Run {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "run",
description: "Run a whale file.",
group: "general",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let path = argument.as_string()?;
let file_contents = fs::read_to_string(path)?;
Function::new(&file_contents).run()
}
}
pub struct Async; pub struct Async;
impl Tool for Async { impl Tool for Async {
fn info(&self) -> ToolInfo<'static> { fn info(&self) -> ToolInfo<'static> {
ToolInfo { ToolInfo {
identifier: "async", identifier: "run",
description: "Run functions in parallel.", description: "Run functions in parallel.",
group: "general", group: "general",
inputs: vec![], inputs: vec![ValueType::ListOf(Box::new(ValueType::Function))],
} }
} }
@ -112,7 +95,7 @@ impl Tool for Wait {
identifier: "wait", identifier: "wait",
description: "Wait for the given number of milliseconds.", description: "Wait for the given number of milliseconds.",
group: "general", group: "general",
inputs: vec![], inputs: vec![ValueType::Integer],
} }
} }

View File

@ -51,7 +51,7 @@ pub mod time;
/// Master list of all tools. /// Master list of all tools.
/// ///
/// This list is used to match identifiers with tools and to provide info to the shell. /// This list is used to match identifiers with tools and to provide info to the shell.
pub const TOOL_LIST: [&'static dyn Tool; 56] = [ pub const TOOL_LIST: [&'static dyn Tool; 55] = [
&collections::Count, &collections::Count,
&collections::CreateTable, &collections::CreateTable,
&collections::Insert, &collections::Insert,
@ -84,7 +84,6 @@ pub const TOOL_LIST: [&'static dyn Tool; 56] = [
&general::Async, &general::Async,
&general::Output, &general::Output,
&general::Repeat, &general::Repeat,
&general::Run,
&general::Wait, &general::Wait,
&gui::BarGraph, &gui::BarGraph,
&gui::Plot, &gui::Plot,
@ -385,7 +384,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn macro_formatting() { fn tool_identifier_formatting() {
for function in TOOL_LIST { for function in TOOL_LIST {
let identifier = function.info().identifier; let identifier = function.info().identifier;
@ -398,4 +397,14 @@ mod tests {
assert!(!identifier.contains('-')); assert!(!identifier.contains('-'));
} }
} }
#[test]
fn tool_inputs_exist() {
for function in TOOL_LIST {
let identifier = function.info().identifier;
let input_count = function.info().inputs.len();
assert!(input_count > 0, "{} has no inputs declared", identifier);
}
}
} }