]> git.proxmox.com Git - proxmox-backup.git/commitdiff
api-types: rework BackupNamespace::map_prefix
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 11 May 2022 10:26:25 +0000 (12:26 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 12 May 2022 07:33:50 +0000 (09:33 +0200)
to use slice::strip_prefix() from std

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
pbs-api-types/src/datastore.rs

index 6d846ae86f38a71efef81f14f7960f2340a9f0be..9a549fb0f0abf5e7f0efe818ad4bbf9abc43f0ba 100644 (file)
@@ -702,25 +702,22 @@ impl BackupNamespace {
         source_prefix: &BackupNamespace,
         target_prefix: &BackupNamespace,
     ) -> Result<Self, Error> {
-        let mut mapped = target_prefix.clone();
-        let mut source_iter = source_prefix.components();
-        let mut self_iter = self.components();
-
-        while let Some(comp) = self_iter.next() {
-            if let Some(source_comp) = source_iter.next() {
-                if source_comp != comp {
-                    bail!(
-                        "Failed to map namespace - {} is not a valid prefix of {}",
-                        source_prefix,
-                        self
-                    );
-                }
-                continue;
-            }
-            mapped.push(comp.to_owned())?;
+        let suffix = self
+            .inner
+            .strip_prefix(&source_prefix.inner[..])
+            .ok_or_else(|| {
+                format_err!(
+                    "Failed to map namespace - {} is not a valid prefix of {}",
+                    source_prefix,
+                    self
+                )
+            })?;
+
+        let mut new = target_prefix.clone();
+        for item in suffix {
+            new.push(item.clone())?;
         }
-
-        Ok(mapped)
+        Ok(new)
     }
 }