use crate::compiler::Program;
use crate::library::{FunctionLibrary, HostFn};
use crate::runtime::provider::{LineProvider, PassthroughProvider};
use crate::saliency::{FirstAvailable, SaliencyStrategy};
use crate::value::VariableStorage;
use super::Runner;
pub struct RunnerBuilder<S: VariableStorage> {
program: Program,
storage: S,
saliency: Box<dyn SaliencyStrategy>,
provider: Box<dyn LineProvider>,
library: FunctionLibrary,
}
impl<S: VariableStorage> RunnerBuilder<S> {
pub fn new(program: Program, storage: S) -> Self {
Self {
program,
storage,
saliency: Box::new(FirstAvailable),
provider: Box::new(PassthroughProvider),
library: FunctionLibrary::new(),
}
}
#[must_use]
pub fn with_saliency(mut self, strategy: impl SaliencyStrategy) -> Self {
self.saliency = Box::new(strategy);
self
}
#[must_use]
pub fn with_provider(mut self, provider: impl LineProvider) -> Self {
self.provider = Box::new(provider);
self
}
#[must_use]
pub fn with_function<F>(mut self, name: &str, f: F) -> Self
where
F: Fn(Vec<crate::value::Value>) -> crate::error::Result<crate::value::Value>
+ Send
+ Sync
+ 'static,
{
self.library.register(name, Box::new(f) as HostFn);
self
}
#[must_use]
pub fn build(self) -> Runner<S> {
let mut runner = Runner::new(self.program, self.storage);
runner.set_saliency_box(self.saliency);
runner.set_provider_box(self.provider);
*runner.library_mut() = self.library;
runner
}
}