Skip to main content

qdrant_client/builders/
formula_builder.rs

1use std::collections::HashMap;
2
3use crate::qdrant::*;
4
5/// Builder for the Formula struct, which represents a scoring formula for points.
6///
7/// The Formula struct is used to define custom scoring expressions and default values.
8#[derive(Clone)]
9pub struct FormulaBuilder {
10    /// The expression that defines how to score points.
11    pub(crate) expression: Expression,
12    /// Default values to use for undefined variables in the expression.
13    pub(crate) defaults: HashMap<String, Value>,
14}
15
16impl FormulaBuilder {
17    /// Sets the expression for the formula.
18    pub fn new<E: Into<Expression>>(expression: E) -> Self {
19        Self {
20            expression: expression.into(),
21            defaults: Default::default(),
22        }
23    }
24
25    pub fn expression<E: Into<Expression>>(self, value: E) -> Self {
26        let mut new = self;
27        new.expression = value.into();
28        new
29    }
30
31    /// Sets all default values for the formula.
32    pub fn defaults(mut self, defaults: HashMap<String, Value>) -> Self {
33        self.defaults = defaults;
34        self
35    }
36
37    /// Adds a single default value to the formula's defaults.
38    pub fn add_default<K: Into<String>, V: Into<Value>>(self, key: K, value: V) -> Self {
39        let mut new = self;
40        new.defaults.insert(key.into(), value.into());
41        new
42    }
43
44    fn build_inner(self) -> Result<Formula, std::convert::Infallible> {
45        Ok(Formula {
46            expression: Some(self.expression),
47            defaults: self.defaults,
48        })
49    }
50}
51
52impl From<FormulaBuilder> for Formula {
53    fn from(value: FormulaBuilder) -> Self {
54        value
55            .build_inner()
56            .unwrap_or_else(|_| panic!("Failed to convert {0} to {1}", "FormulaBuilder", "Formula"))
57    }
58}
59
60impl FormulaBuilder {
61    /// Builds the desired Formula type.
62    pub fn build(self) -> Formula {
63        self.build_inner()
64            .unwrap_or_else(|_| panic!("Failed to build {0} into {1}", "FormulaBuilder", "Formula"))
65    }
66}