nn_rs/metrics/
metric_factory.rs

1use anyhow::Result;
2
3use crate::metrics;
4use crate::types::MetricFunction;
5
6/// Factory function to create the metric function for calculating distance
7/// between two vectors
8///
9/// # Parameters
10/// - metric: which metric function to create
11///
12/// # Return values
13/// - MetricFunction
14pub fn metric_factory(metric: &str) -> Result<MetricFunction> {
15    match metric {
16        "cosine" => Ok(metrics::cosine_distance),
17        "manhattan" => Ok(metrics::manhattan_distance),
18        "euclidean" => Ok(metrics::euclidean_distance),
19        _ => panic!("Did not recognise metric {}", metric),
20    }
21}
22
23
24#[cfg(test)]
25mod tests {
26
27    use super::*;
28    use anyhow::Result;
29    use crate::types::MetricFunction;
30
31    #[test]
32    #[ignore]
33    fn test_metric_factory() -> Result<()> {
34        // is this redundant? we already check that the metrics are fine. so here
35        // we want to check that the factory fn dispatches metric fns but this can't not be
36        // true since in the case that it didn't it wouldn't compile
37        let metrics = vec!["euclidean", "cosine", "manhattan"];
38        for metric in metrics{
39            let _metric_fn: MetricFunction  = metric_factory(metric)?;
40        }
41        Ok(())
42    }
43}