Skip to content

Commit

Permalink
Replace recipe search with a filter
Browse files Browse the repository at this point in the history
This feels a bit more useful and intuitive
  • Loading branch information
LucasPickering committed Dec 21, 2024
1 parent 355d980 commit da7b35d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 133 deletions.
42 changes: 28 additions & 14 deletions crates/core/src/collection/recipe_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,36 @@ pub struct RecipeTree {

/// A path into the recipe tree. Every constructed path is assumed to be valid,
/// which must be enforced by the creator.
#[derive(Clone, Debug, From)]
#[cfg_attr(any(test, feature = "test"), derive(PartialEq))]
#[derive(Clone, Debug, From, Eq, Hash, PartialEq)]
pub struct RecipeLookupKey(Vec<RecipeId>);

impl RecipeLookupKey {
/// How many nodes are above us in the tree?
pub fn depth(&self) -> usize {
self.0.len() - 1
}

/// Get all parent IDs, starting at the root
pub fn ancestors(&self) -> &[RecipeId] {
&self.0[0..self.0.len() - 1]
}
}

impl From<&Vec<&RecipeId>> for RecipeLookupKey {
fn from(value: &Vec<&RecipeId>) -> Self {
Self(value.iter().copied().cloned().collect())
}
}

impl IntoIterator for RecipeLookupKey {
type Item = RecipeId;
type IntoIter = <Vec<RecipeId> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

/// A node in the recipe tree, either a folder or recipe
#[derive(Debug, From, Serialize, Deserialize, EnumDiscriminants)]
#[strum_discriminants(name(RecipeNodeType))]
Expand Down Expand Up @@ -248,18 +274,6 @@ impl RecipeNode {
}
}

impl RecipeLookupKey {
pub fn as_slice(&self) -> &[RecipeId] {
&self.0
}
}

impl From<&Vec<&RecipeId>> for RecipeLookupKey {
fn from(value: &Vec<&RecipeId>) -> Self {
Self(value.iter().copied().cloned().collect())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
47 changes: 26 additions & 21 deletions crates/tui/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,7 @@ use tokio::sync::mpsc::{self, UnboundedReceiver};
/// Get a test harness, with a clean terminal etc. See [TestHarness].
#[fixture]
pub fn harness() -> TestHarness {
TuiContext::init_test();
let (messages_tx, messages_rx) = mpsc::unbounded_channel();
let messages_tx: MessageSender = messages_tx.into();
let collection = Collection::factory(()).into();
let database = CollectionDatabase::factory(());
let request_store = Rc::new(RefCell::new(RequestStore::new(
database.clone(),
TestResponseParser,
)));
ViewContext::init(
Arc::clone(&collection),
database.clone(),
messages_tx.clone(),
);
TestHarness {
collection,
database,
request_store,
messages_tx,
messages_rx,
}
TestHarness::new(Collection::factory(()))
}

/// A container for all singleton types needed for tests. Most TUI tests will
Expand All @@ -63,6 +43,31 @@ pub struct TestHarness {
}

impl TestHarness {
/// Create a new test harness and initialize state
pub fn new(collection: Collection) -> Self {
TuiContext::init_test();
let (messages_tx, messages_rx) = mpsc::unbounded_channel();
let messages_tx: MessageSender = messages_tx.into();
let database = CollectionDatabase::factory(());
let request_store = Rc::new(RefCell::new(RequestStore::new(
database.clone(),
TestResponseParser,
)));
let collection = Arc::new(collection);
ViewContext::init(
Arc::clone(&collection),
database.clone(),
messages_tx.clone(),
);
TestHarness {
collection,
database,
request_store,
messages_tx,
messages_rx,
}
}

/// Get the message sender
pub fn messages_tx(&self) -> &MessageSender {
&self.messages_tx
Expand Down
Loading

0 comments on commit da7b35d

Please sign in to comment.