Skip to main content

rustafari_core/
lib.rs

1//! Offline developer utilities, free of any UI dependency.
2//!
3//! Everything here is pure and synchronous: no network, no filesystem, no
4//! globals. The GUI is one consumer; a CLI or a test harness could be another.
5//!
6//! ## Adding a tool
7//!
8//! 1. Write `src/tools/<name>.rs` with a unit struct that implements [`Tool`].
9//! 2. Register it in the `mod tools` list and in [`all_tools`].
10//!
11//! There is no third step — the frontend renders whatever options the tool
12//! declares.
13
14mod spec;
15mod tools;
16
17pub use spec::{
18    Category, Input, InputMode, OptionSpec, OptionValue, Options, Tool, ToolError, ToolMeta,
19    ToolResult,
20};
21
22/// Every tool the app ships, in menu order.
23pub fn all_tools() -> Vec<Box<dyn Tool>> {
24    vec![
25        Box::new(tools::json::JsonFormatter),
26        Box::new(tools::json_diff::JsonDiff),
27        Box::new(tools::yaml_diff::YamlDiff),
28        Box::new(tools::xml_diff::XmlDiff),
29        Box::new(tools::base64::Base64Tool),
30        Box::new(tools::url::UrlTool),
31        Box::new(tools::hash::HashTool),
32        Box::new(tools::uuid::UuidTool),
33        Box::new(tools::cron::Cron),
34        Box::new(tools::list_compare::ListCompare),
35    ]
36}
37
38/// Case-insensitive match against a tool's name, description and keywords.
39pub fn matches_query(meta: &ToolMeta, query: &str) -> bool {
40    let query = query.trim().to_lowercase();
41    if query.is_empty() {
42        return true;
43    }
44
45    query.split_whitespace().all(|term| {
46        meta.name.to_lowercase().contains(term)
47            || meta.description.to_lowercase().contains(term)
48            || meta.keywords.iter().any(|k| k.contains(term))
49    })
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use std::collections::HashSet;
56
57    #[test]
58    fn tool_ids_are_unique() {
59        let tools = all_tools();
60        let ids: HashSet<_> = tools.iter().map(|t| t.meta().id).collect();
61        assert_eq!(ids.len(), tools.len(), "duplicate tool id");
62    }
63
64    #[test]
65    fn every_declared_option_has_a_usable_default() {
66        for tool in all_tools() {
67            let specs = tool.options();
68            let opts = Options::from_specs(specs);
69            for spec in specs.iter().filter(|s| s.is_value()) {
70                assert!(
71                    opts.get(spec.id()).is_some(),
72                    "{} option {} has no default",
73                    tool.meta().id,
74                    spec.id()
75                );
76            }
77            // Empty input under default options is exactly what the user sees
78            // when they open a tool, so it must never be an error.
79            assert!(
80                tool.run(Input::default(), &opts).is_ok(),
81                "{}",
82                tool.meta().id
83            );
84        }
85    }
86
87    #[test]
88    fn choice_defaults_name_a_real_choice() {
89        for tool in all_tools() {
90            for spec in tool.options() {
91                if let OptionSpec::Choice {
92                    id,
93                    choices,
94                    default,
95                    ..
96                } = spec
97                {
98                    assert!(
99                        choices.iter().any(|(value, _)| value == default),
100                        "{}.{id} defaults to {default}, which is not a choice",
101                        tool.meta().id
102                    );
103                }
104            }
105        }
106    }
107
108    #[test]
109    fn search_matches_name_and_keywords_and_requires_all_terms() {
110        let tools = all_tools();
111        let json = tools[0].meta();
112        assert!(matches_query(&json, ""));
113        assert!(matches_query(&json, "JSON"));
114        assert!(matches_query(&json, "beautify"));
115        assert!(matches_query(&json, "json minify"));
116        assert!(!matches_query(&json, "json sha"));
117    }
118}