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