sandy/src/executor.rs

41 lines
1.0 KiB
Rust

use crate::executor::{javascript::NodeJSExecutor, php::PhpExecutor, python::PythonExecutor};
pub mod javascript;
pub mod php;
pub mod python;
pub mod helper;
pub fn executors() -> Vec<Box<dyn Executor>> {
vec![
Box::new(NodeJSExecutor {}),
Box::new(PhpExecutor {}),
Box::new(PythonExecutor {}),
]
}
pub fn executors_id() -> Vec<String> {
filter_unavailable(&executors())
.iter()
.map(|x| x.id())
.collect()
}
pub fn get_executor<'a, T: Into<String>>(id: T, executors: &'a Vec<Box<dyn Executor>>) -> Option<&'a Box<dyn Executor>> {
let id: String = id.into();
let found = executors.iter().find(|x| x.id() == id);
found
}
pub fn filter_unavailable<'a>(executors: &'a Vec<Box<dyn Executor>>) -> Vec<&Box<dyn Executor>> {
executors
.iter()
.filter(|x| x.available())
.collect()
}
pub trait Executor {
fn id(&self) -> String;
fn exec(&self, code: String) -> Result<String, String>;
fn available(&self) -> bool;
}