norm/metrics/fzf/
distance.rs

1use core::cmp::{Ord, PartialOrd};
2
3pub(super) type Score = i64;
4
5/// The fzf distance type.
6///
7/// This struct is returned by [`FzfV1`](super::FzfV1) and
8/// [`FzfV2`](super::FzfV2)'s [`Metric`](crate::Metric) implementations.
9#[derive(Debug, Clone, Copy, Eq, PartialEq)]
10pub struct FzfDistance(Score);
11
12impl PartialOrd for FzfDistance {
13    #[inline]
14    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
15        Some(self.cmp(other))
16    }
17}
18
19impl Ord for FzfDistance {
20    #[inline]
21    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
22        // This makes the type act like a distance and not like a score.
23        other.0.cmp(&self.0)
24    }
25}
26
27impl Default for FzfDistance {
28    #[inline]
29    fn default() -> Self {
30        Self::from_score(0)
31    }
32}
33
34impl FzfDistance {
35    /// Creates a new [`FzfDistance`] from a score.
36    #[inline(always)]
37    pub(super) fn from_score(score: Score) -> Self {
38        Self(score)
39    }
40
41    /// Returns a score representation of the distance.
42    ///
43    /// This is not part of the public API and should not be relied upon.
44    ///
45    /// It's only used internally for testing and debugging purposes.
46    #[cfg(any(feature = "__into-score", feature = "__tests"))]
47    #[inline(always)]
48    pub fn into_score(self) -> Score {
49        self.0
50    }
51}