use async_trait::async_trait;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::env::{Limits, SessionEnv};
use crate::error::RuntimeResult;
use crate::local_env::LocalSessionEnv;
#[async_trait]
pub trait Sandbox: Send + Sync {
fn name(&self) -> &str;
async fn env_for(&self, workdir: &Path) -> RuntimeResult<Arc<dyn SessionEnv>>;
}
pub struct LocalSandbox {
root: PathBuf,
limits: Limits,
}
impl LocalSandbox {
#[must_use]
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {
root: root.into(),
limits: Limits::default(),
}
}
#[must_use]
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
}
#[async_trait]
impl Sandbox for LocalSandbox {
fn name(&self) -> &str {
"local"
}
async fn env_for(&self, _workdir: &Path) -> RuntimeResult<Arc<dyn SessionEnv>> {
Ok(Arc::new(
LocalSessionEnv::new(&self.root, self.limits).await?,
))
}
}
#[must_use]
pub fn local() -> LocalSandbox {
LocalSandbox::new(std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")))
}