From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 0716A1FF09F for ; Tue, 01 Dec 2026 12:44:40 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 6F6B520CBC; Tue, 01 Dec 2026 12:44:39 +0100 (CET) From: Dominik Csapak 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 Message-ID: <20261201114434.1717720-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201114434.1717720-1-d.csapak@proxmox.com> References: <20261201114434.1717720-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.245 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 6HSS6MVGJ2DFP4UA6T6GSLMJBJPHASY5 X-Message-ID-Hash: 6HSS6MVGJ2DFP4UA6T6GSLMJBJPHASY5 X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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, - optional_terms: Vec, + // in the given order, so that serializing it does not reorder the terms + terms: Vec, } impl FromIterator for Search { fn from_iter>(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 bool>(&mut self, mut filter: F) -> Vec { + 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 { - 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 = 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