mod spec;
mod tools;
pub use spec::{
Category, Format, Input, InputMode, OptionPane, OptionSpec, OptionValue, Options, Tool,
ToolError, ToolMeta, ToolResult,
};
pub fn all_tools() -> Vec<Box<dyn Tool>> {
vec![
Box::new(tools::json::JsonFormatter),
Box::new(tools::json_diff::JsonDiff),
Box::new(tools::yaml_diff::YamlDiff),
Box::new(tools::xml_diff::XmlDiff),
Box::new(tools::base64::Base64Tool),
Box::new(tools::url::UrlTool),
Box::new(tools::hash::HashTool),
Box::new(tools::uuid::UuidTool),
Box::new(tools::cron::Cron),
Box::new(tools::list_compare::ListCompare),
]
}
pub fn matches_query(meta: &ToolMeta, query: &str) -> bool {
let query = query.trim().to_lowercase();
if query.is_empty() {
return true;
}
query.split_whitespace().all(|term| {
meta.name.to_lowercase().contains(term)
|| meta.description.to_lowercase().contains(term)
|| meta.keywords.iter().any(|k| k.contains(term))
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
#[test]
fn tool_ids_are_unique() {
let tools = all_tools();
let ids: HashSet<_> = tools.iter().map(|t| t.meta().id).collect();
assert_eq!(ids.len(), tools.len(), "duplicate tool id");
}
#[test]
fn every_declared_option_has_a_usable_default() {
for tool in all_tools() {
let specs = tool.options();
let opts = Options::from_specs(specs);
for spec in specs.iter().filter(|s| s.is_value()) {
assert!(
opts.get(spec.id()).is_some(),
"{} option {} has no default",
tool.meta().id,
spec.id()
);
}
assert!(
tool.run(Input::default(), &opts).is_ok(),
"{}",
tool.meta().id
);
}
}
#[test]
fn choice_defaults_name_a_real_choice() {
for tool in all_tools() {
for spec in tool.options() {
if let OptionSpec::Choice {
id,
choices,
default,
..
} = spec
{
assert!(
choices.iter().any(|(value, _)| value == default),
"{}.{id} defaults to {default}, which is not a choice",
tool.meta().id
);
}
}
}
}
#[test]
fn chaining_refuses_impossible_links_and_keeps_possible_ones() {
let tools = all_tools();
let by_id = |id: &str| {
tools
.iter()
.find(|t| t.meta().id == id)
.expect("tool exists")
};
let json = by_id("json-formatter");
let list = by_id("list-compare");
let base64 = by_id("base64");
let list_opts = Options::from_specs(list.options());
assert!(!list.produces(&list_opts).flows_into(json.accepts()));
let mut as_json = Options::from_specs(list.options());
as_json.set("format", OptionValue::Choice("json".into()));
assert!(list.produces(&as_json).flows_into(json.accepts()));
let mut decode = Options::from_specs(base64.options());
decode.set("direction", OptionValue::Choice("decode".into()));
assert!(base64.produces(&decode).flows_into(json.accepts()));
let encode = Options::from_specs(base64.options());
assert!(!base64.produces(&encode).flows_into(json.accepts()));
for tool in &tools {
if tool.input_mode() != InputMode::None {
assert!(!tool.accepts().is_empty(), "{}", tool.meta().id);
}
}
}
#[test]
fn search_matches_name_and_keywords_and_requires_all_terms() {
let tools = all_tools();
let json = tools[0].meta();
assert!(matches_query(&json, ""));
assert!(matches_query(&json, "JSON"));
assert!(matches_query(&json, "beautify"));
assert!(matches_query(&json, "json minify"));
assert!(!matches_query(&json, "json sha"));
}
}