use crate::error::Result;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Capability {
ReadProject,
WriteProject,
ReadHome,
WriteHome,
Network,
Subprocess,
}
pub trait Sandbox {
fn declare(&mut self, capabilities: &[Capability]);
fn enter(&self) -> Result<()>;
}
#[derive(Debug, Default, Clone)]
pub struct NoopSandbox {
declared: Vec<Capability>,
}
impl NoopSandbox {
#[must_use]
pub fn declared(&self) -> &[Capability] {
&self.declared
}
}
impl Sandbox for NoopSandbox {
fn declare(&mut self, capabilities: &[Capability]) {
self.declared.extend_from_slice(capabilities);
}
fn enter(&self) -> Result<()> {
Ok(())
}
}
#[must_use]
pub fn for_current_platform() -> Box<dyn Sandbox> {
Box::new(NoopSandbox::default())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn noop_sandbox_records_declarations() {
let mut sb = NoopSandbox::default();
sb.declare(&[Capability::ReadProject, Capability::Subprocess]);
sb.enter().unwrap();
assert_eq!(
sb.declared(),
&[Capability::ReadProject, Capability::Subprocess]
);
}
}