matchmaker/nucleo/
mod.rs

1pub mod injector;
2pub mod query;
3pub mod variants;
4mod worker;
5
6use std::{
7    fmt::{self, Display, Formatter},
8    hash::{Hash, Hasher},
9};
10
11use arrayvec::ArrayVec;
12pub use variants::*;
13pub use worker::*;
14
15pub use nucleo;
16pub use ratatui::{
17    style::{Color, Modifier, Style, Stylize},
18    text::{Line, Span, Text},
19};
20
21use crate::{MAX_SPLITS, SegmentableItem};
22
23// ------------- Wrapper structs
24
25/// This struct implements ColumnIndexable, and can instantiate a worker with columns.
26#[derive(Debug, Clone, Hash, Eq, PartialEq)]
27pub struct Segmented<T: SegmentableItem> {
28    pub inner: T,
29    ranges: ArrayVec<(usize, usize), MAX_SPLITS>,
30}
31
32impl<T: SegmentableItem> ColumnIndexable for Segmented<T> {
33    fn index(&self, index: usize) -> &str {
34        if let Some((start, end)) = self.ranges.get(index) {
35            &self.inner[*start..*end]
36        } else {
37            ""
38        }
39    }
40}
41
42#[derive(Debug, Clone)]
43pub struct Indexed<T> {
44    pub index: u32,
45    pub inner: T,
46}
47
48impl<T: Clone> Indexed<T> {
49    /// Matchmaker requires a way to identify and store selected items from their references in the nucleo matcher. This method simply identifies them by their insertion index and stores the clones of the items.
50    pub fn identifier(&self) -> (u32, T) {
51        (self.index, self.inner.clone())
52    }
53}
54
55impl<T: ColumnIndexable> ColumnIndexable for Indexed<T> {
56    fn index(&self, index: usize) -> &str {
57        self.inner.index(index)
58    }
59}
60
61// ------------------------------------------
62impl<T: Display + SegmentableItem> Display for Segmented<T> {
63    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
64        write!(f, "{}", self.inner)
65    }
66}
67
68impl<T: Display> Display for Indexed<T> {
69    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
70        write!(f, "{}", self.inner)
71    }
72}
73
74impl<T> PartialEq for Indexed<T> {
75    fn eq(&self, other: &Self) -> bool {
76        self.index == other.index
77    }
78}
79
80impl<T> Eq for Indexed<T> {}
81
82impl<T> Hash for Indexed<T> {
83    fn hash<H: Hasher>(&self, state: &mut H) {
84        self.index.hash(state)
85    }
86}