public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pdm-devel] [PATCH datacenter-manager] server: api: resources: make search terms (ascii) case insensitive
@ 2025-09-12  9:30 Dominik Csapak
  2025-09-15 14:41 ` Thomas Lamprecht
  0 siblings, 1 reply; 3+ messages in thread
From: Dominik Csapak @ 2025-09-12  9:30 UTC (permalink / raw)
  To: pdm-devel

by converting both the values and the terms to (ascii) lower case.

Making the search case insensitive seems more intuitive, since most
searches i could find behave this way (except e.g. in editors)

Since all values are currently ascii only, and this is faster than
`to_lowercase` using `to_ascii_lowercase` seems better here.

Also noticed that some matches can be replaced with the respective
MatchCategory matches, so we don't have to repeat this pattern
everywhere.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 server/src/api/resources.rs | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/server/src/api/resources.rs b/server/src/api/resources.rs
index afb53f42..d1ce0421 100644
--- a/server/src/api/resources.rs
+++ b/server/src/api/resources.rs
@@ -78,10 +78,12 @@ impl std::str::FromStr for MatchCategory {
 impl MatchCategory {
     fn matches(&self, value: &str, search_term: &str) -> bool {
         match self {
-            MatchCategory::Type | MatchCategory::Status => value.starts_with(search_term),
-            MatchCategory::Name | MatchCategory::Id | MatchCategory::Remote => {
-                value.contains(search_term)
-            }
+            MatchCategory::Type | MatchCategory::Status => value
+                .to_ascii_lowercase()
+                .starts_with(&search_term.to_ascii_lowercase()),
+            MatchCategory::Name | MatchCategory::Id | MatchCategory::Remote => value
+                .to_ascii_lowercase()
+                .contains(&search_term.to_ascii_lowercase()),
             MatchCategory::Template => match (parse_boolean(value), parse_boolean(search_term)) {
                 (Ok(a), Ok(b)) => a == b,
                 _ => false,
@@ -112,7 +114,10 @@ fn resource_matches_search_term(
             MatchCategory::Remote => category.matches(remote_name, &term.value),
         },
         Some(Err(_)) => false,
-        None => resource.name().contains(&term.value) || resource.id().contains(&term.value),
+        None => {
+            MatchCategory::Name.matches(resource.name(), &term.value)
+                || MatchCategory::Id.matches(&resource.id(), &term.value)
+        }
     };
     Some(matches)
 }
@@ -132,7 +137,10 @@ fn remote_matches_search_term(remote_name: &str, online: Option<bool>, term: &Se
             MatchCategory::Template => false,
         },
         Some(Err(_)) => false,
-        None => remote_name.contains(&term.value) || "remote".starts_with(&term.value),
+        None => {
+            MatchCategory::Name.matches(remote_name, &term.value)
+                || MatchCategory::Type.matches("remote", &term.value)
+        }
     }
 }
 
@@ -188,15 +196,14 @@ fn is_remotes_only(filters: &Search) -> bool {
         if term.is_optional() {
             optional_terms += 1;
         }
-        match term.category.as_deref() {
-            Some("type") if "remote".starts_with(&term.value) => {
+        match term.category.as_deref().map(|c| c.parse::<MatchCategory>()) {
+            Some(Ok(MatchCategory::Type)) if MatchCategory::Type.matches("remote", &term.value) => {
                 if !term.is_optional() {
                     is_required = true;
                 } else {
                     optional_matches += 1;
                 }
             }
-            None => {}
             _ => {}
         }
         // search is short-circuited, so to iterate over all, return true on required and false on optional
-- 
2.47.3



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-16  9:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-12  9:30 [pdm-devel] [PATCH datacenter-manager] server: api: resources: make search terms (ascii) case insensitive Dominik Csapak
2025-09-15 14:41 ` Thomas Lamprecht
2025-09-16  9:45   ` Dominik Csapak

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