Skip to main content

alf/tui/state/
data.rs

1//! Entry data storage
2
3use crate::models::AliasEntry;
4
5/// Entry data storage
6#[derive(Debug, Clone)]
7pub struct EntryData {
8   /// All loaded alias/function entries
9   entries: Vec<AliasEntry>,
10   /// Filtered and searched entries (indexes into `entries`)
11   visible_indices: Vec<usize>,
12}
13
14impl EntryData {
15   /// Create a new EntryData instance with the given entries
16   pub fn new(entries: Vec<AliasEntry>) -> Self {
17      Self {
18         entries,
19         visible_indices: Vec::new(),
20      }
21   }
22
23   /// Get reference to all entries
24   pub fn entries(&self) -> &[AliasEntry] {
25      &self.entries
26   }
27
28   /// Get reference to visible indices
29   pub fn visible_indices(&self) -> &[usize] {
30      &self.visible_indices
31   }
32
33   /// Get mutable reference to visible indices (for filtering operations)
34   pub(super) fn visible_indices_mut(&mut self) -> &mut Vec<usize> {
35      &mut self.visible_indices
36   }
37
38   /// Get the entry at the given visible index
39   pub fn get_visible_entry(
40      &self,
41      selected_index: usize,
42   ) -> Option<&AliasEntry> {
43      self.visible_indices.get(selected_index).and_then(|&idx| self.entries.get(idx))
44   }
45
46   /// Get total number of visible entries
47   pub fn visible_count(&self) -> usize {
48      self.visible_indices.len()
49   }
50
51   /// Check if there are no visible entries
52   pub fn is_empty(&self) -> bool {
53      self.visible_indices.is_empty()
54   }
55
56   /// Sort visible indices with a comparison function
57   /// This method safely handles the borrow checker by splitting the data and indices
58   pub(super) fn sort_visible_indices<F>(
59      &mut self,
60      mut compare: F,
61   ) where
62      F: FnMut(&crate::models::AliasEntry, &crate::models::AliasEntry) -> std::cmp::Ordering,
63   {
64      self.visible_indices.sort_by(|&a, &b| compare(&self.entries[a], &self.entries[b]));
65   }
66}
67
68#[cfg(test)]
69#[path = "data_tests.rs"]
70mod data_tests;