public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager 1/3] pdm-search: keep the order of the search terms
Date: Tue,  1 Dec 2026 12:34:37 +0100	[thread overview]
Message-ID: <20261201114434.1717720-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261201114434.1717720-1-d.csapak@proxmox.com>

and add `take_terms` to take out terms with an arbitrary filter.

Since the terms were stored split into required and optional ones,
serializing a search put all required terms first, regardless of the
order they were given in. Keep them in a single list instead, so the
UI can show and serialize the terms in the order they were entered.

`take_terms` generalizes `take_category`, so the UI can take out all
categorized terms of a search at once.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 lib/pdm-search/src/lib.rs | 62 ++++++++++++++++++++++++---------------
 1 file changed, 38 insertions(+), 24 deletions(-)

diff --git a/lib/pdm-search/src/lib.rs b/lib/pdm-search/src/lib.rs
index 66a335bf..df35aa4c 100644
--- a/lib/pdm-search/src/lib.rs
+++ b/lib/pdm-search/src/lib.rs
@@ -6,17 +6,14 @@ use std::fmt;
 
 #[derive(Default, Clone)]
 pub struct Search {
-    required_terms: Vec<SearchTerm>,
-    optional_terms: Vec<SearchTerm>,
+    // in the given order, so that serializing it does not reorder the terms
+    terms: Vec<SearchTerm>,
 }
 
 impl FromIterator<SearchTerm> for Search {
     fn from_iter<T: IntoIterator<Item = SearchTerm>>(iter: T) -> Self {
-        let (optional_terms, required_terms) = iter.into_iter().partition(|term| term.optional);
-
         Self {
-            required_terms,
-            optional_terms,
+            terms: iter.into_iter().collect(),
         }
     }
 }
@@ -39,7 +36,7 @@ impl Search {
 
     /// Returns true if no [`SearchTerm`] exist
     pub fn is_empty(&self) -> bool {
-        self.required_terms.is_empty() && self.optional_terms.is_empty()
+        self.terms.is_empty()
     }
 
     /// Create a new [`Search`] with the given [`SearchTerm`]s
@@ -56,13 +53,17 @@ impl Search {
             return true;
         }
 
-        if self.required_terms.iter().map(&mut matches).any(|f| !f) {
+        let mut required = self.terms.iter().filter(|term| !term.is_optional());
+        if required.any(|term| !matches(term)) {
             return false;
         }
 
-        if !self.optional_terms.is_empty()
-            && self.optional_terms.iter().map(&mut matches).all(|f| !f)
-        {
+        let mut optional = self
+            .terms
+            .iter()
+            .filter(|term| term.is_optional())
+            .peekable();
+        if optional.peek().is_some() && !optional.any(matches) {
             return false;
         }
 
@@ -71,31 +72,28 @@ impl Search {
 
     /// Add a term to the search
     pub fn add_term(&mut self, term: SearchTerm) {
-        if term.is_optional() {
-            self.optional_terms.push(term);
-        } else {
-            self.required_terms.push(term);
-        }
+        self.terms.push(term);
+    }
+
+    /// Remove all [`SearchTerm`]s for which `filter` returns true and return them
+    pub fn take_terms<F: FnMut(&SearchTerm) -> bool>(&mut self, mut filter: F) -> Vec<SearchTerm> {
+        self.terms.extract_if(.., |term| filter(term)).collect()
     }
 
     /// Remove all [`SearchTerm`]s with the given category, ignoring ASCII case, and return them
     pub fn take_category(&mut self, category: &str) -> Vec<SearchTerm> {
-        let has_category = |term: &mut SearchTerm| {
+        self.take_terms(|term| {
             term.category
                 .as_deref()
                 .is_some_and(|c| c.eq_ignore_ascii_case(category))
-        };
-
-        let mut terms: Vec<SearchTerm> = self.required_terms.extract_if(.., has_category).collect();
-        terms.extend(self.optional_terms.extract_if(.., has_category));
-        terms
+        })
     }
 }
 
 impl fmt::Display for Search {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         let mut sep = "";
-        for term in self.required_terms.iter().chain(self.optional_terms.iter()) {
+        for term in &self.terms {
             write!(f, "{sep}{term}")?;
             sep = " ";
         }
@@ -277,11 +275,27 @@ mod tests {
                 SearchTerm::new("b").optional(true).category(Some("View")),
             ]
         );
-        assert_eq!(search.to_string(), "+bar:view foo view:");
+        assert_eq!(search.to_string(), "foo +bar:view view:");
 
         assert!(search.take_category("view").is_empty());
     }
 
+    #[test]
+    fn take_terms_keeps_order() {
+        let mut search = Search::from("foo +type:qemu +bar status:running baz");
+
+        assert_eq!(
+            search.take_terms(|term| term.category.is_some()),
+            vec![
+                SearchTerm::new("qemu").category(Some("type")),
+                SearchTerm::new("running")
+                    .optional(true)
+                    .category(Some("status")),
+            ]
+        );
+        assert_eq!(search.to_string(), "foo +bar baz");
+    }
+
     #[test]
     fn test_display() {
         let term = SearchTerm::new("foo");
-- 
2.47.3





  reply	other threads:[~2026-12-01 11:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 11:34 [PATCH datacenter-manager 0/3] ui: search: improve filter UX Dominik Csapak
2026-12-01 11:34 ` Dominik Csapak [this message]
2026-12-01 11:34 ` [PATCH datacenter-manager 2/3] ui: search box: show categorized search terms as chips Dominik Csapak
2026-12-01 11:34 ` [PATCH datacenter-manager 3/3] ui: search box: add a panel to add and edit search filters Dominik Csapak
2026-12-01 15:47 ` superseded: [PATCH datacenter-manager 0/3] ui: search: improve filter UX Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201114434.1717720-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal