use std::collections::HashMap;
use async_trait::async_trait;
use crate::plugin::{Plugin, PluginCommand, PluginMetadata};
use crate::filesystem;
type PluginResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub struct FilesystemPlugin {
metadata: PluginMetadata,
}
impl FilesystemPlugin {
pub fn new() -> Self {
Self {
metadata: PluginMetadata {
name: "filesystem".to_string(),
version: "1.0.0".to_string(),
description: "Filesystem 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 FilesystemPlugin {
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("fs_read".to_string(), Box::new(ReadFileCommand) as Box<dyn PluginCommand>);
commands.insert("fs_write".to_string(), Box::new(WriteFileCommand) as Box<dyn PluginCommand>);
commands
}
async fn cleanup(&mut self) -> PluginResult<()> {
Ok(())
}
}
struct ReadFileCommand;
#[async_trait]
impl PluginCommand for ReadFileCommand {
async fn execute(&self, args: &[String]) -> PluginResult<String> {
if args.is_empty() {
return Err("fs_read() requires a path".into());
}
let path = &args[0];
filesystem::read_file(path).await.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
}
fn help(&self) -> &str {
"Reads the content of a file."
}
fn usage(&self) -> &str {
"fs_read <path>"
}
}
struct WriteFileCommand;
#[async_trait]
impl PluginCommand for WriteFileCommand {
async fn execute(&self, args: &[String]) -> PluginResult<String> {
if args.len() < 2 {
return Err("fs_write() requires a path and content".into());
}
let path = &args[0];
let content = &args[1];
filesystem::write_file(path, content).await.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
Ok(String::new())
}
fn help(&self) -> &str {
"Writes content to a file."
}
fn usage(&self) -> &str {
"fs_write <path> <content>"
}
}