use std::future::Future;
use std::path::PathBuf;
use std::sync::Arc;
use futures::future::BoxFuture;
use serde_json::Value;
use tokio_util::sync::CancellationToken;
pub use crate::command::PackageSource;
use crate::command::HostLibrary;
pub trait HostFn: Send + Sync {
fn call(
&self,
cx: HostCallContext,
input: Value,
) -> BoxFuture<'static, Result<Value, HostJsError>>;
}
impl<F, Fut> HostFn for F
where
F: Fn(HostCallContext, Value) -> Fut + Send + Sync,
Fut: Future<Output = Result<Value, HostJsError>> + Send + 'static,
{
fn call(
&self,
cx: HostCallContext,
input: Value,
) -> BoxFuture<'static, Result<Value, HostJsError>> {
Box::pin(self(cx, input))
}
}
#[derive(Debug, Clone)]
pub struct HostCallContext {
pub plugin: String,
pub plugin_version: String,
pub source: PackageSource,
pub entry: String,
pub session_id: Option<String>,
pub project_root: Option<PathBuf>,
pub cwd: PathBuf,
pub via: String,
pub agent_depth: u32,
pub cancel: CancellationToken,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{code}: {message}")]
pub struct HostJsError {
pub code: String,
pub message: String,
}
impl HostJsError {
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
}
}
}
#[derive(Clone)]
pub struct HostJsLib {
namespace: String,
functions: Vec<(String, Arc<dyn HostFn>)>,
prelude: Option<String>,
description: Option<String>,
}
impl std::fmt::Debug for HostJsLib {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HostJsLib")
.field("namespace", &self.namespace)
.field("functions", &self.paths().collect::<Vec<_>>())
.field("prelude", &self.prelude.as_ref().map(String::len))
.field("description", &self.description)
.finish()
}
}
impl HostJsLib {
pub fn new(namespace: &str) -> Self {
Self {
namespace: namespace.to_string(),
functions: Vec::new(),
prelude: None,
description: None,
}
}
pub fn function<F, Fut>(mut self, path: &str, f: F) -> Self
where
F: Fn(HostCallContext, Value) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Value, HostJsError>> + Send + 'static,
{
self.functions.push((path.to_string(), Arc::new(f)));
self
}
pub fn prelude(mut self, js: impl Into<String>) -> Self {
self.prelude = Some(js.into());
self
}
pub fn description(mut self, text: impl Into<String>) -> Self {
self.description = Some(text.into());
self
}
pub fn namespace(&self) -> &str {
&self.namespace
}
pub fn paths(&self) -> impl Iterator<Item = &str> {
self.functions.iter().map(|(path, _)| path.as_str())
}
pub fn function_at(&self, path: &str) -> Option<&Arc<dyn HostFn>> {
self.functions
.iter()
.find(|(p, _)| p == path)
.map(|(_, f)| f)
}
pub fn prelude_source(&self) -> Option<&str> {
self.prelude.as_deref()
}
pub fn library(&self) -> HostLibrary {
HostLibrary {
name: self.namespace.clone(),
description: self.description.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn a_closure_is_a_host_function() {
let lib = HostJsLib::new("codix")
.function("echo", |cx: HostCallContext, input| async move {
Ok(serde_json::json!({ "via": cx.via, "input": input }))
})
.description("Codix flows");
let cx = HostCallContext {
plugin: "p".into(),
plugin_version: "1".into(),
source: PackageSource::Installed,
entry: "cmd".into(),
session_id: None,
project_root: None,
cwd: PathBuf::from("/"),
via: "user".into(),
agent_depth: 0,
cancel: CancellationToken::new(),
};
let out = lib.function_at("echo").unwrap().call(cx, 1.into()).await;
assert_eq!(out, Ok(serde_json::json!({ "via": "user", "input": 1 })));
assert_eq!(
lib.library(),
HostLibrary {
name: "codix".into(),
description: Some("Codix flows".into())
}
);
}
}