From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 326871FF17C for ; Wed, 3 Sep 2025 15:23:49 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 45D7BDF31; Wed, 3 Sep 2025 15:24:00 +0200 (CEST) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Date: Wed, 3 Sep 2025 15:09:18 +0200 Message-ID: <20250903132351.841830-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250903132351.841830-1-d.csapak@proxmox.com> References: <20250903132351.841830-1-d.csapak@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.022 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_MSPIKE_H2 0.001 Average reputation (+2) SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [PATCH datacenter-manager v5 02/10] lib: add pdm-search crate X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" Introduce a new create for search & filter related code. It currently includes basic parsing & testing of search terms. Intended to be used on some API calls that allow for more complex filters, such as the resources API. Contains a `SearchTerm` and a `Search` struct. The former represents a single term to search for, with an optional category and if it's optional or not. The latter represents a full search with multiple terms. Short syntax summary: 'sometext' : normal search term '+sometext' : required search term 'cat:text' : looks for 'text' in the category 'cat' required terms have to exist in the resulting match, while optional ones are OR'd (so at least one optional match must exist) This is loosely inspired by various search syntaxes, e.g. the one from gitlab[0] or query-dsl from elastic[1] [1] https://docs.gitlab.com/user/search/advanced_search/ [2] https://www.elastic.co/docs/reference/query-languages/query-dsl/query-dsl-simple-query-string-query Signed-off-by: Dominik Csapak --- changes from v4: * derive default * impl From<&str> instead of FromStr for SearchTerm, so it cannot fail now * removed category_value_required (a specific implementation lives in the api now) * added 'is_optional' to SearchTerm, so the matches callback can check that too * use Into instead of ToString * optimized the display trait for Search Cargo.toml | 2 + lib/pdm-search/Cargo.toml | 9 ++ lib/pdm-search/src/lib.rs | 267 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 278 insertions(+) create mode 100644 lib/pdm-search/Cargo.toml create mode 100644 lib/pdm-search/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index f07733d..ae2816a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ members = [ "lib/pdm-api-types", "lib/pdm-client", "lib/pdm-config", + "lib/pdm-search", "lib/pdm-ui-shared", "cli/client", @@ -86,6 +87,7 @@ pdm-api-types = { path = "lib/pdm-api-types" } pdm-buildcfg = { path = "lib/pdm-buildcfg" } pdm-config = { path = "lib/pdm-config" } pdm-client = { version = "0.2", path = "lib/pdm-client" } +pdm-search = { version = "0.2", path = "lib/pdm-search" } pdm-ui-shared = { version = "0.2", path = "lib/pdm-ui-shared" } proxmox-fido2 = { path = "cli/proxmox-fido2" } diff --git a/lib/pdm-search/Cargo.toml b/lib/pdm-search/Cargo.toml new file mode 100644 index 0000000..083d62c --- /dev/null +++ b/lib/pdm-search/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "pdm-search" +description = "Proxmox Datacenter Manager shared ui modules" +homepage = "https://www.proxmox.com" + +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true diff --git a/lib/pdm-search/src/lib.rs b/lib/pdm-search/src/lib.rs new file mode 100644 index 0000000..fa484cf --- /dev/null +++ b/lib/pdm-search/src/lib.rs @@ -0,0 +1,267 @@ +//! Abstraction over a [`Search`] that contains multiple [`SearchTerm`]s. +//! +//! Provides methods to filter an item over a combination of such terms and +//! construct them from text, and serialize them back to text. +use std::fmt; + +#[derive(Default, Clone)] +pub struct Search { + required_terms: Vec, + optional_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, + } + } +} + +impl> From for Search { + fn from(value: S) -> Self { + value + .as_ref() + .split_whitespace() + .map(SearchTerm::from) + .collect() + } +} + +impl Search { + /// Create a new empty [`Search`] + pub fn new() -> Self { + Self::with_terms(Vec::new()) + } + + /// Returns true if no [`SearchTerm`] exist + pub fn is_empty(&self) -> bool { + self.required_terms.is_empty() && self.optional_terms.is_empty() + } + + /// Create a new [`Search`] with the given [`SearchTerm`]s + pub fn with_terms>(terms: I) -> Self { + terms.into_iter().collect() + } + + /// Test if the given `Fn(&SearchTerm) -> bool` for all [`SearchTerm`] configured matches + /// + /// Returns true if it matches considering the constraints: + /// if there are no filters, returns true + pub fn matches bool>(&self, mut matches: F) -> bool { + if self.is_empty() { + return true; + } + + if self.required_terms.iter().map(&mut matches).any(|f| !f) { + return false; + } + + if !self.optional_terms.is_empty() + && self.optional_terms.iter().map(&mut matches).all(|f| !f) + { + return false; + } + + true + } +} + +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()) { + write!(f, "{sep}{term}")?; + sep = " "; + } + + Ok(()) + } +} + +#[derive(Debug, Clone, PartialEq)] +pub struct SearchTerm { + optional: bool, + pub value: String, + pub category: Option, +} + +impl SearchTerm { + /// Creates a new [`SearchTerm`]. + pub fn new>(term: S) -> Self { + Self { + value: term.into(), + optional: false, + category: None, + } + } + + /// Builder style method to set the category + pub fn category(mut self, category: Option) -> Self { + self.category = category.map(|s| s.to_string()); + self + } + + /// Builder style method to mark this [`SearchTerm`] as optional + pub fn optional(mut self, optional: bool) -> Self { + self.optional = optional; + self + } + + /// Returns if the search term is optional + pub fn is_optional(&self) -> bool { + self.optional + } +} + +impl> From for SearchTerm { + fn from(value: S) -> Self { + let term = value.as_ref(); + let mut optional = true; + let term = if let Some(rest) = term.strip_prefix("+") { + if rest.is_empty() { + term + } else { + optional = false; + rest + } + } else { + term + }; + + let (term, category) = match term.split_once(':') { + Some((category, new_term)) if category.is_empty() || new_term.is_empty() => { + (term, None) + } + Some((category, new_term)) => (new_term, Some(category)), + None => (term, None), + }; + + SearchTerm::new(term).optional(optional).category(category) + } +} + +impl fmt::Display for SearchTerm { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if !self.optional { + f.write_str("+")?; + } + + if let Some(cat) = &self.category { + f.write_str(cat)?; + f.write_str(":")?; + } + + f.write_str(&self.value) + } +} + +#[cfg(test)] +mod tests { + use crate::{Search, SearchTerm}; + + #[test] + fn parse_test_simple_filter() { + assert_eq!( + SearchTerm::from("foo"), + SearchTerm::new("foo").optional(true), + ); + } + + #[test] + fn parse_test_requires_filter() { + assert_eq!(SearchTerm::from("+foo"), SearchTerm::new("foo"),); + } + + #[test] + fn parse_test_category_filter() { + assert_eq!( + SearchTerm::from("foo:bar"), + SearchTerm::new("bar").optional(true).category(Some("foo")) + ); + assert_eq!( + SearchTerm::from("+foo:bar"), + SearchTerm::new("bar").category(Some("foo")) + ); + } + + #[test] + fn parse_test_special_filter() { + assert_eq!( + SearchTerm::from(":bar"), + SearchTerm::new(":bar").optional(true) + ); + assert_eq!(SearchTerm::from("+cat:"), SearchTerm::new("cat:")); + assert_eq!(SearchTerm::from("+"), SearchTerm::new("+").optional(true)); + assert_eq!(SearchTerm::from(":"), SearchTerm::new(":").optional(true)); + } + + #[test] + fn match_tests() { + let search = Search::from_iter(vec![ + SearchTerm::new("required1").optional(false), + SearchTerm::new("required2").optional(false), + SearchTerm::new("optional1").optional(true), + SearchTerm::new("optional2").optional(true), + ]); + + // each case contains results for + // required1, required2, optional1, optional2 + // and if it should match or not + let cases = [ + ((true, true, true, false), true), + ((true, true, false, true), true), + ((true, true, true, true), true), + ((true, true, false, false), false), + ((true, false, false, false), false), + ((false, false, false, false), false), + ((false, true, false, false), false), + ((false, false, true, true), false), + ((false, true, true, true), false), + ((true, false, true, true), false), + ((true, false, true, false), false), + ((false, true, true, false), false), + ((false, false, true, false), false), + ((true, false, false, true), false), + ((false, true, false, true), false), + ((false, false, false, true), false), + ]; + for (input, expected) in cases { + assert!( + search.matches(|term| { + match term.value.as_str() { + "required1" => input.0, + "required2" => input.1, + "optional1" => input.2, + "optional2" => input.3, + _ => unreachable!(), + } + }) == expected + ) + } + } + + #[test] + fn test_display() { + let term = SearchTerm::new("foo"); + assert_eq!("+foo", &term.to_string()); + + let term = SearchTerm::new("foo").optional(true); + assert_eq!("foo", &term.to_string()); + + let term = SearchTerm::new("foo").optional(false); + assert_eq!("+foo", &term.to_string()); + + let term = SearchTerm::new("foo").category(Some("bar")); + assert_eq!("+bar:foo", &term.to_string()); + + let term = SearchTerm::new("foo").optional(true).category(Some("bar")); + assert_eq!("bar:foo", &term.to_string()); + + let term = SearchTerm::new("foo").optional(false).category(Some("bar")); + assert_eq!("+bar:foo", &term.to_string()); + } +} -- 2.47.2 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel