api-types: rework BackupNamespace::map_prefix

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>
This commit is contained in:
Wolfgang Bumiller 2022-05-11 12:26:25 +02:00 committed by Thomas Lamprecht
parent 53d073ec1a
commit 6b4d057370
1 changed files with 14 additions and 17 deletions

View File

@ -702,25 +702,22 @@ impl BackupNamespace {
source_prefix: &BackupNamespace, source_prefix: &BackupNamespace,
target_prefix: &BackupNamespace, target_prefix: &BackupNamespace,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut mapped = target_prefix.clone(); let suffix = self
let mut source_iter = source_prefix.components(); .inner
let mut self_iter = self.components(); .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() { let mut new = target_prefix.clone();
if let Some(source_comp) = source_iter.next() { for item in suffix {
if source_comp != comp { new.push(item.clone())?;
bail!(
"Failed to map namespace - {} is not a valid prefix of {}",
source_prefix,
self
);
}
continue;
}
mapped.push(comp.to_owned())?;
} }
Ok(new)
Ok(mapped)
} }
} }