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