use std::collections::HashMap;
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Category {
Formatters,
Encoders,
Generators,
Text,
}
impl Category {
pub const ALL: &'static [Category] = &[
Category::Formatters,
Category::Encoders,
Category::Generators,
Category::Text,
];
pub fn label(self) -> &'static str {
match self {
Category::Formatters => "Formatters",
Category::Encoders => "Encoders & Decoders",
Category::Generators => "Generators",
Category::Text => "Text",
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct ToolMeta {
pub id: &'static str,
pub name: &'static str,
pub category: Category,
pub description: &'static str,
pub keywords: &'static [&'static str],
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum OptionPane {
#[default]
Input,
Output,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum InputMode {
Text { placeholder: &'static str },
TwoText {
left_label: &'static str,
right_label: &'static str,
placeholder: &'static str,
},
None,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Input<'a> {
pub left: &'a str,
pub right: &'a str,
}
impl<'a> Input<'a> {
pub fn pair(left: &'a str, right: &'a str) -> Self {
Input { left, right }
}
}
impl<'a> From<&'a str> for Input<'a> {
fn from(left: &'a str) -> Self {
Input { left, right: "" }
}
}
#[derive(Clone, Copy, Debug)]
pub enum OptionSpec {
Toggle {
id: &'static str,
label: &'static str,
default: bool,
},
Choice {
id: &'static str,
label: &'static str,
choices: &'static [(&'static str, &'static str)],
default: &'static str,
},
Number {
id: &'static str,
label: &'static str,
min: i64,
max: i64,
default: i64,
},
Group {
label: &'static str,
pane: OptionPane,
},
Text {
id: &'static str,
label: &'static str,
placeholder: &'static str,
default: &'static str,
},
}
impl OptionSpec {
pub fn id(&self) -> &'static str {
match self {
OptionSpec::Toggle { id, .. }
| OptionSpec::Choice { id, .. }
| OptionSpec::Number { id, .. }
| OptionSpec::Text { id, .. } => id,
OptionSpec::Group { .. } => "",
}
}
pub fn label(&self) -> &'static str {
match self {
OptionSpec::Toggle { label, .. }
| OptionSpec::Choice { label, .. }
| OptionSpec::Number { label, .. }
| OptionSpec::Text { label, .. }
| OptionSpec::Group { label, .. } => label,
}
}
pub fn is_value(&self) -> bool {
!matches!(self, OptionSpec::Group { .. })
}
fn default_value(&self) -> OptionValue {
match self {
OptionSpec::Toggle { default, .. } => OptionValue::Bool(*default),
OptionSpec::Choice { default, .. } => OptionValue::Choice((*default).to_string()),
OptionSpec::Number { default, .. } => OptionValue::Number(*default),
OptionSpec::Text { default, .. } => OptionValue::Text((*default).to_string()),
OptionSpec::Group { .. } => OptionValue::Bool(false),
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum OptionValue {
Bool(bool),
Choice(String),
Number(i64),
Text(String),
}
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Options {
values: HashMap<&'static str, OptionValue>,
}
impl Options {
pub fn from_specs(specs: &[OptionSpec]) -> Self {
Options {
values: specs
.iter()
.filter(|s| s.is_value())
.map(|s| (s.id(), s.default_value()))
.collect(),
}
}
pub fn get(&self, id: &str) -> Option<&OptionValue> {
self.values.get(id)
}
pub fn set(&mut self, id: &'static str, value: OptionValue) {
self.values.insert(id, value);
}
pub fn bool(&self, id: &str) -> bool {
matches!(self.values.get(id), Some(OptionValue::Bool(true)))
}
pub fn choice(&self, id: &str) -> &str {
match self.values.get(id) {
Some(OptionValue::Choice(v)) => v,
_ => "",
}
}
pub fn number(&self, id: &str) -> i64 {
match self.values.get(id) {
Some(OptionValue::Number(v)) => *v,
_ => 0,
}
}
pub fn text(&self, id: &str) -> &str {
match self.values.get(id) {
Some(OptionValue::Text(v)) => v,
_ => "",
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct ToolError(pub String);
impl ToolError {
pub fn new(msg: impl Into<String>) -> Self {
ToolError(msg.into())
}
}
impl fmt::Display for ToolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for ToolError {}
pub type ToolResult = Result<String, ToolError>;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Format {
Any,
Plain,
Json,
Yaml,
Xml,
Base64,
}
impl Format {
pub fn flows_into(self, accepted: &[Format]) -> bool {
self == Format::Any || accepted.contains(&Format::Any) || accepted.contains(&self)
}
}
pub trait Tool: Send + Sync {
fn meta(&self) -> ToolMeta;
fn input_mode(&self) -> InputMode {
InputMode::Text {
placeholder: "Paste your input here",
}
}
fn options(&self) -> &'static [OptionSpec] {
&[]
}
fn accepts(&self) -> &'static [Format] {
&[Format::Any]
}
fn produces(&self, _opts: &Options) -> Format {
Format::Any
}
fn run(&self, input: Input<'_>, opts: &Options) -> ToolResult;
}