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    ]
35}
36
37/// Case-insensitive match against a tool's name, description and keywords.
38pub fn matches_query(meta: &ToolMeta, query: &str) -> bool {
39    let query = query.trim().to_lowercase();
40    if query.is_empty() {
41        return true;
42    }
43
44    query.split_whitespace().all(|term| {
45        meta.name.to_lowercase().contains(term)
46            || meta.description.to_lowercase().contains(term)
47            || meta.keywords.iter().any(|k| k.contains(term))
48    })
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use std::collections::HashSet;
55
56    #[test]
57    fn tool_ids_are_unique() {
58        let tools = all_tools();
59        let ids: HashSet<_> = tools.iter().map(|t| t.meta().id).collect();
60        assert_eq!(ids.len(), tools.len(), "duplicate tool id");
61    }
62
63    #[test]
64    fn every_declared_option_has_a_usable_default() {
65        for tool in all_tools() {
66            let specs = tool.options();
67            let opts = Options::from_specs(specs);
68            for spec in specs {
69                assert!(
70                    opts.get(spec.id()).is_some(),
71                    "{} option {} has no default",
72                    tool.meta().id,
73                    spec.id()
74                );
75            }
76            // Empty input under default options is exactly what the user sees
77            // when they open a tool, so it must never be an error.
78            assert!(
79                tool.run(Input::default(), &opts).is_ok(),
80                "{}",
81                tool.meta().id
82            );
83        }
84    }
85
86    #[test]
87    fn choice_defaults_name_a_real_choice() {
88        for tool in all_tools() {
89            for spec in tool.options() {
90                if let OptionSpec::Choice {
91                    id,
92                    choices,
93                    default,
94                    ..
95                } = spec
96                {
97                    assert!(
98                        choices.iter().any(|(value, _)| value == default),
99                        "{}.{id} defaults to {default}, which is not a choice",
100                        tool.meta().id
101                    );
102                }
103            }
104        }
105    }
106
107    #[test]
108    fn search_matches_name_and_keywords_and_requires_all_terms() {
109        let tools = all_tools();
110        let json = tools[0].meta();
111        assert!(matches_query(&json, ""));
112        assert!(matches_query(&json, "JSON"));
113        assert!(matches_query(&json, "beautify"));
114        assert!(matches_query(&json, "json minify"));
115        assert!(!matches_query(&json, "json sha"));
116    }
117}