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