#[cfg(feature = "python")]
pub(crate) mod python;
use std::path::Path;
use futures::future::BoxFuture;
use serde_json::Value;
use crate::ToolError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Language {
#[cfg(feature = "python")]
Python,
#[cfg(feature = "lua")]
Lua,
#[cfg(feature = "js")]
JavaScript,
}
impl Language {
pub fn name(self) -> &'static str {
match self {
#[cfg(feature = "python")]
Self::Python => "Python",
#[cfg(feature = "lua")]
Self::Lua => "Lua",
#[cfg(feature = "js")]
Self::JavaScript => "JavaScript",
}
}
}
pub struct RawToolDef {
pub name: String,
pub description: String,
pub parameters: Value,
pub meta: Value,
pub func: Box<
dyn Fn(Value) -> BoxFuture<'static, Result<Value, ToolError>> + Send + Sync,
>,
}
pub(crate) fn load_language(
lang: Language,
#[cfg_attr(
not(any(feature = "python", feature = "lua", feature = "js")),
allow(unused_variables)
)]
path: &Path,
) -> Result<Vec<RawToolDef>, ToolError> {
match lang {
#[cfg(feature = "python")]
Language::Python => python::load(path),
#[cfg(feature = "lua")]
Language::Lua => Err(ToolError::Runtime(format!(
"Lua language support not yet implemented (path: {})",
path.display(),
))),
#[cfg(feature = "js")]
Language::JavaScript => Err(ToolError::Runtime(format!(
"JavaScript language support not yet implemented (path: {})",
path.display(),
))),
}
}
pub(crate) fn leak_string(s: String) -> &'static str {
Box::leak(s.into_boxed_str())
}