#![deny(missing_docs)]
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
mod error;
mod policy;
#[cfg(feature = "docker")]
mod docker;
#[cfg(feature = "unsafe-host")]
mod host;
pub use error::{Result, SandboxError};
pub use policy::{NetworkPolicy, SandboxPolicy, SandboxRuntime};
#[cfg(feature = "docker")]
pub use docker::DockerSandbox;
#[cfg(feature = "unsafe-host")]
pub use host::HostSandbox;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Mount {
pub source: PathBuf,
pub target: PathBuf,
pub read_only: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecSpec {
pub cmd: Vec<String>,
pub env: BTreeMap<String, String>,
pub workdir: PathBuf,
pub stdin: Option<Vec<u8>>,
pub mounts: Vec<Mount>,
pub timeout: Duration,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ExecHandle(Uuid);
impl ExecHandle {
pub fn new() -> Self {
Self(Uuid::new_v4())
}
pub fn as_uuid(&self) -> Uuid {
self.0
}
}
impl Default for ExecHandle {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecOutput {
pub exit_code: i32,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub wall_time: Duration,
}
#[async_trait]
pub trait Sandbox: Send + Sync {
async fn spawn(&self, spec: ExecSpec) -> Result<ExecHandle>;
async fn wait(&self, handle: ExecHandle) -> Result<ExecOutput>;
async fn shutdown(&self) -> Result<()>;
fn runtime(&self) -> SandboxRuntime;
}