]> git.proxmox.com Git - proxmox-backup.git/commitdiff
cleanup: implement FromStr for BackupGroup
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 23 Jun 2020 06:16:56 +0000 (08:16 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 23 Jun 2020 06:16:56 +0000 (08:16 +0200)
src/backup/backup_info.rs
src/bin/proxmox-backup-client.rs

index 214515f9c3ba07855cf1bbfb900ad125c7f431d8..0ae4f06617cef950f4d53e211117069ee5e0b932 100644 (file)
@@ -59,17 +59,6 @@ impl BackupGroup {
         &self.backup_id
     }
 
-    pub fn parse(path: &str) -> Result<Self, Error> {
-
-        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<Self, Self::Err> {
+        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;
 
index 9af5e12c35dc0c9989d6f244038e96d241e3aff4..418eac2d3db4dbce541e3c69d1b8e151153ac8ce 100644 (file)
@@ -427,8 +427,8 @@ async fn list_snapshots(param: Value) -> Result<Value, Error> {
 
     let client = connect(repo.host(), repo.user())?;
 
-    let group = if let Some(path) = param["group"].as_str() {
-        Some(BackupGroup::parse(path)?)
+    let group: Option<BackupGroup> = if let Some(path) = param["group"].as_str() {
+        Some(path.parse()?)
     } else {
         None
     };
@@ -1214,7 +1214,7 @@ async fn restore(param: Value) -> Result<Value, Error> {
     let path = tools::required_string_param(&param, "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<Value, Error> {
     let path = format!("api2/json/admin/datastore/{}/prune", repo.store());
 
     let group = tools::required_string_param(&param, "group")?;
-    let group = BackupGroup::parse(group)?;
+    let group: BackupGroup = group.parse()?;
 
     let output_format = get_output_format(&param);
 
@@ -2055,7 +2055,7 @@ async fn mount_do(param: Value, pipe: Option<RawFd>) -> Result<Value, Error> {
 
     let path = tools::required_string_param(&param, "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(&param, "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()?;