use std::collections::HashMap;
use crate::qdrant::*;
#[derive(Clone)]
pub struct FormulaBuilder {
pub(crate) expression: Expression,
pub(crate) defaults: HashMap<String, Value>,
}
impl FormulaBuilder {
pub fn new<E: Into<Expression>>(expression: E) -> Self {
Self {
expression: expression.into(),
defaults: Default::default(),
}
}
pub fn expression<E: Into<Expression>>(self, value: E) -> Self {
let mut new = self;
new.expression = value.into();
new
}
pub fn defaults(mut self, defaults: HashMap<String, Value>) -> Self {
self.defaults = defaults;
self
}
pub fn add_default<K: Into<String>, V: Into<Value>>(self, key: K, value: V) -> Self {
let mut new = self;
new.defaults.insert(key.into(), value.into());
new
}
fn build_inner(self) -> Result<Formula, std::convert::Infallible> {
Ok(Formula {
expression: Some(self.expression),
defaults: self.defaults,
})
}
}
impl From<FormulaBuilder> for Formula {
fn from(value: FormulaBuilder) -> Self {
value
.build_inner()
.unwrap_or_else(|_| panic!("Failed to convert {0} to {1}", "FormulaBuilder", "Formula"))
}
}
impl FormulaBuilder {
pub fn build(self) -> Formula {
self.build_inner()
.unwrap_or_else(|_| panic!("Failed to build {0} into {1}", "FormulaBuilder", "Formula"))
}
}