diskann_vector/
metric.rs

1/*
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT license.
4 */
5#![warn(missing_debug_implementations, missing_docs)]
6use std::str::FromStr;
7
8/// Distance metric
9#[derive(Debug, PartialEq, Eq, Clone, Copy)]
10pub enum Metric {
11    /// Squared Euclidean (L2-Squared)
12    L2,
13
14    /// Cosine similarity
15    /// TODO: T should be float for Cosine distance
16    Cosine,
17}
18
19#[derive(thiserror::Error, Debug)]
20pub enum ParseMetricError {
21    #[error("Invalid format for Metric: {0}")]
22    InvalidFormat(String),
23}
24
25impl FromStr for Metric {
26    type Err = ParseMetricError;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s.to_lowercase().as_str() {
30            "l2" => Ok(Metric::L2),
31            "cosine" => Ok(Metric::Cosine),
32            _ => Err(ParseMetricError::InvalidFormat(String::from(s))),
33        }
34    }
35}