use std::collections::HashMap;
use std::env;
use oci_spec::runtime::Spec;
pub mod default;
pub static EMPTY: Vec<String> = Vec::new();
#[derive(Debug, thiserror::Error)]
pub enum ExecutorError {
#[error("invalid argument")]
InvalidArg,
#[error("failed to execute workload")]
Execution(#[from] Box<dyn std::error::Error + Send + Sync>),
#[error("{0}")]
Other(String),
#[error("{0} executor can't handle spec")]
CantHandle(&'static str),
}
#[derive(Debug, thiserror::Error)]
pub enum ExecutorValidationError {
#[error("{0} executor can't handle spec")]
CantHandle(&'static str),
#[error("{0}")]
ArgValidationError(String),
}
#[derive(Debug, thiserror::Error)]
pub enum ExecutorSetEnvsError {
#[error("failed to set envs")]
SetEnvs(#[from] Box<dyn std::error::Error + Send + Sync>),
#[error("{0}")]
Other(String),
}
pub trait CloneBoxExecutor {
fn clone_box(&self) -> Box<dyn Executor>;
}
pub trait Executor: CloneBoxExecutor {
fn exec(&self, spec: &Spec) -> Result<(), ExecutorError>;
fn validate(&self, spec: &Spec) -> Result<(), ExecutorValidationError>;
fn setup_envs(&self, envs: HashMap<String, String>) -> Result<(), ExecutorSetEnvsError> {
env::vars().for_each(|(key, _value)| unsafe { env::remove_var(key) });
envs.iter()
.for_each(|(key, value)| unsafe { env::set_var(key, value) });
Ok(())
}
}
impl<T> CloneBoxExecutor for T
where
T: 'static + Executor + Clone,
{
fn clone_box(&self) -> Box<dyn Executor> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Executor> {
fn clone(&self) -> Self {
self.clone_box()
}
}