use std::collections::HashMap;
use crate::CustomOperator;
use crate::config::EvaluationConfig;
use crate::engine::Engine;
#[must_use = "the builder is consumed by `.build()`"]
pub struct EngineBuilder {
config: EvaluationConfig,
templating: bool,
constant_folding: bool,
operators: HashMap<String, Box<dyn CustomOperator>>,
}
impl Default for EngineBuilder {
fn default() -> Self {
Self::new()
}
}
impl EngineBuilder {
#[inline]
pub fn new() -> Self {
Self {
config: EvaluationConfig::default(),
templating: false,
constant_folding: true,
operators: HashMap::new(),
}
}
#[inline]
#[must_use = "builder methods return a new builder; chain into `.build()`"]
pub fn with_config(mut self, config: EvaluationConfig) -> Self {
self.config = config;
self
}
#[inline]
#[must_use = "builder methods return a new builder; chain into `.build()`"]
pub fn with_templating(mut self, on: bool) -> Self {
self.templating = on;
self
}
#[cfg_attr(feature = "trace", doc = "")]
#[cfg_attr(
feature = "trace",
doc = "The trace surface ([`crate::Engine::trace`]) always disables folding"
)]
#[cfg_attr(
feature = "trace",
doc = "internally regardless of this setting, since traces would otherwise"
)]
#[cfg_attr(feature = "trace", doc = "lose the folded operators as steps.")]
#[inline]
#[must_use = "builder methods return a new builder; chain into `.build()`"]
pub fn with_constant_folding(mut self, on: bool) -> Self {
self.constant_folding = on;
self
}
#[inline]
#[must_use = "builder methods return a new builder; chain into `.build()`"]
pub fn add_operator<T>(mut self, name: impl Into<String>, operator: T) -> Self
where
T: CustomOperator + 'static,
{
self.operators.insert(name.into(), Box::new(operator));
self
}
pub fn build(self) -> Engine {
Engine::from_builder_parts(
self.config,
self.templating,
self.constant_folding,
self.operators,
)
}
}