From 1e4e1514d3de7d3aad1747e6e5d29be57b0ca42c Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Tue, 10 May 2022 16:06:41 +0200 Subject: [PATCH] pbs-api-types: add parse and print ns_and_snapshot these are helpers for the few cases where we want to print and parse from a format that has the namespace and snapshot combined, like for the on-tape catalog and snapshot archive. Signed-off-by: Dominik Csapak --- pbs-api-types/src/datastore.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs index 58c26a87..95ef7120 100644 --- a/pbs-api-types/src/datastore.rs +++ b/pbs-api-types/src/datastore.rs @@ -1463,3 +1463,26 @@ pub const ADMIN_DATASTORE_PRUNE_RETURN_TYPE: ReturnType = ReturnType { ) .schema(), }; + +/// Parse snapshots in the form 'ns/foo/ns/bar/ct/100/1970-01-01T00:00:00Z' +/// into a [`BackupNamespace`] and [`BackupDir`] +pub fn parse_ns_and_snapshot(input: &str) -> Result<(BackupNamespace, BackupDir), Error> { + match input.rmatch_indices('/').nth(2) { + Some((idx, _)) => { + let ns = BackupNamespace::from_path(&input[..idx])?; + let dir: BackupDir = (&input[idx + 1..]).parse()?; + Ok((ns, dir)) + } + None => Ok((BackupNamespace::root(), input.parse()?)), + } +} + +/// Prints a [`BackupNamespace`] and [`BackupDir`] in the form of +/// 'ns/foo/bar/ct/100/1970-01-01T00:00:00Z' +pub fn print_ns_and_snapshot(ns: &BackupNamespace, dir: &BackupDir) -> String { + if ns.is_root() { + dir.to_string() + } else { + format!("{}/{}", ns.display_as_path(), dir) + } +}