cleanup: implement FromStr for BackupGroup

This commit is contained in:
Dietmar Maurer 2020-06-23 08:16:56 +02:00
parent a67f7d0a07
commit d6d3b353be
2 changed files with 24 additions and 17 deletions

View 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;

View 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()?;