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