use std::collections::HashMap;
use async_trait::async_trait;
use crate::plugin::{Plugin, PluginCommand, PluginMetadata};
use crate::utils;
type PluginResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub struct UtilsPlugin {
metadata: PluginMetadata,
}
impl UtilsPlugin {
pub fn new() -> Self {
Self {
metadata: PluginMetadata {
name: "utils".to_string(),
version: "1.0.0".to_string(),
description: "Utils plugin for OpenScript".to_string(),
author: "OpenScript Team".to_string(),
license: "MIT".to_string(),
dependencies: vec![],
api_version: "1.0".to_string(),
},
}
}
}
#[async_trait]
impl Plugin for UtilsPlugin {
fn metadata(&self) -> &PluginMetadata {
&self.metadata
}
async fn initialize(&mut self) -> PluginResult<()> {
Ok(())
}
fn register_commands(&self) -> HashMap<String, Box<dyn PluginCommand>> {
let mut commands = HashMap::new();
commands.insert("util_split".to_string(), Box::new(SplitCommand) as Box<dyn PluginCommand>);
commands
}
async fn cleanup(&mut self) -> PluginResult<()> {
Ok(())
}
}
struct SplitCommand;
#[async_trait]
impl PluginCommand for SplitCommand {
async fn execute(&self, args: &[String]) -> PluginResult<String> {
if args.len() < 2 {
return Err("util_split() requires a string and a separator".into());
}
let s = &args[0];
let sep = &args[1];
let result = utils::split(s, sep).map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
Ok(serde_json::to_string(&result).unwrap())
}
fn help(&self) -> &str {
"Splits a string by a separator."
}
fn usage(&self) -> &str {
"util_split <string> <separator>"
}
}