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

View 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();
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)
}
}