1
0

Add type checks

This commit is contained in:
Jeff 2023-08-24 16:44:26 -04:00
parent 8721ad41b1
commit eb953b0231
7 changed files with 15 additions and 243 deletions

View File

@ -53,9 +53,9 @@ impl Tool for Repeat {
} }
} }
pub struct Async; pub struct Run;
impl Tool for Async { impl Tool for Run {
fn info(&self) -> ToolInfo<'static> { fn info(&self) -> ToolInfo<'static> {
ToolInfo { ToolInfo {
identifier: "run", identifier: "run",

View File

@ -9,34 +9,9 @@ use eframe::{
}; };
use egui_extras::{Column, StripBuilder, TableBuilder}; use egui_extras::{Column, StripBuilder, TableBuilder};
use crate::{eval_with_context, Error, Result, Table, Tool, ToolInfo, Value, VariableMap}; use crate::{
eval_with_context, Error, Result, Table, Tool, ToolInfo, Value, ValueType, VariableMap,
pub struct CreateLine; };
impl Tool for CreateLine {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "create_line",
description: "Create a map value to be shown as a line in a plot.",
group: "gui",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let argument = argument.as_list()?;
let index = argument[0].as_int()?.clone();
let height = argument[1].as_float()?.clone();
let name = argument[2].clone();
let mut line = VariableMap::new();
line.set_value("index", Value::Float(index as f64))?;
line.set_value("height", Value::Float(height))?;
line.set_value("name", name)?;
Ok(Value::Map(line))
}
}
pub struct BarGraph; pub struct BarGraph;
@ -46,7 +21,7 @@ impl Tool for BarGraph {
identifier: "bar_graph", identifier: "bar_graph",
description: "Render a list of values as a bar graph.", description: "Render a list of values as a bar graph.",
group: "gui", group: "gui",
inputs: vec![], inputs: vec![ValueType::ListOf(Box::new(ValueType::List))],
} }
} }
@ -131,7 +106,7 @@ impl Tool for Plot {
identifier: "plot", identifier: "plot",
description: "Render a list of numbers as a scatter plot graph.", description: "Render a list of numbers as a scatter plot graph.",
group: "gui", group: "gui",
inputs: vec![], inputs: vec![ValueType::ListOf(Box::new(ValueType::Integer))],
} }
} }
@ -191,32 +166,6 @@ impl eframe::App for PlotGui {
} }
} }
pub struct Gui;
impl Tool for Gui {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "gui",
description: "Display a value in a GUI window.",
group: "gui",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let argument = argument.clone();
run_native(
&argument.value_type().to_string(),
NativeOptions::default(),
Box::new(|_cc| Box::new(GuiApp::new(Ok(argument)))),
)
.unwrap();
Ok(Value::Empty)
}
}
pub struct GuiApp { pub struct GuiApp {
text_edit_buffer: String, text_edit_buffer: String,
whale_context: VariableMap, whale_context: VariableMap,

View File

@ -145,7 +145,7 @@ impl Tool for Loop {
identifier: "loop", identifier: "loop",
description: "Repeats a function until the program ends.", description: "Repeats a function until the program ends.",
group: "general", group: "general",
inputs: vec![], inputs: vec![ValueType::Function],
} }
} }

View File

@ -43,7 +43,6 @@ pub mod general;
pub mod gui; pub mod gui;
pub mod logic; pub mod logic;
pub mod network; pub mod network;
pub mod package_management;
pub mod random; pub mod random;
pub mod system; pub mod system;
pub mod time; pub mod time;
@ -51,7 +50,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; 55] = [ pub const TOOL_LIST: [&'static dyn Tool; 49] = [
&collections::Count, &collections::Count,
&collections::CreateTable, &collections::CreateTable,
&collections::Insert, &collections::Insert,
@ -81,22 +80,16 @@ pub const TOOL_LIST: [&'static dyn Tool; 55] = [
&filesystem::Trash, &filesystem::Trash,
&filesystem::Watch, &filesystem::Watch,
&filesystem::Write, &filesystem::Write,
&general::Async, &general::Run,
&general::Output, &general::Output,
&general::Repeat, &general::Repeat,
&general::Wait, &general::Wait,
&gui::BarGraph, &gui::BarGraph,
&gui::Plot, &gui::Plot,
&gui::Gui,
&logic::If, &logic::If,
&logic::IfElse, &logic::IfElse,
&logic::Loop, &logic::Loop,
&network::Download, &network::Download,
&package_management::CoprRepositories,
&package_management::EnableRpmRepositories,
&package_management::InstallPackage,
&package_management::UninstallPackage,
&package_management::UpgradePackages,
&random::Random, &random::Random,
&random::RandomBoolean, &random::RandomBoolean,
&random::RandomFloat, &random::RandomFloat,

View File

@ -1,170 +0,0 @@
use std::process::Command;
use crate::{Error, Result, Tool, ToolInfo, Value};
pub struct CoprRepositories;
impl Tool for CoprRepositories {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "enable_copr_repository",
description: "Enable one or more COPR repositories.",
group: "package management",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let repo_list_string = if let Ok(repo) = argument.as_string().cloned() {
repo
} else if let Ok(repos) = argument.as_list() {
repos.iter().map(|value| value.to_string() + " ").collect()
} else {
return Err(crate::Error::ExpectedString {
actual: argument.clone(),
});
};
Command::new("fish")
.arg("-c")
.arg(format!("sudo dnf -y copr enable {repo_list_string}"))
.spawn()?
.wait()?;
Ok(Value::Empty)
}
}
pub struct InstallPackage;
impl Tool for InstallPackage {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "install_package",
description: "Install one or more packages.",
group: "package management",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let package_list_string = if let Ok(package) = argument.as_string().cloned() {
package
} else if let Ok(packages) = argument.as_list() {
packages
.iter()
.map(|value| value.to_string() + " ")
.collect()
} else {
return Err(Error::ExpectedString {
actual: argument.clone(),
});
};
Command::new("fish")
.arg("-c")
.arg(format!("sudo dnf -y install {package_list_string}"))
.spawn()?
.wait()?;
Ok(Value::Empty)
}
}
pub struct EnableRpmRepositories;
impl Tool for EnableRpmRepositories {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "enable_rpm_repositories",
description: "Enable one or more RPM repositories.",
group: "package management",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
if let Ok(repo) = argument.as_string() {
Command::new("fish")
.arg("-c")
.arg(format!("sudo dnf -y config-manager --add-repo {repo}"))
.spawn()?
.wait()?;
} else if let Ok(repos) = argument.as_list() {
for repo in repos {
Command::new("fish")
.arg("-c")
.arg(format!("sudo dnf -y config-manager --add-repo {repo}"))
.spawn()?
.wait()?;
}
} else {
return Err(crate::Error::ExpectedString {
actual: argument.clone(),
});
};
Ok(Value::Empty)
}
}
pub struct UninstallPackage;
impl Tool for UninstallPackage {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "uninstall_package",
description: "Uninstall one or more packages.",
group: "package management",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let package_list_string = if let Ok(package) = argument.as_string().cloned() {
package
} else if let Ok(packages) = argument.as_list() {
packages
.iter()
.map(|value| value.to_string() + " ")
.collect()
} else {
return Err(Error::ExpectedString {
actual: argument.clone(),
});
};
Command::new("fish")
.arg("-c")
.arg(format!("sudo dnf -y remove {package_list_string}"))
.spawn()?
.wait()?;
Ok(Value::Empty)
}
}
pub struct UpgradePackages;
impl Tool for UpgradePackages {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "upgrade_packages",
description: "Upgrade all installed packages.",
group: "package management",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
argument.as_empty()?;
Command::new("fish")
.arg("-c")
.arg("sudo dnf -y upgrade")
.spawn()?
.wait()?;
Ok(Value::Empty)
}
}

View File

@ -1,6 +1,6 @@
use sys_info::cpu_speed; use sys_info::cpu_speed;
use crate::{Result, Tool, ToolInfo, Value}; use crate::{Result, Tool, ToolInfo, Value, ValueType};
pub struct CpuSpeed; pub struct CpuSpeed;
@ -10,7 +10,7 @@ impl Tool for CpuSpeed {
identifier: "cpu_speed", identifier: "cpu_speed",
description: "Return the current processor speed in megahertz.", description: "Return the current processor speed in megahertz.",
group: "system", group: "system",
inputs: vec![], inputs: vec![ValueType::Empty],
} }
} }

View File

@ -1,6 +1,6 @@
use std::time::Instant; use std::time::Instant;
use crate::{Result, Time, Tool, ToolInfo, Value}; use crate::{Result, Time, Tool, ToolInfo, Value, ValueType};
pub struct Now; pub struct Now;
@ -10,7 +10,7 @@ impl Tool for Now {
identifier: "now", identifier: "now",
description: "Return the current time.", description: "Return the current time.",
group: "time", group: "time",
inputs: vec![], inputs: vec![ValueType::Empty],
} }
} }
@ -31,7 +31,7 @@ impl Tool for Local {
identifier: "local", identifier: "local",
description: "Show a time value adjusted for the current time zone.", description: "Show a time value adjusted for the current time zone.",
group: "time", group: "time",
inputs: vec![], inputs: vec![ValueType::Time],
} }
} }