matchmaker/nucleo/
mod.rs

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