From cf93fbb8935661865f60359bdcf29a7fe92cbe2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Fri, 29 Apr 2022 13:43:58 +0200 Subject: [PATCH] api: add prefix-mapping helper to BackupNamespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit given a namespace, a source prefix and a target prefix this helper strips the source prefix and replaces it with the target one (erroring out if the prefix doesn't match). Signed-off-by: Fabian Grünbichler Signed-off-by: Thomas Lamprecht --- pbs-api-types/src/datastore.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs index 55e4c152..1a68c356 100644 --- a/pbs-api-types/src/datastore.rs +++ b/pbs-api-types/src/datastore.rs @@ -687,6 +687,33 @@ impl BackupNamespace { pub fn components(&self) -> impl Iterator + '_ { self.inner.iter().map(String::as_str) } + + /// Map NS by replacing `source_prefix` with `target_prefix` + pub fn map_prefix( + &self, + 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(); + + 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())?; + } + + Ok(mapped) + } } impl fmt::Display for BackupNamespace {