[pbs-devel] [PATCH proxmox-backup] (List)SyncDirection: extract match check into impl fn

Fabian Grünbichler f.gruenbichler at proxmox.com
Tue Nov 26 10:20:29 CET 2024


in case we add another direction or another call site, doing it without a
wildcard match arm seems cleaner.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
small cleanup as potential follow-up

 src/api2/admin/sync.rs  | 16 ++++++++++++----
 src/api2/config/sync.rs | 11 +++--------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/api2/admin/sync.rs b/src/api2/admin/sync.rs
index 965be8d06..089e6f50d 100644
--- a/src/api2/admin/sync.rs
+++ b/src/api2/admin/sync.rs
@@ -47,6 +47,16 @@ impl From<SyncDirection> for ListSyncDirection {
     }
 }
 
+impl ListSyncDirection {
+    /// Checks whether a `ListSyncDirection` matches a given `SyncDirection`
+    pub fn matches(&self, other: SyncDirection) -> bool {
+        if *self == ListSyncDirection::All {
+            return true;
+        }
+        *self == other.into()
+    }
+}
+
 #[api(
     input: {
         properties: {
@@ -94,10 +104,8 @@ pub fn list_config_sync_jobs(
             _ => {}
         }
 
-        match &sync_direction {
-            ListSyncDirection::Pull if direction != SyncDirection::Pull => continue,
-            ListSyncDirection::Push if direction != SyncDirection::Push => continue,
-            _ => {}
+        if !sync_direction.matches(direction) {
+            continue;
         }
 
         if !check_sync_job_read_access(&user_info, &auth_id, &job) {
diff --git a/src/api2/config/sync.rs b/src/api2/config/sync.rs
index e8a1ad076..bc012744a 100644
--- a/src/api2/config/sync.rs
+++ b/src/api2/config/sync.rs
@@ -15,9 +15,9 @@ use pbs_api_types::{
 };
 use pbs_config::sync;
 
+use crate::api2::admin::sync::ListSyncDirection;
 use pbs_config::CachedUserInfo;
 use pbs_datastore::check_backup_owner;
-use crate::api2::admin::sync::ListSyncDirection;
 
 pub fn check_sync_job_read_access(
     user_info: &CachedUserInfo,
@@ -185,13 +185,8 @@ pub fn list_sync_jobs(
     let list = list
         .into_iter()
         .filter(|sync_job| {
-            let direction = sync_job.sync_direction.unwrap_or_default();
-            match &sync_direction {
-                ListSyncDirection::Pull if direction != SyncDirection::Pull => return false,
-                ListSyncDirection::Push if direction != SyncDirection::Push => return false,
-                _ => {}
-            }
-            check_sync_job_read_access(&user_info, &auth_id, sync_job)
+            sync_direction.matches(sync_job.sync_direction.unwrap_or_default())
+                && check_sync_job_read_access(&user_info, &auth_id, sync_job)
         })
         .collect();
     Ok(list)
-- 
2.39.5





More information about the pbs-devel mailing list