1use std::collections::HashMap;
8use std::fmt;
9
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
11pub enum Category {
12 Formatters,
13 Encoders,
14 Generators,
15 Text,
16}
17
18impl Category {
19 pub const ALL: &'static [Category] = &[
20 Category::Formatters,
21 Category::Encoders,
22 Category::Generators,
23 Category::Text,
24 ];
25
26 pub fn label(self) -> &'static str {
27 match self {
28 Category::Formatters => "Formatters",
29 Category::Encoders => "Encoders & Decoders",
30 Category::Generators => "Generators",
31 Category::Text => "Text",
32 }
33 }
34}
35
36#[derive(Clone, Copy, Debug)]
38pub struct ToolMeta {
39 pub id: &'static str,
41 pub name: &'static str,
42 pub category: Category,
43 pub description: &'static str,
44 pub keywords: &'static [&'static str],
46}
47
48#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
55pub enum OptionPane {
56 #[default]
57 Input,
58 Output,
59}
60
61#[derive(Clone, Copy, PartialEq, Eq, Debug)]
63pub enum InputMode {
64 Text { placeholder: &'static str },
66 TwoText {
69 left_label: &'static str,
70 right_label: &'static str,
71 placeholder: &'static str,
72 },
73 None,
75}
76
77#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
82pub struct Input<'a> {
83 pub left: &'a str,
84 pub right: &'a str,
85}
86
87impl<'a> Input<'a> {
88 pub fn pair(left: &'a str, right: &'a str) -> Self {
89 Input { left, right }
90 }
91}
92
93impl<'a> From<&'a str> for Input<'a> {
94 fn from(left: &'a str) -> Self {
95 Input { left, right: "" }
96 }
97}
98
99#[derive(Clone, Copy, Debug)]
100pub enum OptionSpec {
101 Toggle {
102 id: &'static str,
103 label: &'static str,
104 default: bool,
105 },
106 Choice {
108 id: &'static str,
109 label: &'static str,
110 choices: &'static [(&'static str, &'static str)],
111 default: &'static str,
112 },
113 Number {
114 id: &'static str,
115 label: &'static str,
116 min: i64,
117 max: i64,
118 default: i64,
119 },
120 Group {
126 label: &'static str,
127 pane: OptionPane,
128 },
129 Text {
133 id: &'static str,
134 label: &'static str,
135 placeholder: &'static str,
136 default: &'static str,
137 },
138}
139
140impl OptionSpec {
141 pub fn id(&self) -> &'static str {
142 match self {
143 OptionSpec::Toggle { id, .. }
144 | OptionSpec::Choice { id, .. }
145 | OptionSpec::Number { id, .. }
146 | OptionSpec::Text { id, .. } => id,
147 OptionSpec::Group { .. } => "",
149 }
150 }
151
152 pub fn label(&self) -> &'static str {
153 match self {
154 OptionSpec::Toggle { label, .. }
155 | OptionSpec::Choice { label, .. }
156 | OptionSpec::Number { label, .. }
157 | OptionSpec::Text { label, .. }
158 | OptionSpec::Group { label, .. } => label,
159 }
160 }
161
162 pub fn is_value(&self) -> bool {
164 !matches!(self, OptionSpec::Group { .. })
165 }
166
167 fn default_value(&self) -> OptionValue {
168 match self {
169 OptionSpec::Toggle { default, .. } => OptionValue::Bool(*default),
170 OptionSpec::Choice { default, .. } => OptionValue::Choice((*default).to_string()),
171 OptionSpec::Number { default, .. } => OptionValue::Number(*default),
172 OptionSpec::Text { default, .. } => OptionValue::Text((*default).to_string()),
173 OptionSpec::Group { .. } => OptionValue::Bool(false),
174 }
175 }
176}
177
178#[derive(Clone, PartialEq, Debug)]
179pub enum OptionValue {
180 Bool(bool),
181 Choice(String),
182 Number(i64),
183 Text(String),
184}
185
186#[derive(Clone, PartialEq, Debug, Default)]
191pub struct Options {
192 values: HashMap<&'static str, OptionValue>,
193}
194
195impl Options {
196 pub fn from_specs(specs: &[OptionSpec]) -> Self {
197 Options {
198 values: specs
199 .iter()
200 .filter(|s| s.is_value())
201 .map(|s| (s.id(), s.default_value()))
202 .collect(),
203 }
204 }
205
206 pub fn get(&self, id: &str) -> Option<&OptionValue> {
207 self.values.get(id)
208 }
209
210 pub fn set(&mut self, id: &'static str, value: OptionValue) {
211 self.values.insert(id, value);
212 }
213
214 pub fn bool(&self, id: &str) -> bool {
215 matches!(self.values.get(id), Some(OptionValue::Bool(true)))
216 }
217
218 pub fn choice(&self, id: &str) -> &str {
219 match self.values.get(id) {
220 Some(OptionValue::Choice(v)) => v,
221 _ => "",
222 }
223 }
224
225 pub fn number(&self, id: &str) -> i64 {
226 match self.values.get(id) {
227 Some(OptionValue::Number(v)) => *v,
228 _ => 0,
229 }
230 }
231
232 pub fn text(&self, id: &str) -> &str {
233 match self.values.get(id) {
234 Some(OptionValue::Text(v)) => v,
235 _ => "",
236 }
237 }
238}
239
240#[derive(Clone, PartialEq, Debug)]
243pub struct ToolError(pub String);
244
245impl ToolError {
246 pub fn new(msg: impl Into<String>) -> Self {
247 ToolError(msg.into())
248 }
249}
250
251impl fmt::Display for ToolError {
252 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253 f.write_str(&self.0)
254 }
255}
256
257impl std::error::Error for ToolError {}
258
259pub type ToolResult = Result<String, ToolError>;
260
261#[derive(Clone, Copy, PartialEq, Eq, Debug)]
267pub enum Format {
268 Any,
271 Plain,
274 Json,
275 Yaml,
276 Xml,
277 Base64,
278}
279
280impl Format {
281 pub fn flows_into(self, accepted: &[Format]) -> bool {
288 self == Format::Any || accepted.contains(&Format::Any) || accepted.contains(&self)
289 }
290}
291
292pub trait Tool: Send + Sync {
293 fn meta(&self) -> ToolMeta;
294
295 fn input_mode(&self) -> InputMode {
296 InputMode::Text {
297 placeholder: "Paste your input here",
298 }
299 }
300
301 fn options(&self) -> &'static [OptionSpec] {
302 &[]
303 }
304
305 fn accepts(&self) -> &'static [Format] {
307 &[Format::Any]
308 }
309
310 fn produces(&self, _opts: &Options) -> Format {
314 Format::Any
315 }
316
317 fn run(&self, input: Input<'_>, opts: &Options) -> ToolResult;
318}