From edd3c8c605fa651845da0c238681a872ca573819 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 13 Mar 2019 10:09:39 +0100 Subject: [PATCH] src/client/backup_repo.rs: implement FromStr trait --- src/bin/proxmox-backup-client.rs | 16 +++++----- src/bin/proxmox-protocol-testclient.rs | 2 +- src/client/backup_repo.rs | 42 ++++++++++++++------------ 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 64645442..6bdee694 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -190,7 +190,7 @@ fn list_backups( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let mut client = HttpClient::new(repo.host(), repo.user()); @@ -231,7 +231,7 @@ fn list_backup_groups( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let mut client = HttpClient::new(repo.host(), repo.user()); @@ -288,7 +288,7 @@ fn list_snapshots( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let path = tools::required_string_param(¶m, "group")?; let group = BackupGroup::parse(path)?; @@ -336,7 +336,7 @@ fn forget_snapshots( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let path = tools::required_string_param(¶m, "snapshot")?; let snapshot = BackupDir::parse(path)?; @@ -365,7 +365,7 @@ fn start_garbage_collection( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let mut client = HttpClient::new(repo.host(), repo.user()); @@ -396,7 +396,7 @@ fn create_backup( let backupspec_list = tools::required_array_param(¶m, "backupspec")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false); @@ -495,7 +495,7 @@ fn restore( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let archive_name = tools::required_string_param(¶m, "archive-name")?; @@ -566,7 +566,7 @@ fn prune( ) -> Result { let repo_url = tools::required_string_param(¶m, "repository")?; - let repo = BackupRepository::parse(repo_url)?; + let repo: BackupRepository = repo_url.parse()?; let mut client = HttpClient::new(repo.host(), repo.user()); diff --git a/src/bin/proxmox-protocol-testclient.rs b/src/bin/proxmox-protocol-testclient.rs index 045bdd81..a42be22d 100644 --- a/src/bin/proxmox-protocol-testclient.rs +++ b/src/bin/proxmox-protocol-testclient.rs @@ -593,7 +593,7 @@ fn main() { // optional previous backup: let previous = args.next().map(|s| s.to_string()); - let repo = match BackupRepository::parse(&repo) { + let repo: BackupRepository = match repo.parse() { Ok(repo) => repo, Err(e) => { eprintln!("error parsing repository: {}", e); diff --git a/src/client/backup_repo.rs b/src/client/backup_repo.rs index 64cd9866..be4df1dd 100644 --- a/src/client/backup_repo.rs +++ b/src/client/backup_repo.rs @@ -32,26 +32,6 @@ pub struct BackupRepository { impl BackupRepository { - /// Parse a repository URL. - /// - /// This parses strings like `user@host:datastore`. The `user` and - /// `host` parts are optional, where `host` defaults to the local - /// host, and `user` defaults to `root@pam`. - pub fn parse(url: &str) -> Result { - - let cap = BACKUP_REPO_URL_REGEX.captures(url) - .ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?; - - - Ok(BackupRepository { - //user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(), - //host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(), - user: cap.get(1).map(|m| m.as_str().to_owned()), - host: cap.get(2).map(|m| m.as_str().to_owned()), - store: cap[3].to_owned(), - }) - } - pub fn user(&self) -> &str { if let Some(ref user) = self.user { return user; @@ -82,3 +62,25 @@ impl fmt::Display for BackupRepository { } } } + +impl std::str::FromStr for BackupRepository { + type Err = Error; + + /// Parse a repository URL. + /// + /// This parses strings like `user@host:datastore`. The `user` and + /// `host` parts are optional, where `host` defaults to the local + /// host, and `user` defaults to `root@pam`. + fn from_str(url: &str) -> Result { + + let cap = BACKUP_REPO_URL_REGEX.captures(url) + .ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?; + + Ok(Self { + user: cap.get(1).map(|m| m.as_str().to_owned()), + host: cap.get(2).map(|m| m.as_str().to_owned()), + store: cap[3].to_owned(), + }) + } +} +