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