pub mod native;
#[cfg(feature = "native-executors")]
pub mod thread_pool;
#[cfg(feature = "wasmtime-executor")]
pub mod wasm;
pub use native::NativeExecutor;
#[cfg(feature = "native-executors")]
pub use thread_pool::{ConfigError, ThreadPoolExecutor, ThreadPoolExecutorConfig};
#[cfg(feature = "wasmtime-executor")]
pub use wasm::{
HostAbiImport, HostAbiValidation, SUPPORTED_HOST_ABI_VERSION, WasmExecutionLimits,
WasmExecutor, WasmModuleCacheConfig, WasmModuleCacheStats, supported_host_abi_versions,
verify_wasm_host_abi_bytes,
};
use crate::events::types::TraverseEvent;
use serde_json::Value;
use traverse_contracts::{EventReference, ServiceType};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ArtifactType {
Native,
Wasm,
}
#[derive(Debug, Clone)]
pub struct ExecutorCapability {
pub capability_id: String,
pub artifact_type: ArtifactType,
pub wasm_binary_path: Option<String>,
pub wasm_checksum: Option<String>,
pub host_abi_version: Option<String>,
pub emits: Vec<EventReference>,
pub service_type: ServiceType,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExecutorOutput {
pub value: Value,
pub emitted_events: Vec<TraverseEvent>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExecutorError {
BinaryLoadFailed(String),
ChecksumMismatch { expected: String, actual: String },
RuntimeSetupFailed(String),
MalformedWasmArtifact { error_code: String, detail: String },
UnsupportedAbiVersion {
error_code: String,
requested: String,
supported: String,
},
UnauthorizedHostImport {
error_code: String,
abi_version: String,
module: String,
name: String,
},
ExecutionFailed(String),
Timeout(String),
ResourceExhausted(String),
OutputDeserializationFailed(String),
UnsupportedArtifactType,
}
impl std::fmt::Display for ExecutorError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BinaryLoadFailed(msg) => write!(f, "binary load failed: {msg}"),
Self::ChecksumMismatch { expected, actual } => {
write!(f, "checksum mismatch: expected {expected}, got {actual}")
}
Self::RuntimeSetupFailed(msg) => write!(f, "runtime setup failed: {msg}"),
Self::MalformedWasmArtifact { error_code, detail } => {
write!(f, "{error_code}: {detail}")
}
Self::UnsupportedAbiVersion {
error_code,
requested,
supported,
} => write!(
f,
"{error_code}: requested Traverse Host ABI {requested}, supported {supported}"
),
Self::UnauthorizedHostImport {
error_code,
abi_version,
module,
name,
} => write!(
f,
"{error_code}: ABI {abi_version} does not allow import {module}::{name}"
),
Self::ExecutionFailed(msg) => write!(f, "execution failed: {msg}"),
Self::Timeout(msg) => write!(f, "execution timed out: {msg}"),
Self::ResourceExhausted(msg) => write!(f, "resource exhausted: {msg}"),
Self::OutputDeserializationFailed(msg) => {
write!(f, "output deserialization failed: {msg}")
}
Self::UnsupportedArtifactType => {
write!(f, "unsupported artifact type for this executor")
}
}
}
}
impl std::error::Error for ExecutorError {}
pub trait CapabilityExecutor: Send + Sync {
fn execute(
&self,
capability: &ExecutorCapability,
input: &Value,
) -> Result<ExecutorOutput, ExecutorError>;
}