use super::Context;
use crate::{console, ContextError};
pub struct ContextBuilder {
memory_limit: Option<usize>,
console_backend: Option<Box<dyn console::ConsoleBackend>>,
}
impl ContextBuilder {
pub fn new() -> Self {
Self {
memory_limit: None,
console_backend: None,
}
}
pub fn memory_limit(self, max_bytes: usize) -> Self {
let mut s = self;
s.memory_limit = Some(max_bytes);
s
}
pub fn console<B>(mut self, backend: B) -> Self
where
B: console::ConsoleBackend,
{
self.console_backend = Some(Box::new(backend));
self
}
pub fn build(self) -> Result<Context, ContextError> {
let context = Context::new(self.memory_limit)?;
if let Some(be) = self.console_backend {
context.set_console(be).map_err(ContextError::Execution)?;
}
Ok(context)
}
}