src/client/backup_repo.rs: implement FromStr trait

This commit is contained in:
Dietmar Maurer 2019-03-13 10:09:39 +01:00
parent 874acb7039
commit edd3c8c605
3 changed files with 31 additions and 29 deletions

View File

@ -190,7 +190,7 @@ fn list_backups(
) -> Result<Value, Error> {
let repo_url = tools::required_string_param(&param, "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<Value, Error> {
let repo_url = tools::required_string_param(&param, "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<Value, Error> {
let repo_url = tools::required_string_param(&param, "repository")?;
let repo = BackupRepository::parse(repo_url)?;
let repo: BackupRepository = repo_url.parse()?;
let path = tools::required_string_param(&param, "group")?;
let group = BackupGroup::parse(path)?;
@ -336,7 +336,7 @@ fn forget_snapshots(
) -> Result<Value, Error> {
let repo_url = tools::required_string_param(&param, "repository")?;
let repo = BackupRepository::parse(repo_url)?;
let repo: BackupRepository = repo_url.parse()?;
let path = tools::required_string_param(&param, "snapshot")?;
let snapshot = BackupDir::parse(path)?;
@ -365,7 +365,7 @@ fn start_garbage_collection(
) -> Result<Value, Error> {
let repo_url = tools::required_string_param(&param, "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(&param, "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<Value, Error> {
let repo_url = tools::required_string_param(&param, "repository")?;
let repo = BackupRepository::parse(repo_url)?;
let repo: BackupRepository = repo_url.parse()?;
let archive_name = tools::required_string_param(&param, "archive-name")?;
@ -566,7 +566,7 @@ fn prune(
) -> Result<Value, Error> {
let repo_url = tools::required_string_param(&param, "repository")?;
let repo = BackupRepository::parse(repo_url)?;
let repo: BackupRepository = repo_url.parse()?;
let mut client = HttpClient::new(repo.host(), repo.user());

View File

@ -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);

View File

@ -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<Self, Error> {
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<Self, Self::Err> {
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(),
})
}
}