cleanup: implement FromStr for BackupDir

This commit is contained in:
Dietmar Maurer 2020-06-23 08:09:52 +02:00
parent c8137518fe
commit a67f7d0a07
2 changed files with 24 additions and 19 deletions

View File

@ -188,16 +188,6 @@ impl BackupDir {
self.backup_time
}
pub fn parse(path: &str) -> Result<Self, Error> {
let cap = SNAPSHOT_PATH_REGEX.captures(path)
.ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'", path))?;
let group = BackupGroup::new(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str());
let backup_time = cap.get(3).unwrap().as_str().parse::<DateTime<Utc>>()?;
Ok(BackupDir::from((group, backup_time.timestamp())))
}
pub fn relative_path(&self) -> PathBuf {
let mut relative_path = self.group.group_path();
@ -211,6 +201,21 @@ impl BackupDir {
backup_time.to_rfc3339_opts(SecondsFormat::Secs, true)
}
}
impl std::str::FromStr for BackupDir {
type Err = Error;
/// Parse a snapshot path
///
/// This parses strings like `host/elsa/2020-06-15T05:18:33Z".
fn from_str(path: &str) -> Result<Self, Self::Err> {
let cap = SNAPSHOT_PATH_REGEX.captures(path)
.ok_or_else(|| format_err!("unable to parse backup snapshot path '{}'", path))?;
let group = BackupGroup::new(cap.get(1).unwrap().as_str(), cap.get(2).unwrap().as_str());
let backup_time = cap.get(3).unwrap().as_str().parse::<DateTime<Utc>>()?;
Ok(BackupDir::from((group, backup_time.timestamp())))
}
}
impl std::fmt::Display for BackupDir {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

View File

@ -488,7 +488,7 @@ async fn forget_snapshots(param: Value) -> Result<Value, Error> {
let repo = extract_repository_from_value(&param)?;
let path = tools::required_string_param(&param, "snapshot")?;
let snapshot = BackupDir::parse(path)?;
let snapshot: BackupDir = path.parse()?;
let mut client = connect(repo.host(), repo.user())?;
@ -568,7 +568,7 @@ async fn dump_catalog(param: Value) -> Result<Value, Error> {
let repo = extract_repository_from_value(&param)?;
let path = tools::required_string_param(&param, "snapshot")?;
let snapshot = BackupDir::parse(path)?;
let snapshot: BackupDir = path.parse()?;
let keyfile = param["keyfile"].as_str().map(PathBuf::from);
@ -646,7 +646,7 @@ async fn list_snapshot_files(param: Value) -> Result<Value, Error> {
let repo = extract_repository_from_value(&param)?;
let path = tools::required_string_param(&param, "snapshot")?;
let snapshot = BackupDir::parse(path)?;
let snapshot: BackupDir = path.parse()?;
let output_format = get_output_format(&param);
@ -1217,7 +1217,7 @@ async fn restore(param: Value) -> Result<Value, Error> {
let group = BackupGroup::parse(path)?;
api_datastore_latest_snapshot(&client, repo.store(), group).await?
} else {
let snapshot = BackupDir::parse(path)?;
let snapshot: BackupDir = path.parse()?;
(snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
};
@ -1364,7 +1364,7 @@ async fn upload_log(param: Value) -> Result<Value, Error> {
let repo = extract_repository_from_value(&param)?;
let snapshot = tools::required_string_param(&param, "snapshot")?;
let snapshot = BackupDir::parse(snapshot)?;
let snapshot: BackupDir = snapshot.parse()?;
let mut client = connect(repo.host(), repo.user())?;
@ -1672,9 +1672,9 @@ async fn complete_server_file_name_do(param: &HashMap<String, String>) -> Vec<St
_ => return result,
};
let snapshot = match param.get("snapshot") {
let snapshot: BackupDir = match param.get("snapshot") {
Some(path) => {
match BackupDir::parse(path) {
match path.parse() {
Ok(v) => v,
_ => return result,
}
@ -2058,7 +2058,7 @@ async fn mount_do(param: Value, pipe: Option<RawFd>) -> Result<Value, Error> {
let group = BackupGroup::parse(path)?;
api_datastore_latest_snapshot(&client, repo.store(), group).await?
} else {
let snapshot = BackupDir::parse(path)?;
let snapshot: BackupDir = path.parse()?;
(snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
};
@ -2176,7 +2176,7 @@ async fn catalog_shell(param: Value) -> Result<(), Error> {
let group = BackupGroup::parse(path)?;
api_datastore_latest_snapshot(&client, repo.store(), group).await?
} else {
let snapshot = BackupDir::parse(path)?;
let snapshot: BackupDir = path.parse()?;
(snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time())
};