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

* Re: [pdm-devel] [PATCH datacenter-manager] server: api: resources: make search terms (ascii) case insensitive
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Lamprecht @ 2025-09-15 14:41 UTC (permalink / raw)
  To: Proxmox Datacenter Manager development discussion, Dominik Csapak

Am 12.09.25 um 11:31 schrieb Dominik Csapak:
> 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.

But is it really faster as long as the data only contains ascii?
Checking the implementation it still does ascii first and only a
more elaborate conversion if there's anything left over. Sure, more
work than here, but for the data sizes here that range mostly in the
tens to at max hundreds of bytes, besides some outlier.
So not sure if we should add limitations here, as this also affects
simple umlauts or characters with accents, which are quite common.

That said, most things we currently match are restricted to ascii,
so with a comment that tries to avoid copying this over for something
that is not certain to be ascii (like free form description/notes)
in the future, I'd be fine with doing it your proposed way.

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

Great, but might be even better to add some dedicated helpers private
to this module for that, like (rather verbose):

contains_case_insensitive

starts_with_case_insensitive

As you still need to pass all arguments explicitly anyway (like e.g.
"remote"), so it's not like reusing this gives you any type saftey
over a simple helper, while as being proposed it abstracts away the
simple thing that happens under the hood, so IMO not a very good kind
of abstraction/reuse (but certainly a matter of taste to a degree).
Creating a simple trait for the search and implementing it for &str
in this module might be an alternative to make it a bit more ergonomic
to rust, but not sure if that's worth it (or better), just wanted to
throw out the idea.


_______________________________________________
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

* Re: [pdm-devel] [PATCH datacenter-manager] server: api: resources: make search terms (ascii) case insensitive
  2025-09-15 14:41 ` Thomas Lamprecht
@ 2025-09-16  9:45   ` Dominik Csapak
  0 siblings, 0 replies; 3+ messages in thread
From: Dominik Csapak @ 2025-09-16  9:45 UTC (permalink / raw)
  To: Thomas Lamprecht, Proxmox Datacenter Manager development discussion



On 9/15/25 4:41 PM, Thomas Lamprecht wrote:
> Am 12.09.25 um 11:31 schrieb Dominik Csapak:
>> 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.
> 
> But is it really faster as long as the data only contains ascii?
> Checking the implementation it still does ascii first and only a
> more elaborate conversion if there's anything left over. Sure, more
> work than here, but for the data sizes here that range mostly in the
> tens to at max hundreds of bytes, besides some outlier.
> So not sure if we should add limitations here, as this also affects
> simple umlauts or characters with accents, which are quite common.
> 
> That said, most things we currently match are restricted to ascii,
> so with a comment that tries to avoid copying this over for something
> that is not certain to be ascii (like free form description/notes)
> in the future, I'd be fine with doing it your proposed way.
> 

ok, after re-thinking this, i believe it makes sense to just use
to_lowercase here after all. you're right that the terms/values
are not that long, and mostly ascii anyway so the overhead
should be negligible vs the downside of potentially not matching
correctly should we include any non-ascii data here..

>>
>> Also noticed that some matches can be replaced with the respective
>> MatchCategory matches, so we don't have to repeat this pattern
>> everywhere.
> 
> Great, but might be even better to add some dedicated helpers private
> to this module for that, like (rather verbose):
> 
> contains_case_insensitive
> 
> starts_with_case_insensitive
> 
> As you still need to pass all arguments explicitly anyway (like e.g.
> "remote"), so it's not like reusing this gives you any type saftey
> over a simple helper, while as being proposed it abstracts away the
> simple thing that happens under the hood, so IMO not a very good kind
> of abstraction/reuse (but certainly a matter of taste to a degree).
> Creating a simple trait for the search and implementing it for &str
> in this module might be an alternative to make it a bit more ergonomic
> to rust, but not sure if that's worth it (or better), just wanted to
> throw out the idea.


not super sure what you're proposing here, do you mean we should
introduce these helpers in addition to using the category matches
or instead of?

the reason why i used the category matchers here (besides reusing the 
code) is to get a consistent behavior for the search itself
(contains vs starts_with) for the various fields, so i'd like to
to use them in any case

with that in mind, i don't think the helpers you propose have
much value, as they'll only be used in one place each


_______________________________________________
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