diff --git a/src/backup/backup_info.rs b/src/backup/backup_info.rs index 214515f9..0ae4f066 100644 --- a/src/backup/backup_info.rs +++ b/src/backup/backup_info.rs @@ -59,17 +59,6 @@ impl BackupGroup { &self.backup_id } - pub fn parse(path: &str) -> Result { - - let cap = GROUP_PATH_REGEX.captures(path) - .ok_or_else(|| format_err!("unable to parse backup group path '{}'", path))?; - - Ok(Self { - backup_type: cap.get(1).unwrap().as_str().to_owned(), - backup_id: cap.get(2).unwrap().as_str().to_owned(), - }) - } - pub fn group_path(&self) -> PathBuf { let mut relative_path = PathBuf::new(); @@ -152,6 +141,23 @@ impl BackupGroup { } } +impl std::str::FromStr for BackupGroup { + type Err = Error; + + /// Parse a backup group path + /// + /// This parses strings like `vm/100". + fn from_str(path: &str) -> Result { + let cap = GROUP_PATH_REGEX.captures(path) + .ok_or_else(|| format_err!("unable to parse backup group path '{}'", path))?; + + Ok(Self { + backup_type: cap.get(1).unwrap().as_str().to_owned(), + backup_id: cap.get(2).unwrap().as_str().to_owned(), + }) + } +} + /// Uniquely identify a Backup (relative to data store) /// /// We also call this a backup snaphost. @@ -201,6 +207,7 @@ impl BackupDir { backup_time.to_rfc3339_opts(SecondsFormat::Secs, true) } } + impl std::str::FromStr for BackupDir { type Err = Error; diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 9af5e12c..418eac2d 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -427,8 +427,8 @@ async fn list_snapshots(param: Value) -> Result { let client = connect(repo.host(), repo.user())?; - let group = if let Some(path) = param["group"].as_str() { - Some(BackupGroup::parse(path)?) + let group: Option = if let Some(path) = param["group"].as_str() { + Some(path.parse()?) } else { None }; @@ -1214,7 +1214,7 @@ async fn restore(param: Value) -> Result { let path = tools::required_string_param(¶m, "snapshot")?; let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { - let group = BackupGroup::parse(path)?; + let group: BackupGroup = path.parse()?; api_datastore_latest_snapshot(&client, repo.store(), group).await? } else { let snapshot: BackupDir = path.parse()?; @@ -1438,7 +1438,7 @@ async fn prune_async(mut param: Value) -> Result { let path = format!("api2/json/admin/datastore/{}/prune", repo.store()); let group = tools::required_string_param(¶m, "group")?; - let group = BackupGroup::parse(group)?; + let group: BackupGroup = group.parse()?; let output_format = get_output_format(¶m); @@ -2055,7 +2055,7 @@ async fn mount_do(param: Value, pipe: Option) -> Result { let path = tools::required_string_param(¶m, "snapshot")?; let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { - let group = BackupGroup::parse(path)?; + let group: BackupGroup = path.parse()?; api_datastore_latest_snapshot(&client, repo.store(), group).await? } else { let snapshot: BackupDir = path.parse()?; @@ -2173,7 +2173,7 @@ async fn catalog_shell(param: Value) -> Result<(), Error> { let archive_name = tools::required_string_param(¶m, "archive-name")?; let (backup_type, backup_id, backup_time) = if path.matches('/').count() == 1 { - let group = BackupGroup::parse(path)?; + let group: BackupGroup = path.parse()?; api_datastore_latest_snapshot(&client, repo.store(), group).await? } else { let snapshot: BackupDir = path.parse()?;