From 8f21c992a743e0ae538fdc794fd81b8e274325d9 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 11 May 2022 12:26:25 +0200 Subject: [PATCH] api-types: rework BackupNamespace::map_prefix to use slice::strip_prefix() from std Signed-off-by: Wolfgang Bumiller Signed-off-by: Thomas Lamprecht --- pbs-api-types/src/datastore.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs index 6d846ae8..9a549fb0 100644 --- a/pbs-api-types/src/datastore.rs +++ b/pbs-api-types/src/datastore.rs @@ -702,25 +702,22 @@ impl BackupNamespace { source_prefix: &BackupNamespace, target_prefix: &BackupNamespace, ) -> Result { - let mut mapped = target_prefix.clone(); - let mut source_iter = source_prefix.components(); - let mut self_iter = self.components(); + 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 + ) + })?; - 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 mut new = target_prefix.clone(); + for item in suffix { + new.push(item.clone())?; } - - Ok(mapped) + Ok(new) } }