This should be a bit more intuitive to users than the current
behavior, which is 'always match' for mode==all and 'never match' for
mode==any. The current behavior originates in the neutral element of
the underlying logical operation (and, or).

Signed-off-by: Lukas Wagner <l.wag...@proxmox.com>
---
 proxmox-notify/src/matcher.rs | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/proxmox-notify/src/matcher.rs b/proxmox-notify/src/matcher.rs
index e299fd0..553ca87 100644
--- a/proxmox-notify/src/matcher.rs
+++ b/proxmox-notify/src/matcher.rs
@@ -265,17 +265,22 @@ impl MatcherConfig {
         let mode = self.mode.unwrap_or_default();
 
         let mut is_match = mode.neutral_element();
+        // If there are no matching directives, the matcher will always match
+        let mut no_matchers = true;
 
         if let Some(severity_matchers) = self.match_severity.as_deref() {
+            no_matchers = false;
             is_match = mode.apply(
                 is_match,
                 self.check_matches(notification, severity_matchers)?,
             );
         }
         if let Some(field_matchers) = self.match_field.as_deref() {
+            no_matchers = false;
             is_match = mode.apply(is_match, self.check_matches(notification, 
field_matchers)?);
         }
         if let Some(calendar_matchers) = self.match_calendar.as_deref() {
+            no_matchers = false;
             is_match = mode.apply(
                 is_match,
                 self.check_matches(notification, calendar_matchers)?,
@@ -284,7 +289,7 @@ impl MatcherConfig {
 
         let invert_match = self.invert_match.unwrap_or_default();
 
-        Ok(if is_match != invert_match {
+        Ok(if is_match != invert_match || no_matchers {
             Some(self.target.as_deref().unwrap_or_default())
         } else {
             None
@@ -455,4 +460,25 @@ mod tests {
         let matcher: SeverityMatcher = 
"info,notice,warning,error".parse().unwrap();
         assert!(matcher.matches(&notification).unwrap());
     }
+
+    #[test]
+    fn test_empty_matcher_matches_always() {
+        let notification = Notification::new_templated(
+            Severity::Notice,
+            "test",
+            "test",
+            Value::Null,
+            Default::default(),
+        );
+
+        for mode in [MatchModeOperator::All, MatchModeOperator::Any] {
+            let config = MatcherConfig {
+                name: "matcher".to_string(),
+                mode: Some(mode),
+                ..Default::default()
+            };
+
+            assert!(config.matches(&notification).unwrap().is_some())
+        }
+    }
 }
-- 
2.39.2



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

Reply via email to